blib/lib/Mojolicious/Plugin/Subprocess.pm

filter: Statementstmt 100% 30 / 30 Branchbran 66.6% 4 / 6 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 91.6% 44 / 48 CC 6 SCAR 6.6
CC
6
Cov
92%
__ANON__
3.0
__ANON__
2.1
register
2.0
CRAP 1.9
↑ Summary
1 package Mojolicious::Plugin::Subprocess;
2  
3 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.002';
8  
9 1 ▶sub register {
Subroutine register: called CC=2 SCAR=7.0
Pod documented
10 1 my ($self, $app, $options) = @_;
11
12 1 ▶ my %subprocess_args = %{$options // {}};
Condition50%1/2
$options // {}
l!l
10
MC/DC0%0/1
$options // {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
13 1 my $use_sereal = delete $subprocess_args{use_sereal};
14
15 3 ▶ $app->helper(subprocess => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
16 3 my ($c, $child, $parent) = @_;
17
18 3 my $subprocess = Mojo::IOLoop->subprocess;
19 3 $subprocess->$_($subprocess_args{$_}) for
20 9 grep { exists $subprocess_args{$_} } qw(deserialize ioloop serialize);
21 3 ▶ $subprocess->with_roles('Mojo::IOLoop::Subprocess::Role::Sereal')
Branch: 1 branch
BranchTrueFalseTotal
if $use_sereal 0 3 3
22 ->with_sereal if $use_sereal;
23  
24 3 my $tx = $c->render_later->tx;
25  
26 3 my $p = Mojo::Promise->new;
27 3 ▶ $subprocess->run($child, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
28 3 my ($subprocess, $err, @results) = @_;
29 3 ▶ return $p->reject($err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 2 3
30 2 $p->resolve(@results);
31 3 });
32  
33 2 ▶ return $p->then(sub { $c->$parent(@_) })
Subroutine __ANON__: called CC=1 SCAR=0.0
34 3 ▶ ->catch(sub { $c->helpers->reply->exception(pop) and undef $tx });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$c->helpers->reply->exception(pop()) and undef $tx 2 0 2
35 1 });
36 }
37  
38 1 1;
39  
40 =head1 NAME
41  
42 Mojolicious::Plugin::Subprocess - Subprocesses in Mojolicious applications
43  
44 =head1 SYNOPSIS
45  
46 use Mojolicious::Lite;
47
48 plugin 'Subprocess';
49
50 get '/slow' => sub {
51 my $c = shift;
52 $c->subprocess(sub {
53 return do_slow_stuff();
54 }, sub {
55 my ($c, @results) = @_;
56 $c->render(json => \@results);
57 });
58 };
59
60 # or use Sereal as serializer
61 plugin 'Subprocess' => {use_sereal => 1};
62  
63 =head1 DESCRIPTION
64  
65 L<Mojolicious::Plugin::Subprocess> is a L<Mojolicious> plugin that adds a
66 L</"subprocess"> helper method to your application, which uses
67 L<Mojo::IOLoop::Subprocess> to perform computationally expensive operations in
68 subprocesses without blocking the event loop.
69  
70 The option C<use_sereal> (requires L<Mojo::IOLoop::Subprocess::Role::Sereal>)
71 will use L<Sereal> for data serialization, which is faster than L<Storable> and
72 supports serialization of more reference types such as C<Regexp>. The
73 L<Sereal::Encoder/"FREEZE/THAW CALLBACK MECHANISM"> is supported to control
74 serialization of blessed objects.
75  
76 Any other options passed to the plugin will be used as attributes to build the
77 L<Mojo::IOLoop::Subprocess> object.
78  
79 Note that it does not increase the timeout of the connection, so if your forked
80 process is going to take a very long time, you might need to increase that
81 using L<Mojolicious::Plugin::DefaultHelpers/"inactivity_timeout">.
82  
83 =head1 HELPERS
84  
85 L<Mojolicious::Plugin::Subprocess> implements the following helpers.
86  
87 =head2 subprocess
88  
89 $c->subprocess(sub {
90 my $subprocess = shift;
91 ...
92 }, sub {
93 my ($c, @results) = @_;
94 ...
95 });
96  
97 Execute the first callback in a child process with
98 L<Mojo::IOLoop::Subprocess/"run">, and execute the second callback in the
99 parent process with the results. Automatic rendering is disabled, and an
100 exception response is rendered if an exception is thrown in either callback.
101 This also means that the parent callback will not be called if an exception is
102 thrown in the child callback.
103  
104 =head1 METHODS
105  
106 L<Mojolicious::Plugin::Subprocess> inherits all methods from
107 L<Mojolicious::Plugin> and implements the following new ones.
108  
109 =head2 register
110  
111 $plugin->register(Mojolicious->new);
112 $plugin->register(Mojolicious->new, {ioloop => $ioloop});
113  
114 Register helper in L<Mojolicious> application.
115  
116 =head1 BUGS
117  
118 Report any issues on the public bugtracker.
119  
120 =head1 AUTHOR
121  
122 Dan Book <dbook@cpan.org>
123  
124 =head1 COPYRIGHT AND LICENSE
125  
126 This software is Copyright (c) 2016 by Dan Book.
127  
128 This is free software, licensed under:
129  
130 The Artistic License 2.0 (GPL Compatible)
131  
132 =head1 SEE ALSO
133  
134 L<Mojo::IOLoop::Subprocess>, L<Mojolicious::Plugin::ForkCall>
↑ Summary