3

I have some long running code that I'd like to execute after rendering in a Mojolicious app.

I'd like to avoid using Minion queues as I'd be calling many very short processes, and I've looked into Mojolicious::Plugin::ForkCall and Mojolicious::Plugin::Subprocess but they both timeout (as the short processes get called many times).

I remember coming across an example of this somewhere but cannot find it anymore.

Any help?

simone
  • 4,667
  • 4
  • 25
  • 47

1 Answers1

0

Call fork in an after_dispatch hook?

$app->hook(after_dispatch => sub {
  my $c = shift;
  my $pid = fork();
  if (defined($pid) && $pid == 0) {
      doSlowStuff();
      exit;
  }

});
mob
  • 117,087
  • 18
  • 149
  • 283
  • thanks - it looks like it works but is it possible to do for only one route in a lite - as opposed to for every dispatch? I don't need it all the time - only on some responses. – simone Jan 04 '18 at 19:24
  • Timer with fork() then? http://mojolicious.org/perldoc/Mojo/IOLoop#timer – mpapec Jan 04 '18 at 20:53
  • Yep - I found out thanks to your answer. https://gist.github.com/simonecesano/0ed2163c2ba4e635d458611963027762. Want to edit so I can accept the answer? – simone Jan 04 '18 at 21:07