Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 223 lines (175 sloc) 4.85 kb
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
1 package RunnerRole;
5b0127b @rcaputo Remove emit() from roles.
authored
2 # vim: ts=2 sw=2 noexpandtab
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
3
5b0127b @rcaputo Remove emit() from roles.
authored
4 use Reflex::Role;
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
5
5b0127b @rcaputo Remove emit() from roles.
authored
6 attribute_parameter att_pid => "pid";
7 attribute_parameter att_stderr => "stderr";
8 attribute_parameter att_stdin => "stdin";
9 attribute_parameter att_stdout => "stdout";
10 callback_parameter cb_exit => qw( on att_pid exit );
11 callback_parameter cb_stderr_closed => qw( on att_stderr closed );
12 callback_parameter cb_stderr_data => qw( on att_stderr data );
13 callback_parameter cb_stderr_error => qw( on att_stderr error );
14 callback_parameter cb_stdout_closed => qw( on att_stdout closed );
15 callback_parameter cb_stdout_data => qw( on att_stdout data );
16 callback_parameter cb_stdout_error => qw( on att_stdout error );
17 method_parameter method_put => qw( put att_stdin _ );
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
18
19 role {
4dcae46 @rcaputo Whitespace fixes.
authored
20 my $p = shift;
5b0127b @rcaputo Remove emit() from roles.
authored
21
4dcae46 @rcaputo Whitespace fixes.
authored
22 with 'Reflex::Role::OutStreaming' => {
23 att_handle => $p->att_stdin(),
24 method_put => $p->method_put(),
25 };
5b0127b @rcaputo Remove emit() from roles.
authored
26
4dcae46 @rcaputo Whitespace fixes.
authored
27 my $m_stdout_stop = "stop_" . $p->att_stdout();
28 my $cb_stdout_closed = $p->cb_stdout_closed();
5b0127b @rcaputo Remove emit() from roles.
authored
29
4dcae46 @rcaputo Whitespace fixes.
authored
30 requires(
5b0127b @rcaputo Remove emit() from roles.
authored
31 map { $p->$_() } qw(
4dcae46 @rcaputo Whitespace fixes.
authored
32 att_pid att_stderr att_stdin att_stdout
33 cb_exit
34 cb_stderr_closed cb_stderr_data cb_stderr_data
35 cb_stdout_closed cb_stdout_data cb_stdout_data
36 )
37 );
38
39 after $cb_stdout_closed => sub {
40 my ($self, $args) = @_;
41 $self->$m_stdout_stop();
42 };
43
44 with 'Reflex::Role::InStreaming' => {
45 att_handle => $p->att_stdout(),
46 cb_data => $p->cb_stdout_data(),
47 cb_error => $p->cb_stdout_error(),
48 cb_closed => $cb_stdout_closed,
49 };
50
51 my $m_stderr_stop = "stop_" . $p->att_stderr();
52 my $cb_stderr_closed = $p->cb_stderr_closed();
53
54 after $cb_stderr_closed => sub {
55 my ($self, $args) = @_;
56 $self->$m_stderr_stop();
57 };
58
59 with 'Reflex::Role::InStreaming' => {
60 att_handle => $p->att_stderr(),
61 cb_data => $p->cb_stderr_data(),
62 cb_error => $p->cb_stderr_error(),
63 cb_closed => $cb_stderr_closed,
64 };
65
66 with 'Reflex::Role::PidCatcher' => {
67 att_pid => $p->att_pid(),
68 cb_exit => $p->cb_exit(),
69 };
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
70 };
71
72 1;
73
74 __END__
75
76 extends 'Reflex::Base';
77
e0d6b99 @rcaputo Explicitly export watches() and emits() from their corresponding Refl…
authored
78 use Reflex::Trait::Watched qw(watches);
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
79 use Reflex::PID;
80
81 use Carp qw(croak);
82 use IPC::Run qw(start);
83 use Symbol qw(gensym);
84
85
86 __END__
87
9513af3 @rcaputo Rename "observe" to "watch".
authored
88 watches process => ( isa => 'Maybe[Reflex::PID]', is => 'rw' );
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
89
90 has [qw(stdin stdout stderr)] => (
4dcae46 @rcaputo Whitespace fixes.
authored
91 isa => 'Maybe[FileHandle]',
92 is => 'rw',
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
93 );
94
95 has ipc_run => ( isa => 'IPC::Run', is => 'rw' );
96
97 has cmd => (
4dcae46 @rcaputo Whitespace fixes.
authored
98 isa => 'ArrayRef',
99 is => 'ro',
100 required => 1,
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
101 );
102
103 ### Reap the child process.
104
105 sub on_process_exit {
4dcae46 @rcaputo Whitespace fixes.
authored
106 my ($self, $args) = @_;
107 $self->emit(event => 'exit', args => $args);
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
108 }
109
110 sub kill {
4dcae46 @rcaputo Whitespace fixes.
authored
111 my ($self, $signal) = @_;
112 croak "no process to kill" unless $self->process();
113 $signal ||= 'TERM';
114 kill $signal, $self->process()->pid();
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
115 }
116
117 ### Write to standard input.
118
119 sub on_stdin_error {
4dcae46 @rcaputo Whitespace fixes.
authored
120 my ($self, $args) = @_;
121 $self->emit(event => 'stdin_error', args => $args);
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
122 }
123
5b0127b @rcaputo Remove emit() from roles.
authored
124 with 'Reflex::Role::Writing' => { att_handle => 'stdin' };
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
125
126 sub on_stdin_writable {
4dcae46 @rcaputo Whitespace fixes.
authored
127 my ($self, $arg) = @_;
128 my $octets_left = $self->flush_stdin();
129 return if $octets_left;
130 $self->flush_stdin();
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
131 }
132
5b0127b @rcaputo Remove emit() from roles.
authored
133 with 'Reflex::Role::Writable' => { att_handle => 'stdin' };
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
134
135 ### Read from standard output.
136
137 sub on_stdout_readable {
4dcae46 @rcaputo Whitespace fixes.
authored
138 my ($self, $arg) = @_;
139 my $octets_read = $self->read_stdout($arg);
140 warn $octets_read;
141 return if $octets_read;
142 if (defined $octets_read) {
143 warn 111;
144 $self->pause_stdout_readable();
145 return;
146 }
147 $self->stop_stdout_readable();
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
148 }
149
150 sub on_stdout_error {
4dcae46 @rcaputo Whitespace fixes.
authored
151 my ($self, $args) = @_;
152 $self->emit(event => 'stdout_error', args => $args);
153 $self->stop_stdout_readable();
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
154 }
155
156 with 'Reflex::Role::Reading' => {
4dcae46 @rcaputo Whitespace fixes.
authored
157 att_handle => 'stdout',
158 cb_data => make_emitter(on_stdout => "stdout"),
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
159 };
160
161 with 'Reflex::Role::Readable' => {
4dcae46 @rcaputo Whitespace fixes.
authored
162 att_handle => 'stdout',
163 cb_ready => 'on_stdout_readable',
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
164 };
165
166 ### Read from standard error.
167
168 sub on_stderr_error {
4dcae46 @rcaputo Whitespace fixes.
authored
169 my ($self, $args) = @_;
170 $self->emit(event => 'stderr_error', args => $args);
171 $self->stop_stderr_readable();
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
172 }
173
174 sub on_stderr_readable {
4dcae46 @rcaputo Whitespace fixes.
authored
175 my ($self, $arg) = @_;
176 my $octets_read = $self->read_stderr($arg);
177 warn $octets_read;
178 return if $octets_read;
179 if (defined $octets_read) {
180 warn 111;
181 $self->pause_stderr_readable();
182 return;
183 }
184 $self->stop_stderr_readable();
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
185 }
186
187 with 'Reflex::Role::Reading' => {
4dcae46 @rcaputo Whitespace fixes.
authored
188 att_handle => 'stderr',
189 cb_data => make_emitter(on_stderr => "stderr"),
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
190 };
191
192 with 'Reflex::Role::Readable' => {
4dcae46 @rcaputo Whitespace fixes.
authored
193 att_handle => 'stderr',
194 cb_ready => 'on_stderr_readable',
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
195 };
196
197 sub BUILD {
4dcae46 @rcaputo Whitespace fixes.
authored
198 my $self = shift;
199
200 my ($fh_in, $fh_out, $fh_err) = (gensym(), gensym(), gensym());
201
202 $self->ipc_run(
203 start(
204 $self->cmd(),
205 '<pipe', $fh_in,
206 '>pipe', $fh_out,
207 '2>pipe', $fh_err,
208 )
209 ) or die "IPC::Run start() failed: $? ($!)";
210
211 $self->process(
212 Reflex::PID->new(
213 pid => $self->ipc_run->{KIDS}[0]{PID}
214 )
215 );
216
217 $self->att_stdin($fh_in);
218 $self->att_stdout($fh_out);
219 $self->att_stderr($fh_err);
2ea8fb8 @rcaputo Parameterize events the roles can emit. Create eg-15-ipc-run.pl and a…
authored
220 }
221
222 1;
Something went wrong with that request. Please try again.