torotask
Version:
Task queue processing in NodeJS based on BullMQ and Redis
90 lines • 4.53 kB
TypeScript
import type { StringValue } from 'ms';
import type { Logger } from 'pino';
import type { ZodSchema } from 'zod';
import type { ToroTask } from '../client.js';
import type { TaskJob } from '../job.js';
import type { StepExecutor } from '../step-executor.js';
import type { TaskGroup } from '../task-group.js';
import type { Task } from '../task.js';
import type { TaskWorkerQueue } from '../worker-queue.js';
import type { TaskJobOptions } from './job.js';
import type { TaskQueueOptions } from './queue.js';
import type { EffectivePayloadType, ResolvedSchemaType, SchemaHandler } from './schema.js';
import type { Prettify, SingleOrArray } from './utils.js';
import type { TaskWorkerOptions } from './worker.js';
export type TaskTriggerCronValue = string;
export type TaskTriggerEveryValue = StringValue | number;
/**
* Options for defining a Task, extending BullMQ's JobsOptions.
* @template PayloadType - The payload type for typed deduplication callbacks
*/
export type TaskOptions<PayloadType = any> = Prettify<TaskJobOptions<any, any, PayloadType> & {
allowCatchAll?: boolean;
batch?: TaskWorkerOptions['batch'];
workerOptions?: Partial<Omit<TaskWorkerOptions, 'batch'>>;
queueOptions?: Partial<TaskQueueOptions>;
skipSchemaValidation?: boolean;
}>;
/** Handler details passed to the task handler */
export interface TaskHandlerOptions<PayloadType = unknown> {
id?: string;
name: string;
payload: PayloadType;
}
export interface BaseHandlerContext<PayloadType = unknown, ResultType = unknown> {
logger: Logger;
client: ToroTask;
group: TaskGroup;
queue: TaskWorkerQueue<PayloadType, ResultType>;
}
/** Context passed to the task handler */
export interface TaskHandlerContext<PayloadType = unknown, ResultType = unknown, _CurrentResolvedSchema extends ZodSchema | undefined = undefined> extends BaseHandlerContext<PayloadType, ResultType> {
task: Task<PayloadType, ResultType, any>;
job: TaskJob<PayloadType, ResultType>;
step: StepExecutor<TaskJob<PayloadType, ResultType>>;
token?: string | undefined;
}
/** Task handler function type */
export type TaskHandler<PayloadType = unknown, ResultType = unknown, CurrentResolvedSchema extends ZodSchema | undefined = undefined> = (options: TaskHandlerOptions<PayloadType>, context: TaskHandlerContext<PayloadType, ResultType, CurrentResolvedSchema>) => Promise<ResultType>;
export interface TaskTriggerBase<PayloadType = unknown> {
payload?: PayloadType;
}
export interface TaskTriggerEvent<PayloadType = unknown> extends TaskTriggerBase<PayloadType> {
type: 'event';
event?: string;
if?: string;
}
export interface TaskTriggerCron<PayloadType = unknown> extends TaskTriggerBase<PayloadType> {
type: 'cron';
name?: string;
cron?: TaskTriggerCronValue;
}
export interface TaskTriggerEvery<PayloadType = unknown> extends TaskTriggerBase<PayloadType> {
type: 'every';
name?: string;
every?: TaskTriggerEveryValue;
}
export type TaskTrigger<PayloadType = unknown> = TaskTriggerEvent<PayloadType> | TaskTriggerCron<PayloadType> | TaskTriggerEvery<PayloadType>;
export interface TaskConfig<PayloadExplicit = unknown, ResultType = unknown, SchemaInputValue extends SchemaHandler = undefined> {
handler: TaskHandler<EffectivePayloadType<PayloadExplicit, ResolvedSchemaType<SchemaInputValue>>, ResultType, ResolvedSchemaType<SchemaInputValue>>;
options?: TaskOptions<EffectivePayloadType<PayloadExplicit, ResolvedSchemaType<SchemaInputValue>>>;
schema?: SchemaInputValue;
triggers?: SingleOrArray<TaskTrigger<EffectivePayloadType<PayloadExplicit, ResolvedSchemaType<SchemaInputValue>>>>;
}
export interface TaskDefinition<PayloadExplicit = unknown, ResultType = unknown, SchemaInputValue extends SchemaHandler = undefined> extends TaskConfig<PayloadExplicit, ResultType, SchemaInputValue> {
}
export interface TaskDefinitionRegistry {
[taskName: string]: TaskDefinition<any, any, any>;
}
export type TaskRegistry<TDefs extends TaskDefinitionRegistry> = {
[K in keyof TDefs]: TDefs[K] extends TaskDefinition<infer P, infer R, infer S> ? Task<P, R, S> : never;
};
/**
* Extract payload type from a TaskDefinition
*/
export type TaskPayloadFromDef<TDef> = TDef extends TaskDefinition<infer P, any, infer S> ? EffectivePayloadType<P, ResolvedSchemaType<S>> : unknown;
/**
* Extract result type from a TaskDefinition
*/
export type TaskResultFromDef<TDef> = TDef extends TaskDefinition<any, infer R, any> ? R : unknown;
//# sourceMappingURL=task.d.ts.map