Running timed job

不羁岁月 提交于 2019-12-22 16:12:28

问题


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?


回答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).




回答2:


Use an existing solution...

http://quartznet.sourceforge.net/




回答3:


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




回答4:


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...



来源:https://stackoverflow.com/questions/4519910/running-timed-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!