我该如何调用一个动态调度程序来调用一个方法来在rufus-scheduler和Rails中每天多次获取推文4

问题描述:

我需要在固定时间如1PM,3PM,9AM的一天中调用高音api .. 我使用twiter宝石和鲁弗斯调度我该如何调用一个动态调度程序来调用一个方法来在rufus-scheduler和Rails中每天多次获取推文4

这里是我的模型

class Feed < ActiveRecord::Base 

    def self.latest 
    order('created_at desc').take(500) 
    end 

    def self.pull_tweets 
    $client.search("#ukraine", since_id: maximum(:tweet_id), count: 10).take(10).each do |tweet| 

     create!(
     tweet_id: tweet.id, 
     content: tweet.text, 
     screen_name: tweet.user.screen_name, 
     ) 

    end 
end 

end 

这里是我使用的调度程序调用pull_tweets

class FeedsController < ApplicationController 

    def index 

    scheduler = Rufus::Scheduler.new 
    scheduler.schedule_every('60s') do 
     Feed.pull_tweets 
    end 
    @tweets = Feed.latest 
end 
end 

其WO控制器完美拍摄,但我如何设置多次像1AM,3PM,9AM,用于从用户输入安排任务?

+0

这很糟糕。你是否意识到你每次要求/提交时都会创建一个rufus调度器实例? – jmettraux 2014-10-26 22:16:33

+0

是的,我认为带调度程序实例的初始化程序可以解决问题。但是,请问您可以在这方面帮助我吗? – sukanta 2014-10-26 22:22:57

+1

阅读手册通常有帮助:https://github.com/jmettraux/rufus-scheduler#so-rails – jmettraux 2014-10-26 22:25:56

简化您的控制器:

class FeedsController < ApplicationController 
    def index 
    @tweets = Feed.latest 
    end 
end 

然后确保你有一个配置/初始化/ scheduler.rb,看起来像:

require 'rufus-scheduler' 

Rufus::Scheduler.singleton.every('60s') { Feed.pull_tweets } 

,让你有整个一个调度Ruby进程。

要回答你的问题:

require 'rufus-scheduler' 

Rufus::Scheduler.singleton.cron('0 1,9,15 * * *') { Feed.pull_tweets } 
    # pull tweets at 0100, 0900 and 1500 

你的代码是每一个浏览器获得时间/供稿创建鲁弗斯调度实例,代码在这里有一个单一的调度。

+1

谢谢你。把它放在一起 – sukanta 2014-10-26 22:33:51

+0

你会接受我的回答吗?提前致谢。 – jmettraux 2014-10-26 22:34:33

+0

如果我需要任何帮助,我想我可以敲你。 – sukanta 2014-10-26 22:37:51