Skip to content

Commit

Permalink
Standardize read_multi output
Browse files Browse the repository at this point in the history
  • Loading branch information
matteeyah committed Jan 27, 2024
1 parent 3843f02 commit 5eab9fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
21 changes: 9 additions & 12 deletions lib/activejob-status/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,23 @@ def initialize(jobs)
end

def status
if @jobs.all? { |job| status_for(job) == :queued }
:queued
elsif @jobs.any? { |job| status_for(job) == :failed }
statuses = read.values.map { |cache_value| cache_value[:status] }

if statuses.include?(:failed)
:failed
elsif @jobs.all? { |job| status_for(job) == :completed }
elsif statuses.all?(:queued)
:queued
elsif statuses.all?(: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)
def read
@storage.read_multi(@jobs)
end
alias_method :to_h, :read
end
end
end
4 changes: 3 additions & 1 deletion lib/activejob-status/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def read(job)
end

def read_multi(jobs)
store.read_multi(*jobs.map { |job| key(job) })
keys = jobs.map { |job| key(job) }
data = store.read_multi(*keys)
keys.index_with { |k| data.fetch(k, {}) }
end

def write(job, message, force: false)
Expand Down

0 comments on commit 5eab9fa

Please sign in to comment.