Class: ActiveJobStatus::JobBatch

Inherits:
Object
  • Object
show all
Defined in:
lib/active_job_status/job_batch.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(batch_id:, job_ids:, expire_in: 259200, store_data: true) ⇒ JobBatch

Returns a new instance of JobBatch.



7
8
9
10
11
12
13
# 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

Instance Attribute Details

#batch_idObject (readonly)

Returns the value of attribute batch_id.



4
5
6
# File 'lib/active_job_status/job_batch.rb', line 4

def batch_id
  @batch_id
end

#job_idsObject (readonly)

Returns the value of attribute job_ids.



5
6
7
# File 'lib/active_job_status/job_batch.rb', line 5

def job_ids
  @job_ids
end

Class Method Details

.find(batch_id:) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

Instance Method Details

#add_jobs(job_ids:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# 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?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# 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:) ⇒ Object



15
16
17
18
19
20
21
22
23
# 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