Running Methods at Equal Intervals

As you know, one of the processes in my application is the Ticket Scheduler process. This process will look at the scheduling configuration and inject request tickets into the ticket queue; they will then be dispatched by the Ticket Dispatcher process. What I want to do here is basically implement a process that behaves like the UNIX cron daemon, processing tickets at predefined intervals of time.

So, for example, I may have a sensor check that I want to probe every 5 minutes. I then need this process to inject the appropriate ticket into the queue every 5 minutes. The algorithm that I am going to implement has the following steps:

• Wake up at predefined intervals of time. In our example the shortest interval is 1 minute.

• Find all rules that are supposed to be triggered at the minute.

• Insert appropriate records into the tickets queue.

I therefore need a mechanism to "wake-up" at a given interval of time. I could use the time. sleep() function, which allows me to pause execution for any number of seconds, but that would sacrifice accuracy, because the other code (finding rules and inserting tickets) also takes some time, so if I set my thread to sleep for 60 seconds, and the execution time is 1 second, the total time period will be 61 seconds. I might measure the execution time and then call the sleep function only for 60 seconds minus the execution-time interval, but measuring and subtraction calls are also going to consume time, so that will not be as accurate, either.

What I need is a mechanism that sends a signal to my process, and the process waits for the signal. When the signal is received, the process performs whatever is needed and then waits for the signal again.

Was this article helpful?

0 0

Post a comment