运行定时作业 [英] Running timed job

查看:61
本文介绍了运行定时作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

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

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.

我的主要兴趣点是检查是否应该执行操作的好方法,是否会遵循适当的数据结构.表中的操作数量会非常少,不会超过 25.我无法控制数据库,所以一切都应该在 C# 中以编程方式完成,以防万一.有什么提示、经验或建议吗?

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?

推荐答案

说粒度是 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).

这篇关于运行定时作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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