运行定时作业

发布于 2024-10-09 02:35:46 字数 584 浏览 5 评论 0原文

假设我有一个带有表的数据库,该表中的每条记录都对应于要执行的操作。该表有一个日期时间字段,其中存储应执行的下一时刻的操作。

该表正在由 Windows 服务读取,并且操作存储在数据结构中。每隔几分钟,服务就会读取该表,并将新操作合并到数据结构中,因此同时创建的操作不会被错过。

每个动作都有一定的重复间隔。当操作运行时,日期时间字段将更新到该操作的下一个时刻 应根据该间隔运行。

现在我想知道正确的行动方案:实际开始行动的首选方式是什么 它给定的时间?

假设我有一个应该在数据结构中 09:00 运行的操作,如何确保它在 09:00 触发? 我目前的想法是每分钟检查一次数据结构,将当前时间与计划操作的时间进行比较,如果匹配,则执行操作并根据其关联的时间间隔更新操作的执行时间。

作为一种数据结构,我倾向于按照队列的方式进行思考,因此我可以将下一个作业放在前面,而我只需要检查第一个作业,但是每次发现新操作时我都需要重新排列队列。

我的主要兴趣点是检查是否应该执行某个操作,然后将遵循适当的数据结构的好方法。表中的操作数量非常低,不会超过 25 个。我无法控制数据库,因此一切都应该在 C# 中以编程方式完成,以防您想知道。 有什么建议、经验或建议吗?

Say I have a database with a table, each record in this table corresponds with an action to be ececuted. This table has a datetime field in which the next moment the action should be executed is stored.

This table is being read by a windows servic and the actions are stored in a datastructure. Every few minutes the service reads the table and new actions are merged into the datastructure, so actions created in the meanwhile don't get missed out.

Each action has a certain repeat interval. When an action has run, the datetime field gets updated to the next moment it
should run based on that interval.

Now here's where I'm wondering about the correct course of action: what is the preferred way to actually start the action at
its given time?

Say I've an action that should be run at 09:00 in the datastructure, how do I ensure it getting triggered at 09:00?
What I currently have in mind is to check the datastructure every minute, compare the current time with the time the action is scheduled and if they match, execute the action and update the execute time of the action according to its associated interval.

As a datastructure I tend to think in the line of a queue, so I can have the next job in front and I only have to check the first, but then I need to rearrange the queue everytime new actions are found.

My main point of interest is a good approach for checking if an action should be executed, an appropriate datastructure will follow. The amount of actions in the table will be really low, no more then 25. I have no control over the database so everything should be done programmatically in C# in case your're wondering.
Any tips, experiences or points of advise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋千易 2024-10-16 02:35:46

假设粒度为 1 分钟。我会让一个线程经常轮询数据库,并更新要在下一分钟启动的事项列表。然后,一个单独的计时器线程可以随着分钟的流逝而启动该操作列表。

当您深入了解细节时,它会变得更加复杂,因为您可能还想严格控制与之关联的状态机(准备好、运行、完成等)。

Say the granularity is 1 minute. I'd have one thread poll the db frequently and update a list of things to kick off in the next minute. A separate timer thread could then just kick off that list of actions as the minute ticks over.

When you get into the details it gets a little more complicated, as you might want to keep tight reins on the state machine associated with it as well (prepared, running, complete etc).

呢古 2024-10-16 02:35:46

使用现有的解决方案...

http://quartznet.sourceforge.net/

Use an existing solution...

http://quartznet.sourceforge.net/

碍人泪离人颜 2024-10-16 02:35:46

SQL Agent 似乎是一个不错的选择,但您不会获得有关客户端应用程序的反馈”。

SQL Agent seems to be a good choice, but you'll not get feedback on the client app'.

若沐 2024-10-16 02:35:46

如果您有一个计时器实例作为数据结构的一部分,并安排它在该操作到期时运行一次,该怎么办?这样您就不必进行检查,并且一旦您将结构放入队列或列表中,您就知道它已计划运行。计时器将在单独的线程上触发,因此您的代码将异步运行到与数据库对话的部分...

what if you had a timer instance as part of the datastructure, and schedule it to run once when this action is due. this way you don't have to check, and as soon as you put your structure in the queue or list, you know that it's scheduled to run. the timer will fire on a separate thread, and so your code will run async to the part that talks to the db...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文