lib/job/bonus_features.rb in genki-background_fu-1.0.8.2 vs lib/job/bonus_features.rb in genki-background_fu-1.0.9

- old
+ new

@@ -14,10 +14,11 @@ res = catch(:stopping) do invoke_worker_without_threads; nil end + self.reload self.state = res ? "stopped" : "finished" end def ensure_worker_with_threads ensure_worker_without_threads @@ -42,11 +43,11 @@ update_attribute(:state, "stopping") logger.info("BackgroundFu: Stopping job. Job(id: #{id}).") end end - # Overwritten because of new "stopped" state. + # Overridden because of new "stopped" state. def restart_with_threads! if stopped? || failed? update_attributes!( :result => nil, :progress => nil, @@ -55,31 +56,29 @@ ) logger.info("BackgroundFu: Restarting job. Job(id: #{id}).") end end - # This is the only place where multi-threading - # is used in the plugin and is completely optional. + # Monitors the worker and updates the job progress. If the job's status + # is changed to 'stopping', the worker is requested to stop. def monitor_worker Thread.new do - # 1. running? - check if not failed or finished. - # 2. !Job.find(id).stopping? - check if someone ordered stopping the job. - while(running? && !Job.find(id).stopping?) + while running? && !Job.find(id).stopping? current_progress = @worker.instance_variable_get("@progress") if current_progress == progress sleep 5 else update_attribute(:progress, current_progress) sleep 1 end end - # If someone ordered stopping a job we infrom the worker that it should stop. - if(Job.find(id).stopping?) + if Job.find(id).stopping? @worker.instance_variable_set("@stopping", true) end end + logger.info("BackgroundFu: Job monitoring started. Job(id: #{id}).") end # Closes database connections left after finished threads. def cleanup_after_threads