简体   繁体   English

Rails Rufus 调度程序持久性

[英]Rails Rufus Scheduler Persistence

I have a rufus scheduler in my Rails app that I'm using to record/insert data every 5 minutes from 9:30AM to 4:00PM Mon-Fri.我的 Rails 应用程序中有一个 rufus 调度程序,用于在周一至周五上午 9:30 到下午 4:00 每 5 分钟记录/插入数据。 The method containing this action is fired as soon as user is registered.一旦用户注册,包含此操作的方法就会被触发。 BUT this logging job is intercepted and terminated once user logs out.但是,一旦用户注销,此日志记录作业就会被拦截并终止。

Could anyone help in figuring out how to persist the logging method even when the user logs out/session is destroyed?即使用户注销/会话被破坏,任何人都可以帮助弄清楚如何保留日志记录方法吗?

I am calling the logging method in my Users controller create method and I've set up the logging method in the User model.我在我的用户 controller 创建方法中调用日志记录方法,并且我在用户 model 中设置了日志记录方法。

Any help would be greatly appreciated!任何帮助将不胜感激!

That sounds convoluted.这听起来很复杂。 Why don't you use a single scheduled job that is run every 5 minutes from 9:30AM to 4:00PM Mon to Fri for every User registered in the system.为什么不为系统中注册的每个用户使用一个从周一到周五上午 9:30 到下午 4:00 每 5 分钟运行一次的计划作业。

do_log =
  lambda do
    User.where(status: 'active').each do |u|
      # do the insertion for the user...
    end
  end
scheduler.cron('30,35,40,45,50,55 9 * * mon-fri America/New_York', &do_log)
scheduler.cron('*/5 10-15 * * mon-fri America/New_York', &do_log)

The User.where(status: 'active') is Sequel, it's probably different in Rails, but I am sure you can translate and adapt to your setting and needs. User.where(status: 'active')是续集,它在 Rails 中可能有所不同,但我相信您可以翻译并适应您的设置和需求。

Thus, as soon as a User is registered in your system, the logging will begin for him, even if he's logged out.因此,一旦用户在您的系统中注册,即使他已注销,也会开始为他记录日志。

Yes, I know it's not a single job, it's two jobs, but that takes care of the "0930 to 1630" requirement.是的,我知道这不是一份工作,而是两份工作,但这可以满足“0930 到 1630”的要求。

Maybe that's not what you want, but you are not very clear in your question...也许这不是你想要的,但你的问题不是很清楚......

Update 2021-01-30 - 1416+0900更新 2021-01-30 - 1416+0900

@hurricanenara commented: @hurricanenara 评论道:

I'm not certain where the do_log would be called to act as an all-encompassing job that will be applied to all "active" users during the scheduled times我不确定在哪里调用 do_log 以充当一个包罗万象的作业,该作业将在预定时间内应用于所有“活动”用户

The do_log will be called '30,35,40,45,50,55 9 * * mon-fri America/New_York' and '*/5 10-15 * * mon-fri America/New_York' which corresponds to your requirement: do_log将被称为'30,35,40,45,50,55 9 * * mon-fri America/New_York''*/5 10-15 * * mon-fri America/New_York'对应于您的要求:

every 5 minutes from 9:30AM to 4:00PM Mon-Fri周一至周五上午 9:30 至下午 4:00 每 5 分钟一班

If it is easier to you, the do_log can be "forgotten" if writing如果对您来说更容易,则do_log可以在编写时“忘记”

scheduler.cron('30,35,40,45,50,55 9 * * mon-fri America/New_York') do
  User.where(status: 'active').each do |u|
    # do the insertion for the user...
  end
end
scheduler.cron('*/5 10-15 * * mon-fri America/New_York') do
  User.where(status: 'active').each do |u|
    # do the insertion for the user...
  end
end

As you can see, the do_log lambda avoids a repetition (Don't Repeat Yourself, DRY).如您所见, do_log lambda 避免了重复(不要重复自己,DRY)。

This method assumes that when a "User is registered" in your system, they get added to the database in a user table fronted by a User model.此方法假定当您的系统中的“用户已注册”时,它们会被添加到用户表中的数据库中,该用户表的前面是User model。 The do_log simply iterates over all the user whose status is "active" (not "left", or "revoked", or "whatever"), and records information for that particular user. do_log简单地遍历所有状态为“活动”(不是“离开”、“撤销”或“任何”)的用户,并记录该特定用户的信息。

Your problem description is vague.您的问题描述含糊不清。 I'm guessing that you want to log for each user, because you want to log the information "even when the user logs out/session is destroyed".我猜你想为每个用户记录,因为你想记录信息“即使用户注销/会话被破坏”。

I don't know what you mean by "persist the logging method".我不知道您所说的“坚持记录方法”是什么意思。 I'm not sure you mean "persist to disk", so my guess is that you mean "keep on running".我不确定你的意思是“坚持到磁盘”,所以我猜你的意思是“继续运行”。

It is your responsibility as a developer to clearly state your problem.作为开发人员,您有责任明确 state 您的问题。 Most of the time that effort at clearly stating the problem will yield the answer to you without requiring another developer to listen.大多数情况下,明确说明问题的努力将为您提供答案,而无需其他开发人员倾听。 See also Rubber Ducking .另请参阅橡胶回避

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

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