Ruby Crontab Rake任务失败,但仅在crontab运行命令时

时间:2013-10-10 15:42:45

标签: ruby activerecord cron

我有一个ruby sinatra应用程序,它有一个rake任务,它将使用活动记录检查数据库中的数据文件。我正在使用when gem来按计划运行此rake任务。这个工作正常一段时间,然后几天之后会出现故障,看起来它无法写入日志文件。我有一个救援会给我发电子邮件错误和堆栈跟踪,这就是我得到的:

Broken pipe - 
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:578:in `write'
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:578:in `warn'
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:578:in `rescue in write'
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:562:in `write'
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:382:in `add'
/usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/logger.rb:456:in `error'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:428:in `rescue in log'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `log'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:286:in `execute'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:220:in `execute'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:224:in `exec_query'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:233:in `select'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:30:in `select_one'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:36:in `select_value'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/relation/finder_methods.rb:174:in `exists?'
/home/deployer/public/fulfillment_api/shared/bundle/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/querying.rb:3:in `exists?'
/home/deployer/public/fulfillment_api/releases/20131010141756/app/models/clients/test/order_file.rb:39:in `block in create_new_orders'

每当gem创建的crontab如下所示:

0 0,12 * * * /bin/bash -l -c 'cd /home/deployer/public/fulfillment_api/releases/20131010141756 && RACK_ENV=production bundle exec rake background_jobs:process_and_email['\''khartnet@gmail.com'\''] --silent'

奇怪的是,当我将此命令发送到命令行时,它运行正常。即使我将用户切换到root并运行它,它仍然没有任何问题。我只是在crontab运行时遇到错误。

1 个答案:

答案 0 :(得分:0)

找到答案后:

https://stackoverflow.com/a/14792306/1512654

我尝试设置RVM并记录输出。这似乎让它运作起来。删除额外的部分后,我发现我只需要在cron任务的末尾添加“> /tmp/cron_log.log”以使其正常工作。我不确定这个有效的原因。我猜这个cron工作遇到了STDOUT的问题?