Class: AngryBatch::Batch

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/angry_batch/batch.rb

Overview

Schema Information

Table name: angry_batch_batches

id :bigint(8) not null, primary key complete_handlers :jsonb not null completed_jobs_count :integer default(0), not null failed_jobs_count :integer default(0), not null failure_handlers :jsonb not null finished_at :datetime jobs_count :integer default(0), not null label :string metadata :jsonb not null state :string default("pending"), not null created_at :datetime not null updated_at :datetime not null

Indexes

index_angry_batch_batches_on_state (state)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.expiredObject



36
37
38
# File 'lib/angry_batch/batch.rb', line 36

def expired
  completed.where(updated_at: ...2.days.ago).or(failed.where(updated_at: ...4.weeks.ago)).or(pending.where(updated_at: ...4.weeks.ago))
end

Instance Method Details

#check_status_of_jobsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/angry_batch/batch.rb', line 80

def check_status_of_jobs
  handlers_to_enqueue = with_lock do
    return unless pending?
    return if jobs_count.zero?
    return unless pending_jobs_count <= 0

    self.finished_at = Time.current

    if failed_jobs_count.zero?
      update! state: 'completed'
      complete_handlers
    else
      update! state: 'failed'
      failure_handlers
    end
  end

  enqueue_handlers(handlers_to_enqueue)
end

#enqueue(job_class) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/angry_batch/batch.rb', line 41

def enqueue(job_class, *, **)
  AngryBatch::Helper.assert_batchable(job_class)

  job = job_class.new(*, **)

  with_lock do
    raise AngryBatch::BatchFinishedError, "Batch #{id} is #{state}" unless pending?

    AngryBatch::Helper.add_job_to_batch(self, job)
  end

  AngryBatch::Helper.after_transaction do
    raise ActiveJob::EnqueueError, ["#{job_class} was not enqueued", job.enqueue_error&.message].compact.join(': ') unless job.enqueue
  end

  job
end

#metadataObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/angry_batch/batch.rb', line 59

def 
  raw = read_attribute(:metadata)

  unless defined?() &&  == raw
     = ActiveJob::Arguments.deserialize([raw || {}]).first
     = raw
  end

  
end

#pending_jobs_countObject



70
71
72
# File 'lib/angry_batch/batch.rb', line 70

def pending_jobs_count
  jobs_count - completed_jobs_count - failed_jobs_count
end

#progressObject



74
75
76
77
78
# File 'lib/angry_batch/batch.rb', line 74

def progress
  return 0 if jobs_count.zero?

  ((completed_jobs_count + failed_jobs_count) * 100 / jobs_count).clamp(0, 100)
end