Files
sign/packages/lib/jobs/client/client.ts

33 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-06-17 16:59:14 +10:00
import { match } from 'ts-pattern';
2024-05-15 18:55:05 +10:00
import type { JobDefinition, TriggerJobOptions } from './_internal/job';
import type { BaseJobProvider as JobClientProvider } from './base';
2024-06-17 16:59:14 +10:00
import { InngestJobProvider } from './inngest';
2024-05-15 18:55:05 +10:00
import { LocalJobProvider } from './local';
import { TriggerJobProvider } from './trigger';
2024-05-22 21:57:05 +10:00
export class JobClient<T extends Array<JobDefinition> = []> {
2024-05-15 18:55:05 +10:00
private static _instance: JobClient;
private _provider: JobClientProvider;
2024-05-22 21:57:05 +10:00
public constructor(definitions: T) {
2024-06-17 16:59:14 +10:00
this._provider = match(process.env.NEXT_PRIVATE_JOBS_PROVIDER)
.with('inngest', () => InngestJobProvider.getInstance())
.with('trigger', () => TriggerJobProvider.getInstance())
.otherwise(() => LocalJobProvider.getInstance());
2024-05-22 21:57:05 +10:00
definitions.forEach((definition) => {
this._provider.defineJob(definition);
});
2024-05-15 18:55:05 +10:00
}
2024-05-22 21:57:05 +10:00
public async triggerJob(options: TriggerJobOptions<T>) {
2024-05-15 18:55:05 +10:00
return this._provider.triggerJob(options);
}
public getApiHandler() {
return this._provider.getApiHandler();
}
}