【问题标题】:Why does my rake task succeed through CLI but fail when run through "whenever"为什么我的 rake 任务通过 CLI 成功但通过“无论何时”运行时失败
【发布时间】:2018-07-07 10:07:33
【问题描述】:

我正在尝试每天发出 Trello API 请求。我使用whenever 进行任务调度,使用ruby-trello 作为Trello API 客户端。

当我运行 $ rake trello:chores 时,一切都按预期运行。授权请求被发送到 Trello,API 调用成功。当在 whenever 调度上运行相同的任务时,会出现以下错误结果:

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

【问题讨论】:

  • 听起来像是来自here 的配置错误您是否正确设置了自述文件here 中描述的所有配置?您还需要在您的连接方法中设置 oauth_token_secret 吗?您还可以在 rake 任务中使用 puts 语句,并查看 ENV["TRELLO_API_KEY"] 和 ENV["TRELLO_CLIENT_SECRET"] 设置是否正确。
  • ENV["TRELLO_API_KEY"]ENV["TRELLO_CLIENT_SECRET"]换成真实值可以运行吗?
  • @Tai 是的,似乎访问环境变量是问题所在。当我对密钥和 api 密钥进行硬编码时,任务成功运行。
  • 你如何设置那些环境变量?
  • @Tai 只是在这里为未来的观众留下一个回应 - 环境变量是通过 direnv 设置的,变量放在根目录的 .envrc 中。感谢您快速准确的诊断!

标签: ruby-on-rails ruby whenever trello


【解决方案1】:

尝试在 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

【讨论】:

  • 这是关键 - 添加 2 env :TRELLO... 行以允许环境变量从 rake 任务中持续存在。谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-02-10
  • 2012-02-17
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 2011-04-18
  • 2011-03-05
相关资源
最近更新 更多