File Coverage

blib/lib/Mojolicious/Plugin/ForkCall.pm
Criterion Covered Total %
statement 28 28 100.0
branch 5 6 83.3
condition 2 3 66.6
subroutine 5 5 100.0
pod 1 1 100.0
total 41 43 95.3


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::ForkCall;
2              
3 1     1   1126 use Mojo::Base 'Mojolicious::Plugin';
  1         3  
  1         11  
4              
5 1     1   743 use Mojo::IOLoop::ForkCall;
  1         4  
  1         44  
6 1     1   7 use Carp;
  1         3  
  1         416  
7             our @CARP_NOT = qw/Mojolicious Mojolicious::Controller/;
8              
9             sub register {
10 1     1 1 46 my ($self, $app) = @_;
11              
12             $app->helper(fork_call => sub {
13 5     5   135808 my $c = shift;
14 5 100 66     55 unless (@_ > 1 and ref $_[-1] eq 'CODE') {
15 1         491 croak 'fork_call helper must be passed a callback';
16             }
17              
18 4         27 my $cb = pop;
19 4         15 my @args = @_;
20              
21 4         20 my $tx = $c->render_later->tx;
22             Mojo::IOLoop->delay(
23             sub{
24 4         2096 my $end = shift->begin;
25 4 50       51 my $once = sub { $end->(@_) if $end; undef $end };
  4         61  
  4         3911  
26 4         71 Mojo::IOLoop::ForkCall->new
27             ->catch($once)
28             ->run(@args, $once);
29             },
30             sub {
31 4         228 my ($delay, $err, @return) = @_;
32 4 100       51 die $err if $err;
33 2         14 $c->$cb(@return);
34             }
35             )->catch(sub{
36 3         812 $c->helpers->reply->exception(pop);
37 3         127382 undef $tx;
38 4         180 })->wait;
39 1         13 });
40             }
41              
42             1;
43              
44             =head1 NAME
45              
46             Mojolicious::Plugin::ForkCall - (DEPRECATED) run blocking code asynchronously in Mojolicious applications by forking
47              
48             =head1 SYNOPSIS
49              
50             use Mojolicious::Lite;
51              
52             plugin 'Mojolicious::Plugin::ForkCall';
53              
54             get '/slow' => sub {
55             my $c = shift;
56             my @args = ...;
57             $c->fork_call(
58             sub {
59             my @args = @_;
60             return do_slow_stuff(@args);
61             },
62             [@args],
63             sub {
64             my ($c, @return) = @_;
65             $c->render(json => \@return);
66             }
67             );
68             };
69              
70             ...
71              
72             app->start;
73              
74             =head1 DESCRIPTION
75              
76             DEPRECATED!
77             The main module L is deprecated in favor of the L core module L.
78             This module is also deprecated but in favor of the CPAN module L which does approximately what this module does but in terms of the core module.
79              
80             Registering L adds a helper method C
81             to your L application, making it easy to start code in a forked
82             process using L.
83              
84             Note that it does not increase the timeout of the connection, so if your
85             forked process is going to take a very long time, you might need to increase
86             that using L.
87              
88             =head1 HELPERS
89              
90             This module adds the following helper method to your application:
91              
92             =head2 fork_call
93              
94             $c->fork_call(
95             sub {
96             my @args = @_;
97             # This code is run in a forked process
98             return @return;
99             },
100             [$arg1, $arg2, $arg3], # Optional arguments passed to the above code
101             sub {
102             my ($c, @return) = @_;
103             # This code is run in the current process once the child exits
104             }
105             );
106              
107             The C helper takes up to 3 arguments: a required code reference to
108             be run in a forked child process, an optional array reference of arguments to
109             be passed to the child code, and a required code reference to be run in the
110             parent as a callback. The callback is passed the controler instance and return
111             values of the child.
112              
113             The helper emulates the former L core C helper and as such
114             it will render an exception (500) page if any uncaught exception occurs in the
115             child process or in the parent callback. This also means that the parent
116             callback will not be called if the child process encounters an exception.
117              
118             This helper is a convenience only and is not indended for complex cases.
119             If you need to configure the L instance or want to
120             "fork and forget" a child, you should use the class directly rather than this
121             helper. If more complicated delays are required, you should use the
122             L method directly, along with an instance of
123             L.
124              
125             =cut
126