Class Job<Input>Abstract

The Job class provides a structure for defining and processing background jobs with automatic validation.

Each subclass must define a data class, which extends Data<ObjectParser>, to represent the input structure.

The Job class will automatically instantiate the data class during validation using the provided ObjectParser.

Subclasses only need to define the data attribute and implement the perform method to process the job.

class MyData extends Data<ObjectParser> {
get userId(): number {
return this.parser.getNumber("userId");
}
}

class MyJob extends Job<MyData> {
protected readonly data = MyData;

async perform(input: MyData): Promise<void> {
console.log(`Processing job for user ID: ${input.userId}`);
}
}

// Enqueue a job with the provided data.
MyJob.enqueue({ userId: 123 });

Type Parameters

  • Input extends Data

    The type of data the job will process, which must extend Data<ObjectParser>.

Constructors

Properties

Methods

Constructors

  • Type Parameters

    • Input extends Data<Parser<unknown>>

    Returns Job<Input>

Properties

data: new (parser: ObjectParser) => Input

The Data class for this job, which is used for validation. Must be defined by subclasses.

Type declaration

    • new (parser: ObjectParser): Input
    • The Data class for this job, which is used for validation. Must be defined by subclasses.

      Parameters

      • parser: ObjectParser

      Returns Input

Methods

  • Abstract method that defines the job's logic after the data has been validated.

    Subclasses must implement this method to define the actions taken with the validated input.

    Parameters

    • input: Input

      The validated input data.

    Returns Promise<void>

    A promise that resolves once the job processing is complete.

  • Validates the incoming data using the data class defined in the subclass.

    This method automatically creates an instance of the data class using the provided ObjectParser.

    Parameters

    • body: ObjectParser

      The ObjectParser containing the incoming data.

    Returns Promise<Input>

    A promise that resolves to the validated Input data.

  • Enqueues a job with the provided message, adding it to the job queue for future processing.

    This static method allows jobs to be scheduled by adding the job name and the message to the queue.

    Type Parameters

    • T extends JsonObject
    • J

    Parameters

    • this: new () => J
    • message: T

      An object containing the job data to be enqueued.

    Returns void

    MyJob.enqueue({ userId: 123, action: 'process' });