简体   繁体   English

创建Rails会话后启动间隔任务(用户登录)?

[英]Start an interval task upon creating a Rails session (user logging in)?

I want to basically create a scheduled task upon the user logging in. What Rails utility, gem, etc does this? 我基本上想在用户登录时创建一个计划任务。这是什么Rails实用程序,gem等? Basically the Rails server needs to alert the browser user every two minutes, based on a set of conditions. 基本上,Rails服务器需要根据一组条件每两分钟向浏览器用户发出一次警报。 This task has to end on sessions#destroy 此任务必须在会话上结束#destroy

In Sessions Controller 在会话中控制器

def new
 @user = User.authenticate(params[:session][:email], params[:session]  [:password])
if @user 
 session[:@user_id] = @user.id
 #how do i create a scheduled task for this session? I have a #current_user Application Controller method that will find #sessions[:@user_id] and identify it as the current user.

end
end


def destroy

sessions[:@user_id] = nil
#need to end scheduled task here.

end

To accomplish what you want in Rails, you could use a Thread . 要完成Rails中想要的功能,可以使用Thread

# create thread

my_thread = Thread.new do

  # do some junk

  # put thread to sleep for 120 seconds

  sleep(120)
end

# nuke the thread on session#destroy

my_thread.kill

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM