Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Time Drift when using Win32::Daemon::Simple

by rah (Monk)
on Apr 17, 2003 at 15:12 UTC ( [id://251226]=perlquestion: print w/replies, xml ) Need Help??

rah has asked for the wisdom of the Perl Monks concerning the following question:

I have a service which I built using Win32::Daemon::Simple. Thanks to the many monks who helped me with that effort. Now I have a relatively small but annoying problem with the daemon's callback sub. I have set up a 1 minute interval. However, the interval slows by about 1 second each interval. The result is that an event I want to happen on a particular minute may get skipped as the callback goes from 00:59 to 02:00. What I'm wondering is if there is a way to slew the daemons timer so that it is back in synch?

I know that there is the option of runnning the loop outside of the daemon, but if I wanted to do that, I wouldn't need the daemon to begin with. I like the clean implementation using the daemon, as my code only has to do its job, and needn't worry about "housekeeping". Is there a way to nudge the daemon back in line?

  • Comment on Time Drift when using Win32::Daemon::Simple

Replies are listed 'Best First'.
Re: Time Drift when using Win32::Daemon::Simple
by Jenda (Abbot) on Apr 17, 2003 at 15:46 UTC

    No currently there is no way to tell ServiceLoop() to skip a few seconds to get back in sync. The INTERVAL is rather "sleep this long between actions" than "do this action each N minutes". It was easier to do it that way and I did not care that the action doesn't happen once every ten minutes, but once every ten minutes and one second.

    I will add an option to Win32::Daemon::Simple that will instruct ServiceLoop() to tick in even intervals. Please don't hold your breath.

    Actually I do have a service that acts as a scheduler and starts different subtasks at different scheduled times (each 30 minutes, at 15:40 each day, at 1:30 each Sunday, etc.) which seems to be close to what you are trying to do. I guess I should extract that functionality into a module. Win32::Daemon::Extended I guess ;-)

    Anyway for now you will need to do something like this:

    my $next_time = time()*60; while(1) { DoEvents; sleep(1); if (time() >= $next_time) { $next_time += 60; your_callback(); } }
    (I'll probably do something a little more complex for the ServiceLoop() in "exact" mode to decrease the number of time() calls, but I don't think it matters here.)

    HTH, Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      Not sure I follow (been a long couple of days). Do you mean replace the daemon's interval call back with this timing loop? I ran a simple test and it does seem to stay accurate to the second.

      The other approach I had considered was to test for how much the timer had drifted at safe periods, then pause and resume the service to get back on synch. It doesn't elimate the problem, but allows me to specify where it skips ahead to the next minute. This would eliminate the risk of my missing a significant time period.

        1. Yes. Just replace the

        ServiceLoop(\&callback);
        with the loop.

        2. I don't think I follow, you mean that your callback would wait 59 seconds in case the time drifted to put it back in sync? Well you can do that. Apart from the fact that you will not be able to stop the service (via the Service Manager) during those 59 seconds you are safe.

        Jenda
        All those who understood the topic I just elucidated, please verticaly extend your upper limbs.
           -- Ted Goff

Re: Time Drift when using Win32::Daemon::Simple
by particle (Vicar) on Apr 17, 2003 at 15:42 UTC

    i don't understand precisely how you're timing this daemon. perhaps you should look to see if at least one minute has passed since the last run. if this is what you're doing, i don't see how it's failing without a working code example.

    hope that helps.

    ~Particle *accelerates*

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://251226]
Approved by sschneid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-12-12 18:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (65 votes). Check out past polls.