You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Adds support for `emitDecoratorMetadata: true` and `experimentalDecorators: true` in your tsconfig
* Implement task.onSuccess/onFailure and config.onSuccess/onFailure
* Added onStart and more docs for lifecycle functions
* Use onStart instead of init for TypeORM
Adds support for `emitDecoratorMetadata: true` and `experimentalDecorators: true` in your tsconfig using the [`@anatine/esbuild-decorators`](https://github.com/anatine/esbuildnx/tree/main/packages/esbuild-decorators) package. This allows you to use libraries like TypeORM:
You can also return data from the `init` function that will be available in the params of the `run`, `cleanup`, `onSuccess`, and `onFailure` functions.
142
+
143
+
```ts /trigger/init-return.ts
144
+
exportconst taskWithInitReturn =task({
145
+
id: "task-with-init-return",
146
+
init: async (payload, { ctx }) => {
147
+
return { someData: "someValue" };
148
+
},
149
+
run: async (payload:any, { ctx, init }) => {
150
+
console.log(init.someData); // "someValue"
151
+
},
152
+
});
153
+
```
128
154
129
155
### `cleanup` function
130
156
131
-
This function is called after a run attempt has succeeded or failed.
157
+
This function is called after the `run` function is executed, regardless of whether the run was successful or not. It's useful for cleaning up resources, logging, or other side effects.
158
+
159
+
```ts /trigger/cleanup.ts
160
+
exportconst taskWithCleanup =task({
161
+
id: "task-with-cleanup",
162
+
cleanup: async (payload, { ctx }) => {
163
+
//...
164
+
},
165
+
run: async (payload:any, { ctx }) => {
166
+
//...
167
+
},
168
+
});
169
+
```
132
170
133
171
### `middleware` function
134
172
135
173
This function is called before the `run` function, it allows you to wrap the run function with custom code. For more information [read the guide](/v3/middleware).
136
174
175
+
### `onStart` function
176
+
177
+
When a task run starts, the `onStart` function is called. It's useful for sending notifications, logging, and other side effects. This function will only be called one per run (not per retry). If you want to run code before each retry, use the `init` function.
178
+
179
+
```ts /trigger/on-start.ts
180
+
exportconst taskWithOnStart =task({
181
+
id: "task-with-on-start",
182
+
onStart: async (payload, { ctx }) => {
183
+
//...
184
+
},
185
+
run: async (payload:any, { ctx }) => {
186
+
//...
187
+
},
188
+
});
189
+
```
190
+
191
+
You can also define an `onStart` function in your `trigger.config.ts` file to get notified when any task starts.
When a task attempt succeeds, the `onSuccess` function is called. It's useful for sending notifications, logging, or other side effects.
205
+
When a task run succeeds, the `onSuccess` function is called. It's useful for sending notifications, logging, syncing state to your database, or other side effects.
140
206
141
-
<Snippetfile="coming-soon-slim.mdx" />
207
+
```ts /trigger/on-success.ts
208
+
exportconst taskWithOnSuccess =task({
209
+
id: "task-with-on-success",
210
+
onSuccess: async (payload, output, { ctx }) => {
211
+
//...
212
+
},
213
+
run: async (payload:any, { ctx }) => {
214
+
//...
215
+
},
216
+
});
217
+
```
218
+
219
+
You can also define an `onSuccess` function in your `trigger.config.ts` file to get notified when any task succeeds.
When a task run fails, the `onFailure` function is called. It's useful for sending notifications, logging, or other side effects. It will only be executed once the task run has exhausted all its retries.
234
+
235
+
```ts /trigger/on-failure.ts
236
+
exportconst taskWithOnFailure =task({
237
+
id: "task-with-on-failure",
238
+
onFailure: async (payload, error, { ctx }) => {
239
+
//...
240
+
},
241
+
run: async (payload:any, { ctx }) => {
242
+
//...
243
+
},
244
+
});
245
+
```
246
+
247
+
You can also define an `onFailure` function in your `trigger.config.ts` file to get notified when any task fails.
When a task attempt fails, the `onError` function is called. It's useful for sending notifications, logging, or other side effects.
261
+
You can define a function that will be called when an error is thrown in the `run` function, that allows you to control how the error is handled and whether the task should be retried.
146
262
147
-
<Snippetfile="coming-soon-slim.mdx" />
263
+
Read more about `handleError` in our [Errors and Retrying guide](/v3/errors-retrying).
0 commit comments