feat: add cron-triggered signing reminder email job and update job definitions

This commit is contained in:
Ephraim Atta-Duncan
2025-04-15 07:12:29 +00:00
parent 651f5bbb6d
commit 5840796945
16 changed files with 289 additions and 24 deletions

View File

@ -26,16 +26,26 @@ export type TriggerJobOptions<Definitions extends ReadonlyArray<JobDefinition> =
};
}[number];
export type CronTrigger<N extends string = string> = {
type: 'cron';
schedule: string;
name: N;
};
export type EventTrigger<N extends string = string> = {
type: 'event';
name: N;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type JobDefinition<Name extends string = string, Schema = any> = {
id: string;
name: string;
version: string;
enabled?: boolean;
trigger: {
name: Name;
schema?: z.ZodType<Schema>;
};
trigger:
| (EventTrigger<Name> & { schema?: z.ZodType<Schema> })
| (CronTrigger<Name> & { schema?: z.ZodType<Schema> });
handler: (options: { payload: Schema; io: JobRunIO }) => Promise<Json | void>;
};

View File

@ -35,29 +35,53 @@ export class InngestJobProvider extends BaseJobProvider {
}
public defineJob<N extends string, T>(job: JobDefinition<N, T>): void {
console.log('defining job', job.id);
const fn = this._client.createFunction(
{
id: job.id,
name: job.name,
},
{
event: job.trigger.name,
},
async (ctx) => {
const io = this.convertInngestIoToJobRunIo(ctx);
let fn: InngestFunction.Any;
// We need to cast to any so we can deal with parsing later.
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
let payload = ctx.event.data as any;
if (job.trigger.type === 'cron') {
fn = this._client.createFunction(
{
id: job.id,
name: job.name,
},
{
cron: job.trigger.schedule,
},
async (ctx) => {
const io = this.convertInngestIoToJobRunIo(ctx);
const payload: T | undefined = undefined;
if (job.trigger.schema) {
payload = job.trigger.schema.parse(payload);
}
if (job.trigger.schema) {
console.warn(
`Job "${job.id}" is cron-triggered but defines a schema. The schema will be ignored. `,
);
}
await job.handler({ payload, io });
},
);
await job.handler({ payload: payload as T, io });
},
);
} else {
fn = this._client.createFunction(
{
id: job.id,
name: job.name,
},
{
event: job.trigger.name,
},
async (ctx) => {
const io = this.convertInngestIoToJobRunIo(ctx);
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
let payload = ctx.event.data as any;
if (job.trigger.schema) {
payload = job.trigger.schema.parse(payload);
}
await job.handler({ payload, io });
},
);
}
this._functions.push(fn);
}