Hi,
I'm trying to run an AJAX call that needs to check an external API queue for a response every second, then generate a chart based on that data using JSON.
The code sample from the module I'm using to connect to the API suggests using a for...sleep loop, however clearly this doesn't work with Mojolicious.
I've tried several different permutations of using a recurring loop with varying degrees of success, but just can't get it to work - latest example below. This example never exits, even when there is data returned.
I'm aware that the $success parameter is probably not helping the situation, but would really appreciate any guidance on where to go next to stop the loop once a successful response has been received, and render a JSON response.
my $success = 0;
my $id = Mojo::IOLoop->recurring(
1 => sub {
$self->finish if $success == 1;
if (my $results = $sc_api->check_queue($report_id)) {
my $rows = $sc_api->process_os($results);
$log->info(Dumper($rows));
&_insert_data({domain => $domain, path => $path, data => {os => $rows}});
$success = 1;
}
}
);
# need to wait
$self->on(finish => sub {
# remove ioloop
Mojo::IOLoop->remove($id);
my $db = $self->db;
my $analytics = $db->get_collection('analytics');
my $rows = $analytics->find({id => $url});
my $doc = $rows->next;
$self->render(json => $doc->{'analytics'}->{'os'});
});