The finish event of the controller is not really appropriate here. It will not fire until the transaction is finished, which doesn't happen until you render a response. So once a success happens you want to call something that will render a response from there. You probably want to use the delay helper which will turn off automatic rendering, keep a reference to the transaction, and handle exceptions.
On Tue, Apr 21, 2015 at 3:39 PM, hammondos <[email protected]> wrote: > 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'}); > }); > > > -- > You received this message because you are subscribed to the Google Groups > "Mojolicious" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/mojolicious. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Mojolicious" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/mojolicious. For more options, visit https://groups.google.com/d/optout.
