Rails class cron 作业中的方法在 elastic beanstalk 中不起作用

Rails class method in cron job not working in elastic beanstalk

我有一个 class 方法,我需要每 15 分钟 运行,我有 cron 作业

0,15,30,45 * * * * /bin/bash -l -c 'cd /var/app/current && sudo /opt/rubies/ruby-2.3.5/bin/bundle exec /opt/rubies/ruby-2.3.5/bin/rails runner -e production '\''Structure.check_parking'\'' >> /var/app/current/log/cron_log 2>&1'

运行 在我的弹性 beantsalk 环境中,但我一直收到

的错误
/opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/mysql2-0.4.4/lib/mysql2/client.rb:87:in `connect': Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

我在我的生产环境中使用 RDS 服务器,所以奇怪的是它试图连接到本地 mysql 服务器。

rails 应用程序已连接到数据库,我可以在其上执行所有正常功能,但是当我尝试 运行 此 class 方法时,出现此错误。

我猜它不会 运行 生产,但我真的不确定,我的 database.yml 文件看起来像

development:
    adapter: mysql2
    database: bddatabase
    encoding: utf8
    username: bduser
    password: dbpass
    host: 127.0.0.1
    port: 3306

production:
    adapter: mysql2
    encoding: utf8
    database: <%= ENV['RDS_DB_NAME'] %>
    username: <%= ENV['RDS_USERNAME'] %>
    password: <%= ENV['RDS_PASSWORD'] %>
    host: <%= ENV['RDS_HOSTNAME'] %>
    port: <%= ENV['RDS_PORT'] %>

一段时间以来一直在努力解决这个问题,非常感谢您的帮助!

我弄明白了,原来是我的 cron 任务本身有问题。我认为我引用 bundle 和 rails 的方式并没有指向导致错误的我的应用程序。将命令更改为

0,15,30,45 * * * * /bin/bash -l -c 'cd /var/app/current && bundle exec rails runner -e production 'Structure.check_parking' >> /var/app/current/log/cron_log 2>&1'

成功了