class ActiveJobStatus::JobBatch

Attributes

batch_id[R]
job_ids[R]

Public Class Methods

find(batch_id:) click to toggle source
# File lib/active_job_status/job_batch.rb, line 43
def self.find(batch_id:)
  if ["ActiveSupport::Cache::RedisStore", "ActiveSupport::Cache::ReadthisStore"].include? ActiveJobStatus.store.class.to_s
    job_ids = ActiveJobStatus.store.smembers(batch_id)
  else
    job_ids = ActiveJobStatus.store.fetch(batch_id).to_a
  end

  if job_ids.any?
    ActiveJobStatus::JobBatch.new(batch_id: batch_id,
                                  job_ids: job_ids,
                                  store_data: false)
  end
end
new(batch_id:, job_ids:, expire_in: 259200, store_data: true) click to toggle source
# File lib/active_job_status/job_batch.rb, line 7
def initialize(batch_id:, job_ids:, expire_in: 259200, store_data: true)
  @batch_id = batch_id
  @job_ids = job_ids
  # the store_data flag is used by the ::find method return a JobBatch
  # object without re-saving the data
  self.store_data(expire_in: expire_in) if store_data
end

Public Instance Methods

add_jobs(job_ids:) click to toggle source
# File lib/active_job_status/job_batch.rb, line 25
def add_jobs(job_ids:)
  @job_ids = @job_ids + job_ids
  if ["ActiveSupport::Cache::RedisStore", "ActiveSupport::Cache::ReadthisStore"].include? ActiveJobStatus.store.class.to_s
    # Save an extra redis query and perform atomic operation
    ActiveJobStatus.store.sadd(@batch_id, job_ids)
  else
    existing_job_ids = ActiveJobStatus.store.fetch(@batch_id)
    ActiveJobStatus.store.write(@batch_id, existing_job_ids.to_a | job_ids)
  end
end
completed?() click to toggle source
# File lib/active_job_status/job_batch.rb, line 36
def completed?
  !@job_ids.map do |job_id|
    job_status = ActiveJobStatus.get_status(job_id)
    job_status != nil && job_status != :completed
  end.any?
end
store_data(expire_in:) click to toggle source
# File lib/active_job_status/job_batch.rb, line 15
def store_data(expire_in:)
  ActiveJobStatus.store.delete(@batch_id) # delete any old batches
  if ["ActiveSupport::Cache::RedisStore", "ActiveSupport::Cache::ReadthisStore"].include? ActiveJobStatus.store.class.to_s
    ActiveJobStatus.store.sadd(@batch_id, @job_ids)
    ActiveJobStatus.store.expire(@batch_id, expire_in)
  else
    ActiveJobStatus.store.write(@batch_id, @job_ids, expires_in: expire_in)
  end
end

Private Instance Methods

write(key, job_ids, expire_in=nil) click to toggle source
# File lib/active_job_status/job_batch.rb, line 60
def write(key, job_ids, expire_in=nil)
end