@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: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.