2016-08-09 121 views
1

我正在構建基於Rails的物聯網平臺,其中某些設備在特定的預定時間被操縱。但是,由於Heroku的內置調度庫似乎只能以固定的時間間隔支持回調,因此我在特定時間難以執行操作。我的服務器需要在任意時間撥打電話,而無需再次重複。 Rails中是否有任何其他寶石或方法可用於此功能?如何在特定時間運行一個動作 - Rails Heroku服務器?

回答

0

您可以使用delyed job寶石。利用run_at字段在特定時間運行您的任務。
以下是一個示例

class LongTasks 
    def send_mailer 
    # Some other code 
    end 
    handle_asynchronously :send_mailer, :priority => 20 

    def in_the_future 
    # Some other code 
    end 
    # 5.minutes.from_now will be evaluated when in_the_future is called 
    handle_asynchronously :in_the_future, :run_at => Proc.new { 5.minutes.from_now } 

    def self.when_to_run 
    2.hours.from_now 
    end 

    class << self 
    def call_a_class_method 
     # Some other code 
    end 
    handle_asynchronously :call_a_class_method, :run_at => Proc.new { when_to_run } 
    end 

    attr_reader :how_important 

    def call_an_instance_method 
    # Some other code 
    end 
    handle_asynchronously :call_an_instance_method, :priority => Proc.new {|i| i.how_important } 
end 
相關問題