Skip to content

Commit

Permalink
Use read_multi for reading statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
matteeyah committed Jan 27, 2024
1 parent c6ca11a commit 3843f02
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/activejob-status/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@ module ActiveJob
module Status
class Batch
def initialize(jobs)
@statuses = jobs.map { |job| ActiveJob::Status.get(job) }
@jobs = jobs
@storage = ActiveJob::Status::Storage.new
end

def status
if @statuses.all? { |status| status[:status] == :queued }
if @jobs.all? { |job| status_for(job) == :queued }
:queued
elsif @statuses.any? { |status| status[:status] == :failed }
elsif @jobs.any? { |job| status_for(job) == :failed }
:failed
elsif @statuses.all? { |status| status[:status] == :completed }
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

0 comments on commit 3843f02

Please sign in to comment.