如何定义用户申请某个职位的频率?
我有一个必须重复启动作业的应用程序。但是(是的,如果没有但是的话,这会很容易......)我希望用户在应用程序中定义他们的备份频率。
在最坏的情况中,他们必须选择:
- 每周、
- 每天、
- 每 12 小时、
- 每 6 小时、
- 每小时
在最好的情况中,他们应该能够使用 crontab 表达式(例如,请参阅文档)
如何做到这一点?我是否每分钟启动一个作业来检查上次执行时间、频率,然后在需要时启动另一个作业?我是否创建了一种将由 masterjob 执行的队列?
欢迎任何线索、想法、意见、最佳实践、经验!
编辑:使用 Akka 调度程序解决了这个问题。好吧,这是一个技术解决方案,而不是设计答案,但一切仍然很好。
每个用户定义的重复都是一个参与者,每个周期都会向新参与者发送消息以执行实际作业。
I have an application that has to launch jobs repeatingly. But (yes, that would have been to easy without a but...) I would like users to define their backup frequency in application.
In worst case, they would have to choose between :
- weekly,
- daily,
- every 12 hours,
- every 6 hours,
- hourly
In best case, they should be able to use crontab expressions (see documentation for example)
How to do this? Do I launch a job every minutes that check for last execution time, frequency and then launches another job if needed? Do I create a sort of queue that will be executed by a masterjob?
Any clues, ideas, opinions, best pratices, experiences are welcome!
EDIT : Solved this problem using Akka scheduler. Ok, this is a technical solution not a design answer but still everything works great.
Each user defined repetition is an actor that send messages every period to a new actor to execute the actual job.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的要求/架构,可能有两种方法可以做到这一点:
如果您只能使用 Play:
当然,用户的 cron 不会太精确,因为您必须考虑自己的 cron 延迟加上队列中所有任务的执行延迟,这些任务将按顺序运行。这不是最好的方法,除非您以某种方式禁止每秒运行一次或更频繁地运行的 cron(为了安全起见)。检查 cron 的执行时间,如果它们超过一定时间则将其杀死,这将是一个好主意。
如果您可以使用的不仅仅是 Play:
我认为更好的选择是使用 Quartz(请参阅 this)在用户创建作业时创建未来的执行,并在执行结束后重新编程。
There may be two ways to do this depending on your requirements/architecture:
If you can only use Play:
Of course, the crons of the users will not be too precise, as you have to account for you own cron delays plus execution delays on all the tasks in queue, which will be run sequentially. Not the best approach, unless you somehow disallow crons which run every second or more often than every minute (to be safe). Doing a check on execution time of the crons to kill them if they are over a certain amount of time would be a good idea.
If you can use more than Play:
The better alternative I believe is to use Quartz (see this) to create a future execution when the user creates the job, and reproram it once the execution is over.
谷歌群组上对此进行了讨论。据我记得,您必须定义一个每 6 小时启动一次的作业,并检查必须完成哪些备份。因此,您必须记住上次备份作业何时完成并自己进行控制。我不确定 Quartz 是否可以满足这样的要求。
我查看了源代码(总是一个很好的源;-))并找到了一个方法,我认为这应该是你想要的。但我不确定这是否是一个聪明的设计,因为如果你有 1000 个用户,你就会有 1000 个工作机会。我不确定 Play 是否是为了处理如此大量的工作而构建的。
[更新] 对于 cron 表达式,您应该查看
JobPlugin.scheduleForCRON()
There was a discussion on google-groups about it. As far as I remember you must define a job which start every 6 hours and check which backups must be done. So you must remember when the last backup job was finished and make the control yourself. I'm unsure if Quartz can handle such a requirement.
I looked in the source-code (always a good source ;-)) and found a method every, where I think this should be do what you want. How ever I'm unsure if this is a clever design, because if you have 1000 user you will have then 1000 Jobs. I'm unsure if Play was build to handle such a large number of jobs.
[Update] For cron-expressions you should have a look into
JobPlugin.scheduleForCRON()
有几种方法可以解决这个问题。
如果您没有大量的作业,我只需使用所需的灵活性将它们保存到表中。然后每小时(或您支持的最低间隔)检查所有这些,并运行那些符合条件的。简单的。
或者,如果您更喜欢使用 cron 语法,只需使用回调到正在运行的应用程序的包装器将作业写入(导出)到用户 crontab,或者如果可能的话,在独立进程中启动作业。
There are several ways to solve this.
If you don't have a really huge load of jobs, I'd just persist them to a table using the required flexibility. Then check all of them every hour (or the lowest interval you support) and run those eligible. Simple.
Or, if you prefer to use cron syntax anyway, just write (export) jobs to a user crontab using a wrapper which calls back to your running app, or starts the job in a standalone process if that's possible.