Core Concepts
To make the most out of JoobQ, it's essential to understand its building blocks and how they work together to efficiently process tasks. This section introduces the core concepts that power JoobQ and some additional advanced features to unlock its full potential.
Jobs
Jobs are the fundamental units of work that JoobQ processes asynchronously. A job could involve tasks like sending emails, processing images, or making API requests.
Defining a Job
In JoobQ, jobs are defined as classes that include the JoobQ::Job
module. Each job implements a perform
method where the task's logic resides. Jobs can also include parameters to customize their behavior.
Key Attributes of a Job
Job ID: A unique identifier assigned to each job.
Queue: The queue where the job is placed.
Retries: Number of retry attempts allowed for failed jobs.
Status: Possible statuses include
Enqueued
,Running
,Failed
, andCompleted
.
Queues
A queue organizes jobs awaiting processing. Queues can group jobs by categories or priorities, making it easier to manage and control job processing. For example, you can use separate queues for "emails" and "image_processing."
Queue Prioritization
JoobQ allows prioritization by assigning workers to specific queues. Time-sensitive tasks can be assigned to high-priority queues, ensuring they are processed promptly.
Defining a Queue
Queues are defined in the configuration:
This example defines a queue named email_queue
with 5 workers and throttling limits of 10 jobs per minute.
Workers
Workers are processes responsible for executing jobs. They continuously listen to queues and execute jobs as they become available. JoobQ supports multiple workers for parallel job processing, allowing you to scale your system as needed.
Worker Lifecycle
Start: Workers connect to queues and wait for jobs.
Execute: A job's
perform
method is called.Retry or Complete: Jobs that fail are retried based on the retry policy. Successful jobs are marked as
Completed
.
Scheduling Jobs
JoobQ allows scheduling jobs to run at a specific time in the future or at regular intervals. Scheduling can be done using delays or cron-based recurring schedules.
Delayed Jobs
Delay job execution until a specified time:
This enqueues the job to run 10 minutes later.
Recurring Jobs
Schedule recurring jobs for tasks like daily reports or regular cleanups:
The above example schedules the job to run daily at 9:00 AM, using cron syntax.
Middlewares
Middlewares provide a mechanism to customize and enhance the job processing pipeline. They allow you to intercept job execution to add cross-cutting concerns like logging, retries, timeouts, and throttling.
Using Middlewares
Middlewares are globally configured through the use
method:
Key Built-in Middlewares
RetryMiddleware: Retries failed jobs.
TimeoutMiddleware: Enforces execution time limits.
ThrottleMiddleware: Limits the rate of job execution.
AsyncLoggingMiddleware: Logs job details asynchronously.
Schedulers
Schedulers manage the timing of job execution. JoobQ supports several types of schedulers:
Delayed Jobs: Schedule jobs to run at a specific future time.
Cron Scheduler: Use cron syntax to define recurring jobs.
Recurring Scheduler: Schedule jobs at fixed intervals.
Defining a Scheduler
Schedulers are configured like so:
Advanced Concepts
Dead Letter Queues: Store jobs that fail after exhausting all retries for post-mortem analysis.
Metrics and Monitoring: JoobQ provides built-in support to track queue performance, job execution times, and failure rates.
Custom Middlewares: Create your own middlewares to add custom logic like notifications or analytics.
Summary
JoobQ’s powerful concepts—Jobs, Queues, Workers, Middlewares, Schedulers—allow you to build efficient, scalable, and maintainable background processing systems. Ready to dive deeper? Explore the Configuration section to tailor JoobQ to your application’s needs!
Last updated