diff --git a/lib/activejob-status/batch.rb b/lib/activejob-status/batch.rb index 3027a1a..fb05efa 100644 --- a/lib/activejob-status/batch.rb +++ b/lib/activejob-status/batch.rb @@ -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 diff --git a/lib/activejob-status/storage.rb b/lib/activejob-status/storage.rb index da0f7d6..455fb73 100644 --- a/lib/activejob-status/storage.rb +++ b/lib/activejob-status/storage.rb @@ -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)