mirror of
https://github.com/documenso/documenso.git
synced 2025-11-18 02:32:00 +10:00
wip: background tasks
This commit is contained in:
41
packages/lib/jobs/client/_internal/job.ts
Normal file
41
packages/lib/jobs/client/_internal/job.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ZTriggerJobOptionsSchema = z.object({
|
||||
id: z.string().optional(),
|
||||
name: z.string(),
|
||||
payload: z.unknown().refine((x) => x !== undefined, { message: 'payload is required' }),
|
||||
timestamp: z.number().optional(),
|
||||
});
|
||||
|
||||
// The Omit is a temporary workaround for a "bug" in the zod library
|
||||
// @see: https://github.com/colinhacks/zod/issues/2966
|
||||
export type TriggerJobOptions = Omit<z.infer<typeof ZTriggerJobOptionsSchema>, 'payload'> & {
|
||||
payload: unknown;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export type JobDefinition<T = any> = {
|
||||
id: string;
|
||||
name: string;
|
||||
version: string;
|
||||
enabled?: boolean;
|
||||
trigger: {
|
||||
name: string;
|
||||
schema?: z.ZodSchema<T>;
|
||||
};
|
||||
handler: (options: { payload: T; io: JobRunIO }) => Promise<Json | void>;
|
||||
};
|
||||
|
||||
export interface JobRunIO {
|
||||
// stableRun<T extends Json | void>(cacheKey: string, callback: (io: JobRunIO) => T | Promise<T>): Promise<T>;
|
||||
stableRun<T extends Json | void>(cacheKey: string, callback: () => Promise<T>): Promise<T>;
|
||||
triggerJob(cacheKey: string, options: TriggerJobOptions): Promise<unknown>;
|
||||
wait(cacheKey: string, ms: number): Promise<void>;
|
||||
logger: {
|
||||
info(...args: unknown[]): void;
|
||||
error(...args: unknown[]): void;
|
||||
debug(...args: unknown[]): void;
|
||||
warn(...args: unknown[]): void;
|
||||
log(...args: unknown[]): void;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user