import { z } from 'zod'; import type { Json } from './json'; export type SimpleTriggerJobOptions = { id?: string; name: string; payload: unknown; timestamp?: number; }; export const ZSimpleTriggerJobOptionsSchema = z.object({ id: z.string().optional(), name: z.string(), payload: z.unknown().refine((x) => x !== undefined, { message: 'payload is required' }), timestamp: z.number().optional(), }); // Map the array to create a union of objects we may accept export type TriggerJobOptions = []> = { [K in keyof Definitions]: { id?: string; name: Definitions[K]['trigger']['name']; payload: Definitions[K]['trigger']['schema'] extends z.ZodType ? Shape : unknown; timestamp?: number; }; }[number]; // eslint-disable-next-line @typescript-eslint/no-explicit-any export type JobDefinition = { id: string; name: string; version: string; enabled?: boolean; optimizeParallelism?: boolean; trigger: { name: Name; schema?: z.ZodType; /** * Optional cron expression (e.g., "* * * * *" for every minute). * When set, the job runs on a schedule instead of being event-triggered. */ cron?: string; }; handler: (options: { payload: Schema; io: JobRunIO }) => Promise; }; export interface JobRunIO { // stableRun(cacheKey: string, callback: (io: JobRunIO) => T | Promise): Promise; runTask(cacheKey: string, callback: () => Promise): Promise; triggerJob(cacheKey: string, options: SimpleTriggerJobOptions): Promise; wait(cacheKey: string, ms: number): Promise; logger: { info(...args: unknown[]): void; error(...args: unknown[]): void; debug(...args: unknown[]): void; warn(...args: unknown[]): void; log(...args: unknown[]): void; }; } export const defineJob = (job: JobDefinition): JobDefinition => job;