Skip to content

Commit c6df574

Browse files
committed
Fixed io.cancelEvent (shouldn’t be a noop)
1 parent b9dc7ce commit c6df574

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

examples/job-catalog/src/events.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,26 @@ client.defineJob({
2929
},
3030
});
3131

32+
client.defineJob({
33+
id: "cancel-event-example",
34+
name: "Cancel Event Example",
35+
version: "1.0.0",
36+
trigger: eventTrigger({
37+
name: "cancel.event.example",
38+
}),
39+
run: async (payload, io, ctx) => {
40+
await io.sendEvent(
41+
"send-event",
42+
{ name: "Cancellable Event", id: payload.id },
43+
{
44+
deliverAt: new Date(Date.now() + 1000 * 60 * 60 * 24), // 24 hours from now
45+
}
46+
);
47+
48+
await io.wait("wait-1", 60); // 1 minute
49+
50+
await io.cancelEvent("cancel-event", payload.id);
51+
},
52+
});
53+
3254
createExpressServer(client);

packages/trigger-sdk/src/io.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ export class IO {
221221
);
222222
}
223223

224+
/** `io.cancelEvent()` allows you to cancel an event that was previously sent with `io.sendEvent()`. This will prevent any Jobs from running that are listening for that event if the event was sent with a delay
225+
* @param key
226+
* @param eventId
227+
* @returns
228+
*/
224229
async cancelEvent(key: string | any[], eventId: string) {
225230
return await this.runTask(
226231
key,
@@ -235,15 +240,13 @@ export class IO {
235240
text: eventId,
236241
},
237242
],
238-
noop: true,
239243
},
240244
async (task) => {
241245
return await this._triggerClient.cancelEvent(eventId);
242246
}
243247
);
244248
}
245249

246-
247250
async updateSource(key: string | any[], options: { key: string } & UpdateTriggerSourceBody) {
248251
return this.runTask(
249252
key,
@@ -455,7 +458,10 @@ export class IO {
455458
= * @param onError The callback that will be called when the Task fails. The callback receives the error, the Task and the IO as parameters. If you wish to retry then return an object with a `retryAt` property.
456459
* @returns A Promise that resolves with the returned value of the callback.
457460
*/
458-
async runTask<TResult extends SerializableJson | void = void, TCallbackResult extends unknown = TResult>(
461+
async runTask<
462+
TResult extends SerializableJson | void = void,
463+
TCallbackResult extends unknown = TResult,
464+
>(
459465
key: string | any[],
460466
options: RunTaskOptions,
461467
callback: (task: IOTask, io: IO) => Promise<TCallbackResult | TResult>,
@@ -548,7 +554,7 @@ export class IO {
548554
const result = await callback(task, this);
549555

550556
const output = SerializableJsonSchema.parse(result) as TResult;
551-
557+
552558
this._logger.debug("Completing using output", {
553559
idempotencyKey,
554560
task,

0 commit comments

Comments
 (0)