Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,52 @@ class MyJob < ActiveJob::Base
end
```

### Grouping jobs into batches

```ruby
job = MyJob.perform_later
other_job = OtherJob.perform_later

batch = ActiveJob::Status::Batch.new([job, other_job])
batch.status
# "queued"
```

The batch status can be `queued`, `failed`, `completed` or `working`.

1. The batch is considered `queued` if **all** of the jobs are `queued`
2. The batch is considered `failed` if **one** of the jobs is `failed`
3. The batch is considered `completed` if **all** of the jobs are `completed`
4. The batch is considered `working` in all other circumstances

### Callbacks

You can implement callbacks, by listening to the completion of a batch with a
simple ActiveJob job.

```ruby
# frozen_string_literal: true

require 'activejob-status'

class CallbacksJob < ApplicationJob
queue_as :real_time

def perform(*job_ids)
batch = ActiveJob::Status::Batch.new(job_ids)

case batch.status
when :queued, :working
MonitorAnalysisBatchJob.set(wait: 5.seconds).perform_later(*job_ids)
when :completed
# Completed callback
when :failed
# Failed callback
end
end
end
```

## ActiveJob::Status and exceptions

Internally, ActiveJob::Status uses `ActiveSupport#rescue_from` to catch every `Exception` to apply the `failed` status
Expand Down
1 change: 1 addition & 0 deletions lib/activejob-status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "activejob-status/status"
require "activejob-status/progress"
require "activejob-status/throttle"
require "activejob-status/batch"

module ActiveJob
module Status
Expand Down
34 changes: 34 additions & 0 deletions lib/activejob-status/batch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module ActiveJob
module Status
class Batch
def initialize(jobs)
@jobs = jobs
@storage = ActiveJob::Status::Storage.new
end

def status
if @jobs.all? { |job| status_for(job) == :queued }
:queued
elsif @jobs.any? { |job| status_for(job) == :failed }
:failed
elsif @jobs.all? { |job| status_for(job) == :completed }
:completed
else
:working
end
end

private

def statuses
@statuses ||= @storage.read_multi(@jobs)
end

def status_for(job)
statuses.dig(@storage.key(job), :status)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/activejob-status/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def read(job)
store.read(key(job)) || {}
end

def read_multi(jobs)
store.read_multi(*jobs.map { |job| key(job) })
end

def write(job, message, force: false)
@throttle.wrap(force: force) do
store.write(key(job), message, expires_in: @expires_in)
Expand Down
52 changes: 52 additions & 0 deletions spec/specs/active_job/status/batch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require_relative "../../../spec_helper"
require_relative "../../../jobs/test_jobs"

RSpec.describe ActiveJob::Status::Batch do
describe "#status" do
it "returns queued when all jobs are queued" do
first_job = BaseJob.perform_later
second_job = BaseJob.perform_later
batch = described_class.new([first_job, second_job])

ActiveJob::Status.get(first_job).update(status: :queued)
ActiveJob::Status.get(second_job).update(status: :queued)

expect(batch.status).to eq(:queued)
end

it "returns failed when one job is failed" do
first_job = BaseJob.perform_later
second_job = BaseJob.perform_later
batch = described_class.new([first_job, second_job])

ActiveJob::Status.get(first_job).update(status: :failed)
ActiveJob::Status.get(second_job).update(status: :completed)

expect(batch.status).to eq(:failed)
end

it "returns completed when all jobs are completed" do
first_job = BaseJob.perform_later
second_job = BaseJob.perform_later
batch = described_class.new([first_job, second_job])

ActiveJob::Status.get(first_job).update(status: :completed)
ActiveJob::Status.get(second_job).update(status: :completed)

expect(batch.status).to eq(:completed)
end

it "returns working in other cases" do
first_job = BaseJob.perform_later
second_job = BaseJob.perform_later
batch = described_class.new([first_job, second_job])

ActiveJob::Status.get(first_job).update(status: :queued)
ActiveJob::Status.get(second_job).update(status: :working)

expect(batch.status).to eq(:working)
end
end
end