Rails根据用户输入制作新的cron作业 [英] Rails making new cron jobs based on user input

查看:69
本文介绍了Rails根据用户输入制作新的cron作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想根据用户触发操作的时间每两周调用一次操作.我想令人困惑的是为什么似乎没有一种直接的方法.

In my application, I want to invoke an action every two weeks based on when the user triggered an action. I guess what's confusing is why there doesn't seem to be a straight forward way of doing this.

理想情况下,重复的工作将在模型中设置为 ,而不是其他一些文件.例如,whenever宝石具有以下说明:

Ideally, the repeated job would be set in the model, not some other file. For example, the whenever gem has these instructions:

开始使用

$ cd /apps/my-great-project

$ wheneverize .

这将为您创建初始的config/schedule.rb文件.

This will create initial config/schedule.rb file for you.

但是我不想把时间表放在那儿.我希望日程安排在我的模型中.时间表不是由我设置的,而是由我的用户设置的.

But I don't want to put my schedules in there. I want the schedule to be in my model. The schedule isn't being set by me, it's being set by my users.

有没有简单的方法来实现这一目标?我已经为此困扰了几个星期.

Is there any straightforward way of implementing this? I've been stumped on this for weeks.

推荐答案

您可以使用延迟工作.

http://asciicasts.com/episodes/171-delayed-job

您的用户可以定义存储在模型中的作业.您将创建第一个延迟的作业,在完成每个作业之后,您可以开始一个新的延迟的作业.

Your users could define jobs that are stored in your model. You would create the first delayed job and after each completed job you can start a new delayed job.

如果您的用户创建了一个应在凌晨3:00运行的作业,而他在下午3:00保存了该作业,则您需要创建第一个作业:

If your user creates a job that should run at 3:00 am and he saves that job at 3:00 pm you need to create the first job:

Delayed::Job.enqueue(CronJob.new(params[:id]), 1, 12.hours.from_now ) 

在开始这项工作之前,您将创建下一个工作

Before starting this job you would create tne next job

Delayed::Job.enqueue(CronJob.new(params[:id]), 1, 1.days.from_now )

这篇关于Rails根据用户输入制作新的cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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