1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
from typing import Union
from pydantic import BaseModel, field_validator
from hatchet_sdk.client import Client
from hatchet_sdk.clients.rest.models.cron_workflows import CronWorkflows
from hatchet_sdk.clients.rest.models.cron_workflows_list import CronWorkflowsList
from hatchet_sdk.clients.rest.models.cron_workflows_order_by_field import (
CronWorkflowsOrderByField,
)
from hatchet_sdk.clients.rest.models.workflow_run_order_by_direction import (
WorkflowRunOrderByDirection,
)
class CreateCronTriggerInput(BaseModel):
"""
Schema for creating a workflow run triggered by a cron.
Attributes:
expression (str): The cron expression defining the schedule.
input (dict): The input data for the cron workflow.
additional_metadata (dict[str, str]): Additional metadata associated with the cron trigger (e.g. {"key1": "value1", "key2": "value2"}).
"""
expression: str = None
input: dict = {}
additional_metadata: dict[str, str] = {}
@field_validator("expression")
def validate_cron_expression(cls, v):
"""
Validates the cron expression to ensure it adheres to the expected format.
Args:
v (str): The cron expression to validate.
Raises:
ValueError: If the expression is invalid.
Returns:
str: The validated cron expression.
"""
if not v:
raise ValueError("Cron expression is required")
parts = v.split()
if len(parts) != 5:
raise ValueError(
"Cron expression must have 5 parts: minute hour day month weekday"
)
for part in parts:
if not (
part == "*"
or part.replace("*/", "").replace("-", "").replace(",", "").isdigit()
):
raise ValueError(f"Invalid cron expression part: {part}")
return v
class CronClient:
"""
Client for managing workflow cron triggers synchronously.
Attributes:
_client (Client): The underlying client used to interact with the REST API.
aio (CronClientAsync): Asynchronous counterpart of CronClient.
"""
_client: Client
def __init__(self, _client: Client):
"""
Initializes the CronClient with a given Client instance.
Args:
_client (Client): The client instance to be used for REST interactions.
"""
self._client = _client
self.aio = CronClientAsync(_client)
def create(
self,
workflow_name: str,
cron_name: str,
expression: str,
input: dict,
additional_metadata: dict[str, str],
) -> CronWorkflows:
"""
Creates a new workflow cron trigger.
Args:
workflow_name (str): The name of the workflow to trigger.
cron_name (str): The name of the cron trigger.
expression (str): The cron expression defining the schedule.
input (dict): The input data for the cron workflow.
additional_metadata (dict[str, str]): Additional metadata associated with the cron trigger (e.g. {"key1": "value1", "key2": "value2"}).
Returns:
CronWorkflows: The created cron workflow instance.
"""
validated_input = CreateCronTriggerInput(
expression=expression, input=input, additional_metadata=additional_metadata
)
return self._client.rest.cron_create(
workflow_name,
cron_name,
validated_input.expression,
validated_input.input,
validated_input.additional_metadata,
)
def delete(self, cron_trigger: Union[str, CronWorkflows]) -> None:
"""
Deletes a workflow cron trigger.
Args:
cron_trigger (Union[str, CronWorkflows]): The cron trigger ID or CronWorkflows instance to delete.
"""
id_ = cron_trigger
if isinstance(cron_trigger, CronWorkflows):
id_ = cron_trigger.metadata.id
self._client.rest.cron_delete(id_)
def list(
self,
offset: int | None = None,
limit: int | None = None,
workflow_id: str | None = None,
additional_metadata: list[str] | None = None,
order_by_field: CronWorkflowsOrderByField | None = None,
order_by_direction: WorkflowRunOrderByDirection | None = None,
) -> CronWorkflowsList:
"""
Retrieves a list of all workflow cron triggers matching the criteria.
Args:
offset (int | None): The offset to start the list from.
limit (int | None): The maximum number of items to return.
workflow_id (str | None): The ID of the workflow to filter by.
additional_metadata (list[str] | None): Filter by additional metadata keys (e.g. ["key1:value1", "key2:value2"]).
order_by_field (CronWorkflowsOrderByField | None): The field to order the list by.
order_by_direction (WorkflowRunOrderByDirection | None): The direction to order the list by.
Returns:
CronWorkflowsList: A list of cron workflows.
"""
return self._client.rest.cron_list(
offset=offset,
limit=limit,
workflow_id=workflow_id,
additional_metadata=additional_metadata,
order_by_field=order_by_field,
order_by_direction=order_by_direction,
)
def get(self, cron_trigger: Union[str, CronWorkflows]) -> CronWorkflows:
"""
Retrieves a specific workflow cron trigger by ID.
Args:
cron_trigger (Union[str, CronWorkflows]): The cron trigger ID or CronWorkflows instance to retrieve.
Returns:
CronWorkflows: The requested cron workflow instance.
"""
id_ = cron_trigger
if isinstance(cron_trigger, CronWorkflows):
id_ = cron_trigger.metadata.id
return self._client.rest.cron_get(id_)
class CronClientAsync:
"""
Asynchronous client for managing workflow cron triggers.
Attributes:
_client (Client): The underlying client used to interact with the REST API asynchronously.
"""
_client: Client
def __init__(self, _client: Client):
"""
Initializes the CronClientAsync with a given Client instance.
Args:
_client (Client): The client instance to be used for asynchronous REST interactions.
"""
self._client = _client
async def create(
self,
workflow_name: str,
cron_name: str,
expression: str,
input: dict,
additional_metadata: dict[str, str],
) -> CronWorkflows:
"""
Asynchronously creates a new workflow cron trigger.
Args:
workflow_name (str): The name of the workflow to trigger.
cron_name (str): The name of the cron trigger.
expression (str): The cron expression defining the schedule.
input (dict): The input data for the cron workflow.
additional_metadata (dict[str, str]): Additional metadata associated with the cron trigger (e.g. {"key1": "value1", "key2": "value2"}).
Returns:
CronWorkflows: The created cron workflow instance.
"""
validated_input = CreateCronTriggerInput(
expression=expression, input=input, additional_metadata=additional_metadata
)
return await self._client.rest.aio.cron_create(
workflow_name=workflow_name,
cron_name=cron_name,
expression=validated_input.expression,
input=validated_input.input,
additional_metadata=validated_input.additional_metadata,
)
async def delete(self, cron_trigger: Union[str, CronWorkflows]) -> None:
"""
Asynchronously deletes a workflow cron trigger.
Args:
cron_trigger (Union[str, CronWorkflows]): The cron trigger ID or CronWorkflows instance to delete.
"""
id_ = cron_trigger
if isinstance(cron_trigger, CronWorkflows):
id_ = cron_trigger.metadata.id
await self._client.rest.aio.cron_delete(id_)
async def list(
self,
offset: int | None = None,
limit: int | None = None,
workflow_id: str | None = None,
additional_metadata: list[str] | None = None,
order_by_field: CronWorkflowsOrderByField | None = None,
order_by_direction: WorkflowRunOrderByDirection | None = None,
) -> CronWorkflowsList:
"""
Asynchronously retrieves a list of all workflow cron triggers matching the criteria.
Args:
offset (int | None): The offset to start the list from.
limit (int | None): The maximum number of items to return.
workflow_id (str | None): The ID of the workflow to filter by.
additional_metadata (list[str] | None): Filter by additional metadata keys (e.g. ["key1:value1", "key2:value2"]).
order_by_field (CronWorkflowsOrderByField | None): The field to order the list by.
order_by_direction (WorkflowRunOrderByDirection | None): The direction to order the list by.
Returns:
CronWorkflowsList: A list of cron workflows.
"""
return await self._client.rest.aio.cron_list(
offset=offset,
limit=limit,
workflow_id=workflow_id,
additional_metadata=additional_metadata,
order_by_field=order_by_field,
order_by_direction=order_by_direction,
)
async def get(self, cron_trigger: Union[str, CronWorkflows]) -> CronWorkflows:
"""
Asynchronously retrieves a specific workflow cron trigger by ID.
Args:
cron_trigger (Union[str, CronWorkflows]): The cron trigger ID or CronWorkflows instance to retrieve.
Returns:
CronWorkflows: The requested cron workflow instance.
"""
id_ = cron_trigger
if isinstance(cron_trigger, CronWorkflows):
id_ = cron_trigger.metadata.id
return await self._client.rest.aio.cron_get(id_)
|