Skip to content

Commit

Permalink
feat(api): add job route
Browse files Browse the repository at this point in the history
  • Loading branch information
Diluka committed May 17, 2024
1 parent 876dfb1 commit d10fae6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/api/src/handlers/addJob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { BaseAdapter } from '../queueAdapters/base';
import { BullBoardRequest, ControllerHandlerReturnType } from '../../typings/app';
import { queueProvider } from '../providers/queue';
import { formatJob } from './queues';

async function addJob(
req: BullBoardRequest,
queue: BaseAdapter
): Promise<ControllerHandlerReturnType> {
const { name, data, options } = req.body;

const job = await queue.addJob(name, data, options);

return {
status: 200,
body: {
job: formatJob(job, queue),
status: job.getState(),
},
};
}

export const addJobHandler = queueProvider(addJob);
3 changes: 3 additions & 0 deletions packages/api/src/queueAdapters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
JobStatus,
QueueAdapterOptions,
QueueJob,
QueueJobOptions,
Status,
} from '../../typings/app';

Expand Down Expand Up @@ -45,6 +46,8 @@ export abstract class BaseAdapter {

public abstract clean(queueStatus: JobCleanStatus, graceTimeMs: number): Promise<void>;

public abstract addJob(name: string, data: any, options: QueueJobOptions): Promise<QueueJob>;

public abstract getJob(id: string): Promise<QueueJob | undefined | null>;

public abstract getJobCounts(): Promise<JobCounts>;
Expand Down
5 changes: 5 additions & 0 deletions packages/api/src/queueAdapters/bull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
JobCounts,
JobStatus,
QueueAdapterOptions,
QueueJobOptions,
Status,
} from '../../typings/app';
import { STATUSES } from '../constants/statuses';
Expand All @@ -26,6 +27,10 @@ export class BullAdapter extends BaseAdapter {
return this.queue.clean(graceTimeMs, jobStatus as any);
}

public addJob(name: string, data: any, options: QueueJobOptions) {
return this.queue.add(name, data, options);
}

public getJob(id: string): Promise<Job | undefined | null> {
return this.queue.getJob(id).then((job) => job && this.alignJobData(job));
}
Expand Down
5 changes: 5 additions & 0 deletions packages/api/src/queueAdapters/bullMQ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
JobCounts,
JobStatus,
QueueAdapterOptions,
QueueJobOptions,
Status,
} from '../../typings/app';
import { STATUSES } from '../constants/statuses';
Expand All @@ -27,6 +28,10 @@ export class BullMQAdapter extends BaseAdapter {
await this.queue.clean(graceTimeMs, 1000, jobStatus);
}

public addJob(name: string, data: any, options: QueueJobOptions) {
return this.queue.add(name, data, options);
}

public getJob(id: string): Promise<Job | undefined> {
return this.queue.getJob(id);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AppRouteDefs } from '../typings/app';
import { addJobHandler } from './handlers/addJob';
import { cleanAllHandler } from './handlers/cleanAll';
import { cleanJobHandler } from './handlers/cleanJob';
import { emptyQueueHandler } from './handlers/emptyQueue';
Expand Down Expand Up @@ -33,6 +34,11 @@ export const appRoutes: AppRouteDefs = {
route: '/api/queues/:queueName/:jobId',
handler: jobHandler,
},
{
method: 'post',
route: '/api/queues/:queueName/add',
handler: addJobHandler,
},
{
method: 'put',
route: '/api/queues/:queueName/retry/:queueStatus',
Expand Down
6 changes: 6 additions & 0 deletions packages/api/typings/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export interface QueueJobJson {
parentKey?: string;
}

export interface QueueJobOptions {
delay?: number;
attempts?: number;
}

export interface RedisStats {
version: string;
mode: RedisInfo['redis_mode'];
Expand Down Expand Up @@ -127,6 +132,7 @@ export interface BullBoardRequest {
queues: BullBoardQueues;
query: Record<string, any>;
params: Record<string, any>;
body: Record<string, any>;
}

export type ControllerHandlerReturnType = {
Expand Down

0 comments on commit d10fae6

Please sign in to comment.