Whenever cron job in Rails fails with 'production' database is not configured error

前端 未结 1 628
陌清茗
陌清茗 2021-01-06 08:48

In my Rails app I use whenever gem to run a Sideqik task. When I run that command manually in the rails console it works. But the Whenever cron fails with the following erro

相关标签:
1条回答
  • 2021-01-06 09:36

    You're encountering this error because your production settings aren't configured correctly to connect to your database. When you run the command from the Rails console, you're connecting to the development environment, but your cron job is being run in the production environment.


    Rails has 3 built-in environments. The main difference is that each has their own database, but there are other differences too.

    • Production, for when your code is running live. This environment does a lot of caching, and doesn't show development error messages to the user.
    • Development, for building your application. This is the default environment, and is the environment that you spend most of your time in.
    • Test, for running automated tests. The main reason this is separate is so your test suite can empty & recreate the database on each test run. This makes your tests more reproducible, and stops an accidental test run from destroying your development DB.

    Mostly, these are configured via files in config/environments. The database connections are configured in config/database.yml - you'll find YAML keys for development, production, and test in there. The Rails Guide has more details about configuring applications.

    You can find out which environment you're running in via Rails.env. For instance:

    user@foo $ rails c
    Loading development environment (Rails 4.1.1)
    irb(main):001:0> Rails.env
    "development"
    

    And you can force a command to run in a given environment via a RAILS_ENV shell environment variable:

    user@foo $ RAILS_ENV=production rails c
    Loading production environment (Rails 4.1.1)
    irb(main):001:0> Rails.env
    "production"
    

    To fix your problem, you have two choices:

    1. Force your cron command to run in the development environment. It sounds like your app is running in the development environment (because it works from the console, and you have no production database configured). So forcing the cron job to run in the development environment should fix this. You could set it in your cronjob directly by setting the RAILS_ENV variable as we saw above, but whenever lets you specify the environment on the commandline or within your schedule.rb:

      set :environment, "development"
      
    2. Fix your app to run in production. If your application is live, it really should be running in the production environment. It will be faster and more secure that way - as well as letting you mess things up when developing new features without the risk of destroying something used by your users. You'll have to set up the database, and will have to test your app in production. The best way to do this varies by platform; this Stack Overflow question has a good summary of the various options.

    0 讨论(0)
提交回复
热议问题