Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a Plack request handler in background?

I have a simple Plack app (something like Plack::App::GitHub::WebHook) that I run using plackup. I perform a lengthy operation in the request handler, which currently makes the app unresponsive to subsequent requests until the operation is finished. How can I perform the operation in background, without blocking other requests? And a follow-up question, how can I keep at most one job running in that background queue?

I’m used to libdispatch, so I’d love something like this:

my $queue = Hypothetical::Concurrency::Queue->new(max_jobs => 1);
$queue->dispatch(sub {
    # code
});
like image 904
zoul Avatar asked Jan 07 '14 17:01

zoul


Video Answer


1 Answers

I have just realized there’s one very important distinction I forgot to make: I don’t have to wait for the job to finish to respond to the HTTP request. Which means I’m fine with Forks::Super:

#!/usr/bin/env perl

use strict;
use warnings;
use Forks::Super MAX_PROC => 1, ON_BUSY => 'queue';

my $app = sub {
    my $env = shift;
    fork sub {
        # lengthy operation
    };
    return [202, ['Content-Type'=>'text/plain', 'Content-Length'=>8], ["Accepted"]];
};

Now servicing the request finishes immediately, the long operation runs in background and there’s always at most one of them running. Forks::Super looks complex and takes ages to install, so I’d be happy if someone knows a more lightweight module offering a similar feature.

like image 158
zoul Avatar answered Nov 08 '22 18:11

zoul