Skip to content

Watchdog timers #255

@danepowell

Description

@danepowell

I have a periodic timer that polls an external service every 5 seconds. When it sees a certain response it should cancel the timer and print the result.

But obviously I don't want that timer to run forever, so I've also created a watchdog timer that should cancel both timers after 45 minutes.

The problem is both timers need to be able to cancel each other, and it becomes a bootstrap problem. If I define it exactly as I've described above, the program will always run for 45 minutes because the pollingTimer has no way to reference the watchdogTimer.

Something like this, based on the example in the README:

$loop = React\EventLoop\Loop::get();

$timer = $loop->addPeriodicTimer(5, function () {
    if (someExternalResult()) {
      $loop->cancelTimer($timer);
      $loop->cancelTimer($watchdogTimer); // But $watchdogTimer isn't defined yet
    }
    echo 'Tick' . PHP_EOL;
});

$watchdogTimer = $loop->addTimer(60*45, function () use ($loop, $timer) {
    $loop->cancelTimer($timer);
    echo 'Done' . PHP_EOL;
});

$loop->run();

Am I missing something obvious here?

The workaround would be to have the pollingTimer just track the number of iterations it has run internally and cancel itself instead of relying on a watchdog. It's simpler in some ways but I'm not convinced it's 100% as effective.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions