how to update a bootstrap progress bar within mojolicious

247 views
Skip to first unread message

Johan De Graeve

unread,
Mar 28, 2015, 9:23:23 AM3/28/15
to mojol...@googlegroups.com
Hi everybody,

Could somebody provide me please with a simple example to update a (bootstrap) progress bar when performing a long lasting function.

I read that maybe the after_build_tx hook is the answer to my questions. Just not sure how to use is.

after_build_tx

Emitted right after the transaction is built and before the HTTP request gets parsed.

$app->hook(after_build_tx => sub {
  my ($tx, $app) = @_;
  ...
});

This is a very powerful hook and should not be used lightly, it makes some rather advanced features such as upload progress bars possible. Note that this hook will not work for embedded applications, because only the host application gets to build transactions. (Passed the transaction and application object)

I wrote a controller which a refresh function which takes quite a while

sub refresh {
    my $self    = shift;

    $total = $self->modules->nr_of_modules();
    foreach my $environment (@environments) {
        my @modules = $self->modules->modules_in_env($environment);
        foreach my $module (@modules) {
            $self->modules->add_module(environment => $environment, module => $module);
            $done++;
            $progress = int($done / $total)*100;
            $self->render( progress => $progress );
        }
    }
}

And would like to pass the $progress to my template and update the progress bar.
 
<div class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="<%= $progress =>" aria-valuemin="0" aria-valuemax="100" style="width: <%= $progress %>%;">
    <%= $progress %>%
  </div>
</div>

All help will be very much appreciated.

Thx,
Johan

Jason Galea

unread,
Apr 1, 2015, 7:44:28 AM4/1/15
to mojol...@googlegroups.com
Hi Johan,

I don't think after_build_tx is what you want. It looks like that is for hooking into the request transaction and getting progress updates from it.

I think you would want to background your process in some way (fork or job queue maybe.. minion?) and return your page response. The background process should then store it's progress periodically in a database or similar. From the page you then poll the server via ajax to another endpoint that can read and return the progress value and update the progress bar in the page.

cheers,

Jason

 

--
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 mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Jason Galea

unread,
Apr 1, 2015, 7:57:49 AM4/1/15
to mojol...@googlegroups.com
possibly dependant on how long your process takes, you could simplify things by using "on 'finish'" to run your process after rendering a response. Then same deal with recording progress to db etc..

sub refresh {
    my $self    = shift;

    $self->on(finish => sub{
        my $ctrl = shift;
        $total = $self->modules->nr_of_modules();
        ...
    });

    $self->render()
}

 

Reply all
Reply to author
Forward
0 new messages