|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +This guide gives you an overview of the `async-job` gem and explains the core concepts. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add the gem to your project: |
| 8 | + |
| 9 | +``` shell |
| 10 | +$ bundle add async-job |
| 11 | +``` |
| 12 | + |
| 13 | +## Core Concepts |
| 14 | + |
| 15 | +`async-job` is a library for building asynchronous job queues. |
| 16 | + |
| 17 | +- Several included {ruby Async::Job::Queue} implementations for enqueueing and running jobs. |
| 18 | +- Several supported {ruby Async::Job::Coder} implementations for encoding and decoding job payloads. |
| 19 | +- A {ruby Async::Job::Generic} class which describes the minimum required job interface. |
| 20 | + |
| 21 | +The `async-job` library provides a framework for enqueueing and dequeuing jobs, without prescribing how jobs are executed. It is expected that you would use this library to wrap existing schemas for job definition and execution. This design allows you to use `async-job` in a wide variety of applications, without the need for cumbersome wrappers. |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +In general, a job processing system pipeline comprises two parts: a client that submits jobs into a queue, and a server that reads jobs out of a queue and processes them. |
| 26 | + |
| 27 | +```mermaid |
| 28 | +sequenceDiagram |
| 29 | + participant A as Client (Application) |
| 30 | + participant ME as Middleware (Enqueue) |
| 31 | + participant S as Server (Queue) |
| 32 | + participant MD as Middleware (Dequeue) |
| 33 | + participant H as Delegate |
| 34 | +
|
| 35 | + Note over A,S: Client |
| 36 | + A->>+ME: Submit Job |
| 37 | + ME->>+S: Enqueue Job |
| 38 | +
|
| 39 | + Note over S,H: Server |
| 40 | + S->>+MD: Dequeue Job |
| 41 | + MD->>+H: Execute Job |
| 42 | +``` |
| 43 | + |
| 44 | +You can use {ruby Async::Job::Builder} to create a pipeline that includes both the producer and consumer sides of a queue: |
| 45 | + |
| 46 | +```ruby |
| 47 | +require 'async' |
| 48 | +require 'async/job' |
| 49 | +require 'async/job/processor/inline' |
| 50 | + |
| 51 | +# This is how we execute a job from the queue: |
| 52 | +executor = proc do |job| |
| 53 | + puts "Processing job: #{job}" |
| 54 | +end |
| 55 | + |
| 56 | +# Create a simple inline pipeline: |
| 57 | +pipeline = Async::Job::Builder.build(executor) do |
| 58 | + # We are going to use an inline processor which processes the job in the background using Async{}: |
| 59 | + enqueue Async::Job::Processor::Inline |
| 60 | +end |
| 61 | + |
| 62 | +# Enqueue a job: |
| 63 | +Async do |
| 64 | + pipeline.call("My job") |
| 65 | + # Prints "Processing job: My job" |
| 66 | +end |
| 67 | +``` |
| 68 | + |
| 69 | +## Rails Integration |
| 70 | + |
| 71 | +You can use `async-job` with Rails's ActiveJob framework by using the `async-job-adapter-active_job` gem. This allows you to use `async-job` as a backend for ActiveJob, which is useful for integrating with existing Rails applications. |
| 72 | + |
| 73 | +### Redis Processor |
| 74 | + |
| 75 | +The `async-job-processor-redis` gem provides a Redis-based processor for `async-job`. This processor is similar to Sidekiq, and is designed to provide a similar interface and feature set. |
0 commit comments