Scheduling Jobs - Optimal Design
I'm looking for an optimal design for the following usecase.
I'm building a system where users create开发者_开发知识库 entities with end date/time. I need to schedule a job to change the status of these entities to Expired as soon as the end time is reached.
These are the two solutions that i can think of
- A job runs every minute and runs a query to check for expired entities (endTime > sysdate). Problem: Load on Database, Running this query every minute may put load on the database.
- Schedule a job for each entity, as soon as the entity is created. Problem: Too Many Jobs will get created in system, 1000's of these entities will be created daily.
Is there any better solution than the above two ? How do people do this in general ?
A query every minute is not a very heavy load. I have an windows service that does the following:
Manages a schedule of voip sessions, starts them, ends them, starts recordings, ends recordings, clears recording space on the recording drive, displays OSD messages. Each of these tasks are in their own thread with a timer and run their query about every 4 seconds. There are over 100,000 records in the database and 100's of users of the web app that accesses this same database at a time. It runs fine and has been for about 3 years now.
Depending on your framework, there may be better options, but I haven't found any at the moment. For instance, .NET has the Task Parallel Library, which I haven't used yet, which I hear has some things to offer here. Anyways, I have been using approach #1 for years, and it has never done me wrong.
When you think about it, even an event based system is sitting in a loop somewhere with some function pointers. The question is whether or not you are manually managing it and how it is threaded. The db load should be insignificant.
I my opinion you can combine two ideas.
In every hour run a job that gets data . Now create jobs for each of the objects that expire in the next 60 mins only.
now the 60 mins time is just example.. change it to suit your needs.
精彩评论