Question

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
});
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top