【发布时间】:2014-12-08 22:17:05
【问题描述】:
我有一个作为模型方法实现的延迟工作(见下文)。如果我使用delayed_job 守护程序,它会默默地运行并死去。没有一项工作完成,也没有找到记录的消息。但如果我使用 RAILS_ENV=production rake jobs:work 一切正常。
我不知道为什么,即使抛出异常它也应该出现在日志中,但是没有。如果逻辑有问题,那么为什么 rake 作业会成功?
def recalc(params)
last_known = self
t = nil # target(self)
target_date = self.as_on.yesterday
success = true
saved = -1
# cater for the first one
TimeSlot.where(employee_id:self.employee_id).where('incurred_on >= ?', self.as_on).order('incurred_on ASC').each do |ts|
# loop
if (ts.incurred_on >= target_date) then
if !t.nil? && target_date.day <=7 # roll over to a new month
t.bal_sick += 4 # add 4 days
if t.bal_sick > 40
overflow = t.bal_sick-40
t.bal_sick = 40
t.bal_sick2 += overflow
t.bal_sick2 = 120 if t.bal_sick2 > 120 # overflow again
end
end
unless saved<0
success = t.save
last_known = t
end
if success
saved += 1
t = target(last_known)
target_date = t.as_on
else
logger.warn("Recalc cannot saved a record for #{t.errors.first}")
logger.warn(t.inspect)
return
end
end
if ts.types.include? 'overtime'
t.bal_ot += ts.hours.to_i
t.bal_ot = 100 if t.bal_ot >100
elsif ts.types.include? 'toil'
t.bal_ot -= ts.hours.to_i
elsif ts.types.include? 'vacation'
t.bal_vacation -= ts.hours
elsif ts.types.include? 'sick1'
t.bal_sick -= ts.hours
end
end
logger.info("Recalc saved %d records"% saved)
end
【问题讨论】:
标签: ruby-on-rails activerecord delayed-job