我正在使用Whenever gem作为cron任务。
这是我的schedule.rb文件"
set :output, "#{path}/log/cron.log"
set :environment, 'develop'
every 2.minutes do
runner "User.say_hello"
end
但它使用额外的反斜杠写入crontab并且无法正常工作
script/rails runner -e develop '\''User.say_hello'\''
但它应该是那样的?
script/rails runner -e develop 'User.say_hello'
更新
这是我在User class
中的类方法def self.say_hello
Mailer.cooperation("current_user").deliver!
end
它引发了这个错误
You did not specify how you would like Rails to report deprecation notices for your develop environment, please set config.active_support.deprecation to :log, :notify or :stderr at config/environments/develop.rb
/home/sunloverz/.rvm/gems/ruby-1.9.3-p448@socposts/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:47:in `resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
from /home/sunloverz/.rvm/gems/ruby-1.9.3-p448@socposts/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:41:in `resolve_string_connection'
from /home/sunloverz/.rvm/gems/ruby-1.9.3-p448@socposts/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:25:in `spec'
我已添加
config.active_support.deprecation = :log
在cron日志中打印并且不发送邮件,
但Mailer.cooperation("current_user").deliver!
正在从rails控制台发送邮件
的database.yml
development:
adapter: postgresql
encoding: utf8
reconnect: false
database: project_db
pool: 5
username: root
password: lkjsdEEdfd
host: localhost
答案 0 :(得分:2)
问题是您在同一环境中使用不同的名称:
在您的database.yml中,您将其称为development
而您正在使用名为develop
他们必须匹配。
现在一切正常,因为Rails使用的默认名称是开发,所以如果你在迁移时没有指定任何环境,它将默认使用开发。 但是你正在使用开发环境运行随时随地的任务,实际上没有任何数据库关联。
希望它有所帮助。