簡體   English   中英

為什么我的rake任務通過CLI成功但在“ everyever”運行時失敗

[英]Why does my rake task succeed through CLI but fail when run through “whenever”

我正在嘗試每天發出Trello API請求。 我使用的是每當進行任務調度和紅寶石trello作為Trello API客戶端。

當我運行$ rake trello:chores一切都會按預期運行。 授權請求將發送到Trello,並且API調用成功。 在按計划執行相同任務時,將出現以下錯誤:

rake aborted!
Trello::ConfigurationError: Trello has not been configured to make authorized requests.
/Users/matt/.rvm/gems/ruby-2.4.1/gems/ruby-trello-2.0.0/lib/trello/authorization.rb:11:in `authorize'
/Users/matt/.rvm/gems/ruby-2.4.1/gems/ruby-trello-2.0.0/lib/trello/client.rb:88:in `invoke_verb'
/Users/matt/.rvm/gems/ruby-2.4.1/gems/ruby-trello-2.0.0/lib/trello/client.rb:19:in `get'
/Users/matt/.rvm/gems/ruby-2.4.1/gems/ruby-trello-2.0.0/lib/trello/client.rb:44:in `find'
/Users/matt/.rvm/gems/ruby-2.4.1/gems/ruby-trello-2.0.0/lib/trello/list.rb:27:in `find'
/Users/matt/code/pi/lib/tasks/scheduler.rake:24:in `get_trello_tasks'
/Users/matt/code/pi/lib/tasks/scheduler.rake:19:in `block (2 levels) in <top (required)>'
/Users/matt/.rvm/gems/ruby-2.4.1/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
/Users/matt/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `eval'
/Users/matt/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `<main>'
 Tasks: TOP => trello:chores
(See full trace by running task with --trace)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


# lib/tasks/scheduler.rake:

namespace :trello do

  task :chores => :environment do
    puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
     require "#{Rails.root}/app/controllers/concerns/trello_api"
     include TrelloAPI
     connect
     set_objects

     today = Date.today
     week_of_year = today.strftime('%V')
     day_of_month = today.strftime('%d')
     day_of_week = today.strftime('%u')

     get_trello_tasks(today, "monthly") if day_of_month == "01"
     get_trello_tasks(today, "weekly") if day_of_week == "1"
     get_trello_tasks(today, "annual")
   end
end

def get_trello_tasks(today, frequency)
  list = Trello::List.find(@trello_objects[frequency])
  list.cards.each do |card|
    options = {
      :list_id => @trello_objects["chore_list"],
      :source_card_id => card.attributes[:id],
      :source_card_properties => "labels",
      :pos => "bottom"
    }
    if frequency == "annual"
      if card.attributes[:due].present?
        if card.attributes[:due].strftime('%m-%d') == Date.today.strftime('%m-%d')
        Trello::Card.create(options)
        end
      end
    else
      Trello::Card.create(options)
    end
  end
end


# config/schedule.rb:

set :chronic_options, :hours24 => true
set :output, "log/cron_log.log"
env :PATH, ENV['PATH']

every 1.minute do
  rake "trello:chores", :environment => "development"
end


# app/controllers/concerns/trello_api.rb:

module TrelloAPI
  extend ActiveSupport::Concern

  ... some other methods used in controllers

  private
    def connect
      require 'trello'
      Trello.configure do |config|
        config.consumer_key = ENV["TRELLO_API_KEY"]
        config.consumer_secret = ENV["TRELLO_CLIENT_SECRET"]
        config.oauth_token = User.first.trello_auth_token
      end
    end

    def set_objects
      @trello_objects = {
        "annual" => "******************",
        "backlog" => "******************",
        "board" => "******************",
        "chore_list" => "******************",
        "monthly" => "******************",
        "persistent" => "******************",
        "quarterly" => "******************",
        "semi-annual" => "******************",
        "weekly" => "******************"
      }
      return @trello_objects
    end
end

嘗試在schedule.rb中設置環境變量:

# config/schedule.rb:

set :chronic_options, :hours24 => true
set :output, "log/cron_log.log"
env :PATH, ENV['PATH']
env :TRELLO_API_KEY, ENV['TRELLO_API_KEY']
env :TRELLO_CLIENT_SECRET, ENV['TRELLO_CLIENT_SECRET']

every 1.minute do
  rake "trello:chores", :environment => "development"
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM