如何安排一个函数在未来的时间执行? [英] How to schedule a function to execute at a future time?

查看:43
本文介绍了如何安排一个函数在未来的时间执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rails 应用程序,其中用户创建了一个名为 rooster 的记录,并将名为on"的布尔值设置为 true.在 Rails 控制台中,它看起来像:

I have a rails application in which a user creates a record called a rooster with a boolean named "on" set to true. In rails console it looks like:

Rooster 
=> Rooster{:id integer, :user_id integer, :created_at datetime, :updated_at datetime, :on boolean} 

现在,经过一段时间(例如 10 小时)后,我需要应用程序自动将该布尔值翻转为false"(用户不执行任何操作).

Now, after some period of time (say, 10 hours) I need the app the automatically flip that boolean to "false" (the user doesn't do anything).

很简单的想法.那么,如何在 rails 3 中创建一个在一段时间后执行的方法?

Pretty simple idea. So, how do I create a method in rails 3 that executes after some time period has elapsed?

推荐答案

使用 DelayedJob 你可以做类似的事情

With DelayedJob you can do something like

  def in_the_future
   # Some other code
  end
  handle_asynchronously :in_the_future, :run_at => Proc.new { 10.hours.from_now }

很好吗?:) 我建议你阅读 jared 链接的文档

Very nice? :) I suggest you read the documentation as linked by jared

这篇关于如何安排一个函数在未来的时间执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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