我正在使用发条宝石发送每日电子邮件。我在lib文件夹中创建了clock.rb文件。
#clock.rb
require 'rubygems'
require 'clockwork'
include Clockwork
every(1.day, 'reminders.send', :at => '09:30'){
@leave_details = LeaveDetail.all(:conditions => {:status => [LeaveDetail::STATUS_PENDING, LeaveDetail::STATUS_PENDING_MNGT]})
@leave_details.each do |ld|
UserMailer.leave_reminder_email(ld).deliver
end
}
我创建了一个Procfile: -
web: bundle exec thin start -d
clock: bundle exec clockwork lib/clock.rb
当我命令
foreman start
显示错误: -
17:20:51 web.1 | started with pid 21144
17:20:51 clock.1 | started with pid 21146
17:20:52 clock.1 | I, [2012-12-06T17:20:52.558231 #21150] INFO -- : Starting clock for 1 events: [ reminder.deliver ]
17:20:52 clock.1 | I, [2012-12-06T17:20:52.558324 #21150] INFO -- : Triggering 'reminder.deliver'
17:20:52 clock.1 | E, [2012-12-06T17:20:52.558399 #21150] ERROR -- : uninitialized constant LeaveDetail (NameError)
17:20:52 clock.1 | /home/akhileshwar/Desktop/14-11-2012/RoR/P10HR/lib/clock.rb:8:in `block in <top (required)>'
17:20:52 clock.1 | /home/akhileshwar/.rvm/gems/ruby-1.9.3-p194@rails326/gems/clockwork-0.4.1/lib/clockwork.rb:93:in `call'
17:20:52 clock.1 | /home/akhileshwar/.rvm/gems/ruby-1.9.3-p194@rails326/gems/clockwork-0.4.1/lib/clockwork.rb:93:in `run'
17:20:52 clock.1 | /home/akhileshwar/.rvm/gems/ruby-1.9.3-p194@rails326/gems/clockwork-0.4.1/lib/clockwork.rb:172:in `block in tick'
17:20:52 clock.1 | /home/akhileshwar/.rvm/gems/ruby-1.9.3-p194@rails326/gems/clockwork-0.4.1/lib/clockwork.rb:170:in `each'
17:20:52 clock.1 | /home/akhileshwar/.rvm/gems/ruby-1.9.3-p194@rails326/gems/clockwork-0.4.1/lib/clockwork.rb:170:in `tick'
17:20:52 clock.1 | /home/akhileshwar/.rvm/gems/ruby-1.9.3-p194@rails326/gems/clockwork-0.4.1/lib/clockwork.rb:156:in `block in run'
17:20:52 clock.1 | /home/akhileshwar/.rvm/gems/ruby-1.9.3-p194@rails326/gems/clockwork-0.4.1/lib/clockwork.rb:155:in `loop'
17:20:52 clock.1 | /home/akhileshwar/.rvm/gems/ruby-1.9.3-p194@rails326/gems/clockwork-0.4.1/lib/clockwork.rb:155:in `run'
17:20:52 clock.1 | /home/akhileshwar/.rvm/gems/ruby-1.9.3-p194@rails326/gems/clockwork-0.4.1/bin/clockwork:19:in `<top (required)>'
17:20:52 clock.1 | /home/akhileshwar/.rvm/gems/ruby-1.9.3-p194@rails326/bin/clockwork:23:in `load'
17:20:52 clock.1 | /home/akhileshwar/.rvm/gems/ruby-1.9.3-p194@rails326/bin/clockwork:23:in `<main>'
17:20:52 web.1 | >> Deleting stale PID file tmp/pids/thin.pid
17:20:52 web.1 | exited with code 0
17:20:52 system | sending SIGTERM to all processes
SIGTERM received
17:20:52 clock.1 | terminated by SIGTERM
LeaveDetail是模型中的ruby类。 任何人都可以告诉我为什么'LeaveDetail'在这里显示未初始化? thnks
答案 0 :(得分:0)
您实际上并没有在发条中加载您的应用。发条是一个单独的独立红宝石过程。如果您没有告诉它应用程序的其余部分,它不知道它的存在,因此您的错误是合乎逻辑的。这来自sidekiq's clockwork example documentation:
# require boot & environment for a Rails app
require_relative "../config/boot"
require_relative "../config/environment"
答案 1 :(得分:0)
您可以使用include Clockwork
作为模块,添加require './config/boot'
和require './config/environment'
require './config/boot'
require './config/environment'
require 'clockwork'
module Clockwork
every(1.day, 'reminders.send', :at => '09:30'){
@leave_details = LeaveDetail.all(:conditions => {:status => [LeaveDetail::STATUS_PENDING, LeaveDetail::STATUS_PENDING_MNGT]})
@leave_details.each do |ld|
UserMailer.leave_reminder_email(ld).deliver
end
}
end