From 06635281c1fbadade6fdc605398696067597b07d Mon Sep 17 00:00:00 2001 From: Gabriel Alacchi Date: Sat, 29 May 2021 11:15:02 -0700 Subject: [PATCH] Max iters per instance + enqueue after shutdown --- lib/job-iteration/iteration.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/job-iteration/iteration.rb b/lib/job-iteration/iteration.rb index e0f263b7..864da3d4 100644 --- a/lib/job-iteration/iteration.rb +++ b/lib/job-iteration/iteration.rb @@ -34,6 +34,7 @@ def inspected_cursor :start_time, :times_interrupted, :total_time, + :instance_iterations ) define_callbacks :start @@ -42,11 +43,17 @@ def inspected_cursor end module ClassMethods + cattr_accessor :max_iters + def method_added(method_name) ban_perform_definition if method_name.to_sym == :perform super end + def max_iters_per_run(value) + self.max_iters = value + end + def on_start(*filters, &blk) set_callback(:start, :after, *filters, &blk) end @@ -70,6 +77,7 @@ def initialize(*arguments) super self.times_interrupted = 0 self.total_time = 0.0 + self.instance_iterations = 0 assert_implements_methods! end ruby2_keywords(:initialize) if respond_to?(:ruby2_keywords, true) @@ -136,6 +144,8 @@ def interruptible_perform(*arguments) run_callbacks(:complete) output_interrupt_summary end + + reenqueue_iteration_job unless completed end def iterate_with_enumerator(enumerator, arguments) @@ -149,11 +159,11 @@ def iterate_with_enumerator(enumerator, arguments) found_record = true each_iteration(object_from_enumerator, *arguments) self.cursor_position = index + self.instance_iterations += 1 end next unless job_should_exit? self.executions -= 1 if executions > 1 - reenqueue_iteration_job return false end @@ -256,6 +266,8 @@ def output_interrupt_summary end def job_should_exit? + return true if self.class.max_iters.present? && self.class.max_iters == instance_iterations + if ::JobIteration.max_job_runtime && start_time && (Time.now.utc - start_time) > ::JobIteration.max_job_runtime return true end