Skip to main content
Jobs in Orionjs enable you to execute background tasks, schedule recurring operations, and handle asynchronous processing. Powered by the @orion-js/dogs package, jobs provide a robust way to manage both recurring and event-driven background operations.

Installation

Defining Jobs Controllers

The recommended way to define jobs in Orionjs is to use the @Jobs() decorator on a class and the job-specific decorators on methods:
With this approach:
  • The class is decorated with @Jobs() to mark it as a job controller
  • Each job is defined as a class property using createRecurrentJob or createEventJob with its specific decorator (@RecurrentJob or @EventJob)
  • Job parameters are validated based on the schema provided in the params option
  • You can use dependency injection with @Inject(() => Service)
  • The class can also include regular methods that aren’t jobs

Types of Jobs

Orionjs supports two types of jobs:

1. Recurrent Jobs

Jobs that run at specified intervals or on a schedule:

2. Event Jobs

Jobs that run on-demand when triggered by an event:

Job Creation Options

RecurrentJob Options

EventJob Options

Starting Workers

In your application setup (typically in your main file), you start workers to process jobs:
Each startWorkers() call creates one scheduler loop, regardless of workersCount. The scheduler claims jobs atomically until its concurrency is full. It queries one partition at a time, advances through a shuffled order, and waits pollInterval only after a complete empty sweep. This avoids a $in across partitions and keeps each acquisition on an equality-prefixed index. Priority is strict inside each partition and approximate across partitions. Use the same nPartitions value in every application instance. Dogs stores no worker registry or partition metadata. A background reconciler gradually assigns legacy records and repairs partitions outside the configured range without moving actively locked jobs. workersInstance.runningExecutions reports how many active executions currently consume scheduler capacity. maxTriesReachedRetentionMs controls how long terminal event-job records remain in MongoDB. It defaults to one week; set it to null to retain them indefinitely. Dogs stores an expiresAt date, ensures a TTL index for that field, and applies the configured retention to existing maxTriesReached records whenever workers start. Use maxParallelExecutionsPerServer when a job is safe to queue but should not run too many times concurrently on the same server, for example:
This limit only applies to event jobs. Recurrent jobs already execute as a single job record, so they do not need this option.

Scheduling Event Jobs

Trigger an event job to run:

Job Execution Context

The job’s resolve function receives additional parameters:

Error Handling

Jobs support sophisticated error handling:

Monitoring Jobs

You can monitor job status and history:

Best Practices

  1. Keep Jobs Idempotent: Design jobs so they can be safely re-executed without side effects.
  2. Use Parameters Validation: Define schemas for job parameters to ensure they’re valid.
  3. Handle Errors Properly: Implement robust error handling with appropriate retry strategies.
  4. Set Realistic Lock Times: Ensure the lockTime is longer than a job’s expected execution time.
  5. Monitor Job Performance: Keep track of job durations to optimize processing.
  6. Organize by Domain: Group related jobs in domain-specific controller classes.
  7. Test Jobs Thoroughly: Write unit tests for job logic to ensure reliability.
  8. Check Job Status: Implement health checks to monitor job processing.