lib/bj/runner.rb in ThiagoLelis-backgroundjob-1.0.3 vs lib/bj/runner.rb in ThiagoLelis-backgroundjob-1.0.4

- old
+ new

@@ -181,28 +181,28 @@ catch :no_jobs do loop do job = thread = stdout = stderr = nil Bj.transaction(options) do - now = Time.now + now = Util.now - job = Bj::Table::Job.find :first, + job = Bj::Table::Job.find_first_and_lock( :conditions => ["state = ? and submitted_at <= ?", "pending", now], :order => "priority DESC, submitted_at ASC", :limit => 1, - :lock => true + :lock => true) throw :no_jobs unless job Bj.logger.info{ "#{ job.title } - started" } command = job.command env = job.env || {} stdin = job.stdin || '' stdout = job.stdout || '' stderr = job.stderr || '' - started_at = Time.now + started_at = Util.now thread = Util.start command, :cwd=>Bj.rails_root, :env=>env, :stdin=>stdin, :stdout=>stdout, :stderr=>stderr job.state = "running" job.runner = Bj.hostname @@ -211,11 +211,11 @@ job.save! job.reload end exit_status = thread.value - finished_at = Time.now + finished_at = Util.now Bj.transaction(options) do job = Bj::Table::Job.find job.id break unless job job.state = "finished" @@ -285,11 +285,11 @@ end end def fill_morgue Bj.transaction do - now = Time.now + now = Util.now jobs = Bj::Table::Job.find :all, :conditions => ["state = 'running' and runner = ?", Bj.hostname] jobs.each do |job| if job.is_restartable? Bj.logger.info{ "#{ job.title } - found dead and bloated but resubmitted" } @@ -307,10 +307,10 @@ end end def archive_jobs Bj.transaction do - now = Time.now + now = Util.now too_old = now - Bj.ttl jobs = Bj::Table::Job.find :all, :conditions => ["(state = 'finished' or state = 'dead') and submitted_at < ?", too_old] jobs.each do |job| Bj.logger.info{ "#{ job.title } - archived" }