Cron基础的任务,存储在数据库中

huangapple go评论45阅读模式
英文:

Cron based jobs, which are in Database

问题

我正在尝试编写一个进程,它从数据库中存储的一组记录中读取cron表达式,并在下一个小时内触发时运行作业(执行一个程序)。数据库中具有cron表达式的记录可以具有不同的触发时间(如每周五或每小时等)。

具有cron表达式的表格示例:

----------

0 0 12 * * ?   , 12中午, AJOB
----------

0 11 11 11 11 ?  , 十一月11日, BJOB
----------

0 15 10 * * ? , 每天10:15, XJOB
----------

用户可以更新此表格中的cron表达式。

如何设计这种类型的应用程序最佳?我看到的主要问题是:假设我每隔1小时运行一次我的作业,并且获取计划在接下来1小时内的记录并运行我的作业,当应用程序运行时,这看起来都很好。如果应用程序宕机了2小时,我们可能会错过一些需要在那个时间触发的作业。

如何编写这种类型的应用程序,考虑到应用程序可能会失败,但在宕机期间我们不应该错过任何cron表达式?

cron API还具有nextTriggerTime,但在上一个触发时间上支持较少。

英文:

I am trying to write a process, which reads the cron expression from a set of records stored in database and runs a job (execute a program if that expression triggers in the next one hour). The records with cron expression in the database can have different triggering times (like Friday or hourly etc).

Example of the table with cron expressions.

----------

0 0 12 * * ?   , 12Noon, AJOB
----------

0 11 11 11 11 ?  , Nov 11, BJOB
----------

0 15 10 * * ? , EveryDay 10: 15, XJOB
----------

Users can update this crons in the table.

What's the best way to design this kind of application?

The major problem I see here is the following: let's say I ran my job every 1 hour and take the records which are scheduled in the next one hour and run my job, This looks all good when the application is up. If the application is down for 2 hours we might miss some jobs which need to triggered at that time.

How to write this kind of application keeping in mind that the application can fail, but we should not miss any crons during the downtime?

The cron API also has nextTriggerTime, but much less support on previous trigger time.

答案1

得分: 2

自Akka是一个问题标签,我假设你正在寻找一些基于Akka的解决方案。

对于在JVM中的类似cron的作业调度器,也许可以直接使用Quartz(正如Ashiq建议的),它是Java,并且应该很容易集成到Scala+Akka项目中。另外,可以查看akka-quartz-scheduler。它与Akka集成得很好,并提供了cron工具。

关于应用程序:

  1. 在DB或磁盘上维护一个检查点表,以指示“如果cron作业ID=X已运行”。
  2. 您的应用程序在启动时将所有cron表达式加载到内存中,触发所有这些表达式的Quartz调度器。如果应用程序不崩溃,它将继续运行。
  3. 每当您的应用程序成功运行cron作业时,更新检查点表。
  4. 如果您的应用程序崩溃并重新启动,请首先查看检查点表以了解它的运行情况,并调度尚未完成的作业。
英文:

Since Akka is one of the question tags, I assume you're looking for some Akka based solution.

For cron-like job scheduler in JVM, maybe go straight with Quartz (as suggested by Ashiq), it is Java and should be straightforward to integrate into a Scala+Akka project. Also, take a look at akka-quartz-scheduler. It integrates with Akka well and it provides the cron utils.

In terms of the application:

  1. Maintain a checkpoint table in DB or disk, whichever fits better, to indicate "if cron job ID=X has been run"
  2. Your app loads all cron expressions into memory on startup, trigger quartz scheduler on all of them. If the app doesn't crash, it'll keep running.
  3. Every time your app runs a cron job successfully, update the checkpoint table.
  4. If your app crashes and restarts, lookup the checkpoint table first to understand where it has been, and scheduler the yet-to-finished jobs.

答案2

得分: 0

你可以使用Quartz,这是一个可以集成到Java中的作业调度库。它可以存储和触发cron作业和简单作业。它还具有错过策略,您可以在任何触发器被错过的情况下利用它们。

英文:

You can use Quartz which is a job scheduling library that can be integrated to Java. It can store and trigger cron jobs and simple jobs. It also has misfire policies which you can utilise in case any of the triggers are missed.

huangapple
  • 本文由 发表于 2020年7月31日 02:01:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63178815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定