JobQueueService

JobQueueService

JobQueueService

The JobQueueService is used to create new JobQueue instances and access existing jobs.

Example

// A service which transcodes video files
class VideoTranscoderService {

  private jobQueue: JobQueue<{ videoId: string; }>;

  onModuleInit() {
    // The JobQueue is created on initialization
    this.jobQueue = this.jobQueueService.createQueue({
      name: 'transcode-video',
      concurrency: 5,
      process: async job => {
        try {
          const result = await this.transcodeVideo(job.data.videoId);
          job.complete(result);
        } catch (e) {
          job.fail(e);
        }
      },
    });
  }

  addToTranscodeQueue(videoId: string) {
    this.jobQueue.add({ videoId, })
  }

  private transcodeVideo(videoId: string) {
    // e.g. call some external transcoding service
  }

}

Signature

class JobQueueService implements OnApplicationBootstrap, OnModuleDestroy {
  constructor(configService: ConfigService, processContext: ProcessContext)
  createQueue(options: CreateQueueOptions<Data>) => JobQueue<Data>;
  getJob(id: ID) => Promise<Job | undefined>;
  getJobs(options?: JobListOptions) => Promise<PaginatedList<Job>>;
  getJobsById(ids: ID[]) => Promise<Job[]>;
  getJobQueues() => GraphQlJobQueue[];
  removeSettledJobs(queueNames: string[], olderThan?: Date) => ;
}

Implements

  • OnApplicationBootstrap
  • OnModuleDestroy

Members

constructor

method
type:
(configService: ConfigService, processContext: ProcessContext) => JobQueueService

createQueue

method
type:
(options: CreateQueueOptions<Data>) => JobQueue<Data>
Configures and creates a new JobQueue instance.

getJob

method
type:
(id: ID) => Promise<Job | undefined>
Gets a job by id. The implementation is handled by the configured JobQueueStrategy.

getJobs

method
type:
(options?: JobListOptions) => Promise<PaginatedList<Job>>
Gets jobs according to the supplied options. The implementation is handled by the configured JobQueueStrategy.

getJobsById

method
type:
(ids: ID[]) => Promise<Job[]>
Gets jobs by ids. The implementation is handled by the configured JobQueueStrategy.

getJobQueues

method
type:
() => GraphQlJobQueue[]
Returns an array of { name: string; running: boolean; } for each registered JobQueue.

removeSettledJobs

method
type:
(queueNames: string[], olderThan?: Date) =>
Removes settled jobs (completed or failed). The implementation is handled by the configured JobQueueStrategy.