为什么我的 rake 任务通过 CLI 成功,但在 运行 到 "whenever" 时失败

Why does my rake task succeed through CLI but fail when run through "whenever"

我正在尝试每天向 Trello API 发出请求。我正在使用 whenever for task scheduling and ruby-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