Module: BatchProcessor::Batch::Controller

Extended by:
ActiveSupport::Concern
Included in:
BatchProcessor::BatchBase
Defined in:
lib/batch_processor/batch/controller.rb

Instance Method Summary collapse

Instance Method Details

#abort!Object



70
71
72
73
74
75
76
77
78
# File 'lib/batch_processor/batch/controller.rb', line 70

def abort!
  raise BatchProcessor::NotStartedError unless started?
  raise BatchProcessor::AlreadyFinishedError if finished?
  raise BatchProcessor::AlreadyAbortedError if aborted?

  run_callbacks(:batch_aborted) { details.aborted_at = Time.current }

  aborted?
end

#clear!Object



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

def clear!
  raise BatchProcessor::NotAbortedError unless aborted?
  raise BatchProcessor::AlreadyFinishedError if finished?
  raise BatchProcessor::AlreadyClearedError if cleared?

  run_callbacks(:batch_cleared) do
    pending_jobs_count = details.pending_jobs_count
    running_jobs_count = details.running_jobs_count

    pipelined do
      details.cleared_at = Time.current
      details.finished_at = Time.current
      details.decrement(:pending_jobs_count, by: pending_jobs_count)
      details.decrement(:running_jobs_count, by: running_jobs_count)
      details.increment(:cleared_jobs_count, by: pending_jobs_count + running_jobs_count)
    end
  end

  run_callbacks(:batch_finished)

  cleared?
end

#enqueuedObject



61
62
63
64
65
66
67
68
# File 'lib/batch_processor/batch/controller.rb', line 61

def enqueued
  raise BatchProcessor::AlreadyEnqueuedError if enqueued?
  raise BatchProcessor::NotStartedError unless started?

  run_callbacks(:batch_enqueued) { details.enqueued_at = Time.current }

  enqueued?
end

#finishObject



103
104
105
106
107
108
109
110
# File 'lib/batch_processor/batch/controller.rb', line 103

def finish
  raise BatchProcessor::AlreadyFinishedError if finished?
  raise BatchProcessor::StillProcessingError if unfinished_jobs?

  run_callbacks(:batch_finished) { details.finished_at = Time.current }

  finished?
end

#startObject



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

def start
  raise BatchProcessor::CollectionInvalidError unless collection.valid?
  raise BatchProcessor::AlreadyStartedError if started?
  raise BatchProcessor::CollectionEmptyError if collection_items.empty? && !allow_empty?

  run_callbacks(:batch_started) do
    collection_size = collection_items.count

    pipelined do
      details.class_name = class_name
      details.started_at = Time.current
      details.size = collection_size
      details.pending_jobs_count = collection_size
    end
  end

  started?
end