Skip to content

Instantly share code, notes, and snippets.

@Roeya
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Roeya/9827d3624d8af09ed42f to your computer and use it in GitHub Desktop.
Save Roeya/9827d3624d8af09ed42f to your computer and use it in GitHub Desktop.
# This is a recurring timer I use, the
# the timer should fire till a certain condition
# is fulfilled
# Caveats - I get Mojo::Reactor::Poll: Timer failed: Missing or empty input
# from time to time
my ($i, $done, $id) = (0, 0, undef);
$id = Mojo::IOLoop->recurring(0.2 => sub {
return if ($done);
app->log->debug("waiting for $json - $i time");
my $temp;
eval {
$temp = check_json_result($base_path, $json);
};
if ($@) {
app->log->error("something is wrong here $@");
$temp = {status => 0, error => $@};
}
if ($temp || $i++ > $max_retries) {
if ($temp) {
app->log->debug("found $json");
$hash = $temp;
}
$done = 1;
$c->render(json => $hash, status => 200);
}
if ($id && $done) {
Mojo::IOLoop->remove($id);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment