| 1 | package Mojo::IOLoop::ProcBackground; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use Proc::Background; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | # | ||||||||||||
| 8 | # Thanks to Mojo::IOLoop::ReadWriteFork and Mojo::IOLoop::ForkCall | ||||||||||||
| 9 | # | ||||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | use constant DEBUG => $ENV{MOJO_PROCBACKGROUND_DEBUG} || 0; | ||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_PROCBACKGROUND_DEBUG'} || 0
MC/DC0%0/1
$ENV{'MOJO_PROCBACKGROUND_DEBUG'} || 0
$ENV{'MOJO_PROCBACKGROUND_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_PROCBACKGROUND_DEBUG'}
| |||||||||||||
| 12 | |||||||||||||
| 13 | 1 | our $VERSION = '0.06'; | |||||||||||
| 14 | |||||||||||||
| 15 | =head1 NAME | ||||||||||||
| 16 | |||||||||||||
| 17 | Mojo::IOLoop::ProcBackground - IOLoop interface to Proc::Background | ||||||||||||
| 18 | |||||||||||||
| 19 | =head1 VERSION | ||||||||||||
| 20 | |||||||||||||
| 21 | 0.06 | ||||||||||||
| 22 | |||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||
| 24 | |||||||||||||
| 25 | This is an IOLoop interface to Proc::Background. | ||||||||||||
| 26 | |||||||||||||
| 27 | From Proc::Background: | ||||||||||||
| 28 | |||||||||||||
| 29 | This is a generic interface for placing processes in the background on both Unix and | ||||||||||||
| 30 | Win32 platforms. This module lets you start, kill, wait on, retrieve exit values, and | ||||||||||||
| 31 | see if background processes still exist. | ||||||||||||
| 32 | |||||||||||||
| 33 | |||||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||||
| 35 | |||||||||||||
| 36 | use Mojolicious::Lite; | ||||||||||||
| 37 | |||||||||||||
| 38 | use Mojo::IOLoop::ProcBackground; | ||||||||||||
| 39 | |||||||||||||
| 40 | use File::Temp; | ||||||||||||
| 41 | use File::Spec; | ||||||||||||
| 42 | use Proc::Background; | ||||||||||||
| 43 | |||||||||||||
| 44 | any '/run' => sub { | ||||||||||||
| 45 | my $self = shift; | ||||||||||||
| 46 | |||||||||||||
| 47 | # Setup our request to take a while | ||||||||||||
| 48 | Mojo::IOLoop->stream($self->tx->connection)->timeout(30); | ||||||||||||
| 49 | $self->render_later; | ||||||||||||
| 50 | |||||||||||||
| 51 | $self->on(finish => sub { | ||||||||||||
| 52 | $self->app->log->debug("Finished"); | ||||||||||||
| 53 | }); | ||||||||||||
| 54 | |||||||||||||
| 55 | # We want the UserAgent to see something as soon as possible | ||||||||||||
| 56 | $self->res->code(200); | ||||||||||||
| 57 | $self->res->headers->content_type('text/html'); | ||||||||||||
| 58 | $self->write_chunk("<html><body>Starting...<br>\n"); | ||||||||||||
| 59 | |||||||||||||
| 60 | # This is our utility script that will run in the background | ||||||||||||
| 61 | my $tmp = File::Temp->new(UNLINK => 0, SUFFIX => '.pl'); | ||||||||||||
| 62 | my $statefile = $self->stash->{_statefile} = File::Spec->catfile(File::Spec->tmpdir, "done"); | ||||||||||||
| 63 | print($tmp 'sleep(10); $f="$ARGV[0].$$"; open($fh, ">", $f); sleep(3)'); | ||||||||||||
| 64 | my $script = $tmp->filename; | ||||||||||||
| 65 | undef($tmp); | ||||||||||||
| 66 | |||||||||||||
| 67 | # Thanks CPAN.. :) The magic happens in Proc::Background | ||||||||||||
| 68 | my $proc = $self->stash->{_proc} = Mojo::IOLoop::ProcBackground->new; | ||||||||||||
| 69 | |||||||||||||
| 70 | # Every so often we get a heartbeat from the background process | ||||||||||||
| 71 | $proc->on(alive => sub { | ||||||||||||
| 72 | my ($proc) = @_; | ||||||||||||
| 73 | |||||||||||||
| 74 | my $pid = $proc->proc->pid; | ||||||||||||
| 75 | my $statefile = $self->stash->{_statefile} . ".$pid"; | ||||||||||||
| 76 | |||||||||||||
| 77 | if (-f $statefile) { | ||||||||||||
| 78 | $self->write_chunk("Done</body></html>"); | ||||||||||||
| 79 | $proc->unsubscribe("alive"); | ||||||||||||
| 80 | } | ||||||||||||
| 81 | }); | ||||||||||||
| 82 | |||||||||||||
| 83 | # When the process terminates, we get this event | ||||||||||||
| 84 | $proc->on(dead => sub { | ||||||||||||
| 85 | my ($proc) = @_; | ||||||||||||
| 86 | |||||||||||||
| 87 | my $pid = $proc->proc->pid; | ||||||||||||
| 88 | my $statefile = $self->stash->{_statefile} . ".$pid"; | ||||||||||||
| 89 | |||||||||||||
| 90 | $self->app->log->debug("Done: $statefile"); | ||||||||||||
| 91 | $self->finish; | ||||||||||||
| 92 | }); | ||||||||||||
| 93 | |||||||||||||
| 94 | # Start our process | ||||||||||||
| 95 | $proc->run([$^X, $script, $statefile]); | ||||||||||||
| 96 | }; | ||||||||||||
| 97 | |||||||||||||
| 98 | # Run the app | ||||||||||||
| 99 | push(@ARGV, 'daemon', '-l', 'http://*:5555') unless @ARGV; | ||||||||||||
| 100 | app->log->level("debug"); | ||||||||||||
| 101 | app->secrets(["I Knos# you!!"]); | ||||||||||||
| 102 | app->start; | ||||||||||||
| 103 | |||||||||||||
| 104 | =head2 SEE ALSO | ||||||||||||
| 105 | |||||||||||||
| 106 | =over | ||||||||||||
| 107 | |||||||||||||
| 108 | =item L<Mojo::IOLoop::ReadWriteFork> | ||||||||||||
| 109 | |||||||||||||
| 110 | =item L<Mojo::IOLoop::ForkCall> | ||||||||||||
| 111 | |||||||||||||
| 112 | =back | ||||||||||||
| 113 | |||||||||||||
| 114 | =cut | ||||||||||||
| 115 | |||||||||||||
| 116 | # Thanks jberger.. :) | ||||||||||||
| 117 | |||||||||||||
| 118 | 1 | has 'command'; | |||||||||||
| 119 | |||||||||||||
| 120 | 0 | ▶ | has proc => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 121 | 0 | my $command = shift->command; | |||||||||||
| 122 | 0 | ▶ | Proc::Background->new(ref($command) ? @{ $command } : $command); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | 1 | }; | |||||||||||
| 124 | |||||||||||||
| 125 | 0 | ▶ | has recurring => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 126 | 0 | my $self = shift; | |||||||||||
| 127 | 0 | ▶ | Mojo::IOLoop->recurring(0.05 => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 128 | 0 | ▶ | if ($self->proc->alive) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | 0 | $self->emit("alive"); | |||||||||||
| 130 | } | ||||||||||||
| 131 | else { | ||||||||||||
| 132 | 0 | Mojo::IOLoop->remove($self->recurring); | |||||||||||
| 133 | 0 | $self->emit("dead"); | |||||||||||
| 134 | } | ||||||||||||
| 135 | 0 | }); | |||||||||||
| 136 | 1 | }; | |||||||||||
| 137 | |||||||||||||
| 138 | 0 | ▶ | sub run { | ||||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 139 | 0 | my $self = shift; | |||||||||||
| 140 | 0 | ▶ | $self->command(shift) if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 141 | 0 | $self->proc; # build proc | |||||||||||
| 142 | 0 | $self->recurring; # start watching it | |||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | 1 | 1; | |||||||||||