@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:
- The class is decorated with
@Jobs()to mark it as a job controller - Each job is defined as a class property using
createRecurrentJoborcreateEventJobwith its specific decorator (@RecurrentJobor@EventJob) - Job parameters are validated based on the schema provided in the
paramsoption - 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: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:
Scheduling Event Jobs
Trigger an event job to run:Job Execution Context
The job’sresolve function receives additional parameters:
Error Handling
Jobs support sophisticated error handling:Monitoring Jobs
You can monitor job status and history:Best Practices
- Keep Jobs Idempotent: Design jobs so they can be safely re-executed without side effects.
- Use Parameters Validation: Define schemas for job parameters to ensure they’re valid.
- Handle Errors Properly: Implement robust error handling with appropriate retry strategies.
- Set Realistic Lock Times: Ensure the lockTime is longer than a job’s expected execution time.
- Monitor Job Performance: Keep track of job durations to optimize processing.
- Organize by Domain: Group related jobs in domain-specific controller classes.
- Test Jobs Thoroughly: Write unit tests for job logic to ensure reliability.
- Check Job Status: Implement health checks to monitor job processing.