添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

PG::UndefinedTable: ERROR: relation "services" does not exist LINE 1: SELECT "services".* FROM "services"

Ask Question

I have some trouble using the whenever gem. I create a rake task which is working perfectly fine when I launch it myself BUT when I try to automate it with whenever I got the following message in my log:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "services" does not exist
LINE 1: SELECT "services".* FROM "services"
: SELECT "services".* FROM "services"
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `async_exec'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `block in exec_no_cache'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract_adapter.rb:590:in `block in log'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activesupport-5.0.6/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `exec_no_cache'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:585:in `execute_and_clear'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:103:in `exec_query'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:377:in `select_prepared'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/database_statements.rb:39:in `select_all'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/query_cache.rb:95:in `select_all'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/querying.rb:39:in `find_by_sql'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/relation.rb:706:in `exec_queries'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/relation.rb:583:in `load'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/relation.rb:260:in `records'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/activerecord-5.0.6/lib/active_record/relation/delegation.rb:38:in `each'
/Users/Naekh/code/yoando/statuschecker/statuschecker/lib/tasks/fb_ping.rake:5:in `block (2 levels) in <top (required)>'
/Users/Naekh/code/yoando/statuschecker/statuschecker/lib/tasks/rake_them_all.rake:4:in `block (2 levels) in <top (required)>'
/Users/Naekh/.rvm/gems/ruby-2.4.1/gems/rake-12.1.0/exe/rake:27:in `<top (required)>'
/Users/Naekh/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `eval'
/Users/Naekh/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `<main>'

Is this a naming issue ? I check and double check but did I miss something?

my environment is development.

Here is my schedule.rb:

set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"}
every 2.minutes do
  rake 'check:all', :environment => 'development'

and my schema:

ActiveRecord::Schema.define(version: 20170922073721) do
  enable_extension "plpgsql"
  create_table "pings", force: :cascade do |t|
    t.boolean  "up"
    t.integer  "service_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["service_id"], name: "index_pings_on_service_id", using: :btree
  create_table "services", force: :cascade do |t|
    t.string   "name"
    t.string   "web_api"
    t.string   "json_path"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  add_foreign_key "pings", "services"

here is my models: - service.rb

class Service < ApplicationRecord
  has_many :pings, dependent: :destroy
  validates :name, uniqueness: true, presence: true
  validates :web_api, uniqueness: true, presence: true
  • ping.rb

    class Ping < ApplicationRecord belongs_to :service I'd assume that the cron job created by whenever does not have access to the same set of gems (?altough it should fail normally, then?). Can you try to run the rake task with the same bash the cron job uses? – ulferts Sep 25, 2017 at 8:39 @ulferts I runned : /bin/bash -l -c 'cd /Users/Naekh/code/yoando/statuschecker/statuschecker && RAILS_ENV=production bundle exec rake check:all --silent >> log/cron_log.log 2>> log/cron_error_log.log' in my terminal and got the same error – Doge Sep 25, 2017 at 8:50 Did you make sure migrations were run in your development environment? bundle exec rake db:migrate RAILS_ENV=development because your cron job is using development. Also can you post the rake task? Is it using same environment – lacostenycoder Sep 25, 2017 at 8:58

    Cron is sending this:

    /bin/bash -l -c 'cd /Users/Naekh/code/yoando/statuschecker/statuschecker && RAILS_ENV=production bundle exec rake check:all --silent >> log/cron_log.log 2>> log/cron_error_log.log'
    

    As you can see, he is sending it to the production environment, even if in my schedule:

    set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"}
    every 2.minutes do
      rake 'check:all', :environment => 'development'
    

    I'm telling him to send it to the development environment. I need now to find a way to force him to go in that environment as when i run:

    /bin/bash -l -c 'cd /Users/Naekh/code/yoando/statuschecker/statuschecker && RAILS_ENV=environment bundle exec rake check:all --silent >> log/cron_log.log 2>> log/cron_error_log.log'
    

    it works

    I updated cronjob environment with:

    whenever --update-crontab --set environment='development'
    

    and now it's working smoothly

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •