How do I run a Plack request handler in background?
Asked Answered
G

1

6

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
});
Grangerize answered 7/1, 2014 at 17:33 Comment(1)
You'll want a multi-process plack server (starman is popular) for the first, and some form of lock for the second.Slater
G
4

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.

Grangerize answered 7/1, 2014 at 19:1 Comment(1)
Maybe 202 Accepted would be a better choice than 200 OK in this case?Eke

© 2022 - 2024 — McMap. All rights reserved.