File Coverage

blib/lib/Mojolicious/Plugin/ForkCall.pm
Criterion Covered Total %
statement 31 31 100.0
branch 5 6 83.3
condition 2 3 66.6
subroutine 6 6 100.0
pod 1 1 100.0
total 45 47 95.7


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