Class: AngryBatch::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/angry_batch/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(label: nil) ⇒ Builder

Returns a new instance of Builder.



4
5
6
7
8
9
10
11
12
13
# File 'lib/angry_batch/builder.rb', line 4

def initialize(label: nil)
  @batch = AngryBatch::Batch.new(
    label: label,
    state: 'scheduling',
    complete_handlers: [],
    failure_handlers: [],
  )
  @jobs = []
  @performed = false
end

Instance Method Details

#enqueue(job_class) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/angry_batch/builder.rb', line 35

def enqueue(job_class, *, **)
  raise AngryBatch::BatchArgumentError, 'Batch is already running' if performed?
  raise AngryBatch::BatchArgumentError, "#{job_class} must be a subclass of ActiveJob::Base" unless job_class.is_a?(Class) && job_class < ActiveJob::Base
  raise AngryBatch::BatchArgumentError, "#{job_class} must include AngryBatch::Batchable" unless job_class.included_modules.include?(AngryBatch::Batchable)

  @jobs << job_class.new(*, **)
end

#on_complete(job_class) ⇒ Object



21
22
23
24
25
26
# File 'lib/angry_batch/builder.rb', line 21

def on_complete(job_class, *, **)
  raise AngryBatch::BatchArgumentError, 'Batch is already running' if performed?
  raise AngryBatch::BatchArgumentError, "#{job_class} must be a subclass of ActiveJob::Base" unless job_class.is_a?(Class) && job_class < ActiveJob::Base

  @batch.complete_handlers << [job_class, job_class.new(*, **).serialize['arguments']]
end

#on_failure(job_class) ⇒ Object



28
29
30
31
32
33
# File 'lib/angry_batch/builder.rb', line 28

def on_failure(job_class, *, **)
  raise AngryBatch::BatchArgumentError, 'Batch is already running' if performed?
  raise AngryBatch::BatchArgumentError, "#{job_class} must be a subclass of ActiveJob::Base" unless job_class.is_a?(Class) && job_class < ActiveJob::Base

  @batch.failure_handlers << [job_class, job_class.new(*, **).serialize['arguments']]
end

#perform_laterObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/angry_batch/builder.rb', line 43

def perform_later
  raise AngryBatch::BatchArgumentError, 'Batch is empty' if empty?
  raise AngryBatch::BatchArgumentError, 'Batch is already running' if performed?

  ActiveRecord::Base.transaction(requires_new: true) do
    @batch.save!

    @jobs.each do |job|
      @batch.jobs.create!(
        active_job_idx: job.job_id,
        active_job_class: job.class.name,
        active_job_arguments: job.serialize['arguments'],
      )
    end

    @batch.update!(state: 'pending')
  end

  @performed = true
  @jobs.each(&:enqueue)
  @batch.check_status_of_jobs
rescue
  unless @performed
    @batch = AngryBatch::Batch.new(
      label: @batch.label,
      state: 'scheduling',
      complete_handlers: @batch.complete_handlers,
      failure_handlers: @batch.failure_handlers,
    )
  end
  raise
end

#performed?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/angry_batch/builder.rb', line 15

def performed?
  @performed
end