发条和register_javascript_expansion异常

时间:2013-07-18 09:15:34

标签: ruby ruby-on-rails-3 sidekiq engineyard clockwork

我正在使用SidekiqClockwork进行后台作业和日程安排。我的lib/clock.rb有一个执行某些rake任务的下一个代码:

require 'clockwork'
require File.expand_path('../../config/boot',        __FILE__)
require File.expand_path('../../config/environment', __FILE__)
require 'sidekiq'

include Clockwork
include Sidekiq

module Clockwork    
  every(1.day, 'rake places:get_from_api', at: '22:45') do
    Sidekiq.logger.info 'Starting rake places:get_from_api'
    execute_rake('places.rake', 'places:get_from_api') if Rails.env.production?
  end
end

def execute_rake(file,task)
  require 'rake'
  rake = Rake::Application.new
  Rake.application = rake
  Rake::Task.define_task(:environment)
  load "#{Rails.root}/lib/tasks/#{file}"
  rake[task].invoke
end

我在EngineYard上部署应用程序,并有一个after_restart.rb挂钩:

if environment == 'production'
  cw_pid_file = "#{shared_path}/pids/clockwork.pid"
  # stop clockwork
  run "if [ -d #{current_path} ] && [ -f #{cw_pid_file} ]; then cd #{current_path} && kill -int $(cat #{cw_pid_file}) 2>/dev/null; else echo 'clockwork was not running' ; fi"
  ## start clockwork
  run "cd #{current_path} && bundle exec clockworkd -c #{current_path}/lib/clock.rb --pid-dir #{shared_path}/pids --log --log-dir #{shared_path}/log restart >> log/clockwork.log 2>&1 &"
  run "ps -eo pid,command | grep clockwork | grep -v grep | awk '{print $1}' > #{cw_pid_file}"
end

部署后,我会在clockworkd.clock.output日志中看到an exception

如何解决?

1 个答案:

答案 0 :(得分:1)

Engine Yard的工作人员在这里。

查看您的错误,您正试图在Nil上调用每个语句。可能在你的自定义rake任务中。如果没有看到更多的代码,我们无法真正调试它。

此外,我可以推荐普通的Cron任务作为在Engine Yard上运行常规任务的更好方法,而不是使用发条。

  1. 在您的环境底部点击Crontabs(图片http://d.pr/i/3Yeo
  2. 通过提供作业名称,例如命令来添加作业用于运行rake任务: cd /data/APP_NAME/current && RAILS_ENV=production /usr/local/bin/bundle exec rake task_name
  3. 给它一个用户和时间间隔,然后按“创建”。
  4. 返回您的环境,按“应用”。
  5. 如果你想在rails中设置cron任务的更多ruby方法,有一个名为Whenever的漂亮宝石允许你在ruby中编写cron任务但是它们被写入crontab。一个Google,无论何时在Engine Yard上返回一些关于其他人如何去做的帖子,但是为了做一些像你所展示的那样简单,我的上述说明可能是最简单的。