2017-09-24 109 views
0

我每遇到一些麻煩。我創建了完美運行rake任務,當我啓動它自己:ActiveRecord :: StatementInvalid:PG :: UndefinedTable:錯誤:關係

namespace :check do 
    desc "check all services" 
    task :all do 
     Rake::Task["fb_ping:check"].invoke 
     Rake::Task["lithium_ping:check"].invoke 
     Rake::Task["algolia_ping:check"].invoke 
    end 
end 

但是當它經過時:

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

我得到了我的日誌以下回報:

rake aborted! 
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>' 

我不知道如何獨立工作的東西可以崩潰時,每當調用

// Here是我的模式:

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 
    end 

    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 
    end 

    add_foreign_key "pings", "services" 
end 

這裏是一個ping檢查(fb_ping.rake),我在我的其他測試中使用了相同的語法。當我手動啓動所有這些工作:

namespace :fb_ping do 
desc "New fb ping" 
task check: :environment do 

    Service.all.each do | s | 
    if s.name == 'Facebook' 
     if FacebookStatusService.status === 1 
     Ping.create(up: true, service: s) 
     puts 'FB service is up' 
     else 
     Ping.create(up: false, service: s) 
     puts 'FB service is down' 
     end 
    end 
    end 
end 

+0

這有可能是耙invoke方法試圖使用您的測試環境。 https://stackoverflow.com/questions/36125355/custom-rake-task-for-db-table-not-found – AytanLeibowitz

+0

你可以發佈'db/schema.rb'的內容嗎? – garythegoat

+0

@garythegoat我更新了我的帖子(感謝您的幫助) – Doge

回答

0

If a :path is not set it will default to the directory in which whenever was executed. :environment_variable will default to 'RAILS_ENV'. :environment will default to 'production'. https://github.com/javan/whenever

動態調整的環境中,你需要通過在飛行中的變量。 https://github.com/javan/whenever/wiki/Setting-variables-on-the-fly

作爲第一個測試,嘗試硬編碼當前的環境下,我假設在下面的例子中發展:

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

更新我的文章我剛剛嘗試過,並且在我的錯誤日誌中有相同的消息。我運行一個檢查,看看我是不是在測試中而不是在控制檯(Rails.env)中進行開發,並且我正在開發 – Doge

+0

如果這看起來很愚蠢,我很抱歉,但是您是否嘗試過重新啓動您的rails控制檯? – AytanLeibowitz

+0

是的,我做了:) 這並不愚蠢。我很感激幫助 – Doge

相關問題