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