Heroku Scheduler - 设置日常任务 [英] Heroku Scheduler - set up daily task

查看:167
本文介绍了Heroku Scheduler - 设置日常任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解Heroku的调度程序文档。

I'm having trouble understanding Heroku's scheduler documentation.

他们将此作为示例代码,但我不确定它是什么:

They give this as example code, but I'm not sure what's what:

desc "This task is called by the Heroku scheduler add-on"
task :update_feed => :environment do
  puts "Updating feed..."
  NewsFeed.update
  puts "done."
end

task :send_reminders => :environment do
  User.send_reminders
end

我希望能够使用这个调度程序每天从我的NotificationsController调用方法。我已经尝试修改它们的代码:

I am hoping to use this scheduler to call method from my NotificationsController on a daily basis. I've tried modifying their code to:

desc "This task is called by the Heroku scheduler add-on"
task notify: :environment do
  Notifications.notify
end

我的notifications_controller .rb看起来像:
注意包含了Twilio API的集成...这也将在重构之中,但它确实可以在测试中使用。

My notifications_controller.rb looks like: Notice integration of Twilio's API is included...also this will be refactored down the road, but it does work in testing.

class NotificationsController < ApplicationController
    require 'twilio-ruby'

    skip_before_action :verify_authenticity_token
    before_action :set_twilio_client

    $numbers = []
    User.all.select {|user| $numbers << user.phone_number}


    def daily_text
        $daily_text = ["Test 1", "Test 2", "Test 3"]
        $daily_text.sample
    end

    def notify
        message = @client.messages.create(
            from: '+1401XXXXXXX',
            to: $numbers,
            body: daily_text
            )

        render plain: message.status
    end

    private

    def set_twilio_client
        twilio_account_sid = ENV["TWILIO_ACCOUNT_SID"]
        twilio_auth_token = ENV["TWILIO_AUTH_TOKEN"]
        @client = Twilio::REST::Client.new twilio_account_sid, twilio_auth_token
    end

end

也许我需要将某些内容移至模型级别?任何指导都是值得赞赏的。

Maybe I need to move some stuff up to model level? Any guidance is appreciated.

推荐答案

我重构了大部分东西并将它们转移到TwilioClient模型中:

I refactored and moved most things into a TwilioClient model:

class TwilioClient
    require 'twilio-ruby'

    def initialize
        @client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]
    end

    def notify
        @client.messages.create(
            from: '+1401XXXXXXX',
            to: all_phone_numbers,
            body: daily_text
        )
    end

    private

    def daily_text
        ["Test 1", "Test 2", "Test 3"].sample
    end

    def all_phone_numbers
        User.pluck(:phone_number)
    end

end

通知Controller现在看起来像:

Notifications Controller now looks like:

class NotificationsController < ApplicationController
    skip_before_action :verify_authenticity_token

    def notify
        client = TwilioClient.new
        result = client.notify
        render plain: result.status
    end
end

和佣金任务:

and rake task:

desc "This task is called by the Heroku scheduler add-on"
task notify: :environment do
  client = TwilioClient.new
  client.notify
end

这篇关于Heroku Scheduler - 设置日常任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆