为什么我的rake任务通过CLI获得成功但是在#34;每当"

时间:2018-01-28 17:01:47

标签: ruby-on-rails ruby whenever trello

我试图每天发出Trello API请求。我使用whenever进行任务调度,使用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

1 个答案:

答案 0 :(得分: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