lib/resque/plugins/multi_step_task.rb in resque-multi-step-1.0.0 vs lib/resque/plugins/multi_step_task.rb in resque-multi-step-1.0.1

- old
+ new

@@ -80,22 +80,30 @@ pjg = new(task_id) end # Handle job invocation def perform(task_id, job_module_name, *args) + task = perform_without_maybe_finalize(task_id, job_module_name, *args) + task.maybe_finalize + end + + def perform_without_maybe_finalize(task_id, job_module_name, *args) task = MultiStepTask.find(task_id) begin constantize(job_module_name).perform(*args) rescue Exception => e task.increment_failed_count raise end - task.increment_completed_count - task.maybe_finalize + task end + def perform_finalization(task_id, job_module_name, *args) + perform_without_maybe_finalize(task_id, job_module_name, *args) + end + # Normally jobs that are part of a multi-step task are run # asynchronously by putting them on a queue. However, it is # often more convenient to just run the jobs synchronously as # they are registered in a development environment. Setting # mode to `:sync` provides a way to do just that. @@ -217,11 +225,10 @@ # that finish about the same time. raise FinalizationAlreadyBegun unless redis.setnx("i_am_the_finalizer", 1) if synchronous? sync_finalize! - else if fin_job_info = redis.lpop('finalize_jobs') fin_job_info = Yajl::Parser.parse(fin_job_info) Resque::Job.create(queue_name, FinalizationJob, self.task_id, *fin_job_info) else @@ -232,20 +239,19 @@ end def sync_finalize! while fin_job_info = redis.lpop('finalize_jobs') job_class_name, *args = Yajl::Parser.parse(fin_job_info) - self.class.perform(task_id, job_class_name, *args) + self.class.perform_finalization(task_id, job_class_name, *args) end + nuke end # Execute finalization sequence if it is time. def maybe_finalize - return unless ready_for_finalization? + return unless ready_for_finalization? && !incomplete_because_of_errors? finalize! - rescue Exception - # just eat the exception end # Is this task at the point where finalization can occur. def ready_for_finalization? finalizable? && completed_count >= normal_job_count @@ -260,9 +266,14 @@ # If the failed job is retried and succeeds finalization will # proceed at usual. def incomplete_because_of_errors? failed_count > 0 && completed_count < normal_job_count end + + def unfinalized_because_of_errors? + failed_count > 0 && completed_count < (normal_job_count + finalize_job_count) + end + end end end