开发者

Run a sub in a perl script based on the time?

I have a perl script that runs as a daemon looping all the time. I want to run a subfunction in a perl script that is based on the time (or timer) so every 2hrs it would run that subfunction and continue with it's loop. I'm thinking getting the epoch time and 开发者_C百科just checking it a few times through the loop and once it's greater then 2hrs it runs the subfunction. Is there a better way to do this in perl?

Thanks,

LF4


This depends on whether there should be 2 hours since the START of the last subroutine launch, or since the END of last execution.


1) If the latter (2 hours between the end of running the last subroutine and the start of new one), cespinoza's solution is perfectly acceptable (loop infinitely, and call sleep(7200); after executing the subroutine).

my $timeout = 7200;
while (1) {
    dostuff();
    sleep($timeout);
};

The only problem with this is that it can't handle the case where dostuff() takes forever, e.g. gets stuck - for the discussion of why it's an important situation to consider and approaches to solve, see below.


2) If the former (2 hours between starting points), you have three options, related to handling the subroutine run-time that exceeds 2 hours[0]. Your 3 options, explained in detail below, are to either:

2a) kick off a new subroutine while the old one keeps running (in parallel);

2b) to kick off a new subroutine AFTER the old one finishes;

2c) to kick off a new subroutine but first stop the execution of the prior one.

2a an 2c options require you to set an alarm() for 2 hours, and differ in what happens when an alarm gets triggered.

[0] NOTE: since any subroutine is likely to require at least SOME resources from the PC, there's always a - however small - chance that it would exceed 2 hours, so you have to pick one of those 3 options to handle such a scenario.


2a) Kick off every 2 hours, running in parallel with old execution if not finished.

This option is, essentially, implementing cron functionality.

Anytime you hear the word parallel, you would likely fork off the process.

my $timeout = 7200;
while (1) { # Not tested!
    eval {
        local $SIG{ALRM} = sub { die "alarm\n" };
        if (!defined($child_pid = fork())) {
            die "cannot fork: $!\n";
        } elsif (!$child_pid) { # Child
            dostuff();
            exit;
        } # Parent continues to sleep for 2 hours
        alarm $timeout; # You need it in case forking off take >2hrs
        sleep; # forever
    };
    die unless $@ eq "alarm\n"; # propagate unexpected errors
    # We don't need to check if $@ is true due to forever sleep
}

2b) Kick off every 2 hours, if the old one didn't finish, let it run till it finishes

This can be re-worded as "kick off task, if it finishes faster than 2 hours, sleep for the remainder"

my $timeout = 7200;
while (1) {
    my $start = time;
    dostuff();
    my $end = time;
    my $lasted = $end - $start;
    if ($lasted < $timeout) {  
        sleep($timeout - $lasted);
    }
};

2c) Kick off every two hours, if the previous one didn't finish, time it out and kill it

Whenever you see logic like this, alarm is obviously the answer.

while (1) {
    my $finished = 0;
    eval {
        local $SIG{ALRM} = sub { die "alarm\n" };
        alarm 7200;
        dostuff();
        $finished = 1;
        sleep; # forever
    };
    die unless $@ eq "alarm\n"; # propagate unexpected errors
    warn "Timed out!!!\n" unless $finished
}

P.S. As cespinoza noted, you need to somehow daemonize the script (ensure it doesn't get killed when you exit the shell that started it), by either Unix means (e.g. launching it as nohup) or Perlish means (search for daemonize + Perl on Stackoverflow for mechanics of that).


Something like crontab would be best to do a timed job like that. However, if you want to run a Perl daemon, you'll have to use some kind of event handler. Two choices off the top of my head are POE and AnyEvent.


You might want to check Schedule::Cron for task planning and execution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜