| 1 | # Copyright 2007, 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Gtk2::Subprocess; | ||
| 18 | use 5.008; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use Glib 1.220; | ||
| 23 | use Gtk2; | ||
| 24 | use Gtk2::Ex::TreeModelBits; | ||
| 25 | use List::Util; | ||
| 26 | use POSIX qw(EWOULDBLOCK); | ||
| 27 | use Locale::TextDomain ('App-Chart'); | ||
| 28 | |||
| 29 | use Glib::Ex::FreezeNotify; | ||
| 30 | use App::Chart::Glib::Ex::MoreUtils; | ||
| 31 | use Glib::Ex::SourceIds; | ||
| 32 | use App::Chart; | ||
| 33 | |||
| 34 | # uncomment this to run the ### lines | ||
| 35 | #use Smart::Comments; | ||
| 36 | |||
| 37 | use constant { MAX_RUNNING => 3, | ||
| 38 | IDLE_TIMEOUT_SECONDS => 60, | ||
| 39 | DEFAULT_STATUS => '' }; | ||
| 40 | |||
| 41 | use Glib::Object::Subclass | ||
| 42 | 'Glib::Object', | ||
| 43 | signals => { notify => \&_do_notify, | ||
| 44 | status_changed => { param_types => [ 'Glib::String' ], | ||
| 45 | return_type => undef }, | ||
| 46 | }, | ||
| 47 | properties => [Glib::ParamSpec->string | ||
| 48 | ('status', | ||
| 49 | 'status', | ||
| 50 | 'Blurb.', | ||
| 51 | '', | ||
| 52 | Glib::G_PARAM_READWRITE), | ||
| 53 | |||
| 54 | Glib::ParamSpec->object | ||
| 55 | ('job', | ||
| 56 | 'job', | ||
| 57 | 'Blurb.', | ||
| 58 | # actually 'App::Chart::Gtk2::Job', but saying that | ||
| 59 | # creates package load order headaches | ||
| 60 | 'Glib::Object', | ||
| 61 | Glib::G_PARAM_READWRITE), | ||
| 62 | ]; | ||
| 63 | |||
| 64 | |||
| 65 | our $store = Gtk2::ListStore->new ('Glib::Scalar'); | ||
| 66 | |||
| 67 | sub INIT_INSTANCE { | ||
| 68 | my ($self) = @_; | ||
| 69 | ### Subprocess INIT_INSTANCE() | ||
| 70 | |||
| 71 | $self->{'status'} = DEFAULT_STATUS; | ||
| 72 | liststore_append_with_values ($store, 0 => $self); | ||
| 73 | |||
| 74 | # or maybe $^X for '/usr/bin/perl' and $0 for 'chart', except that bombs | ||
| 75 | # badly if being run from a script | ||
| 76 | my @cmd = ('chart', '--subprocess'); | ||
| 77 | if ($App::Chart::option{'verbose'}) { push @cmd, '--verbose' } | ||
| 78 | |||
| 79 | require IO::Socket; | ||
| 80 | my ($sock_child, $sock_parent) = IO::Socket->socketpair | ||
| 81 | (Socket::AF_UNIX(), Socket::SOCK_STREAM(), 0); | ||
| 82 | |||
| 83 | ### @cmd | ||
| 84 | require Proc::SyncExec; | ||
| 85 | my $pid = Proc::SyncExec::sync_exec | ||
| 86 | (sub { | ||
| 87 | my $fd = $sock_child->fileno; | ||
| 88 | return POSIX::dup2 ($fd, 0) != -1 | ||
| 89 | && POSIX::dup2 ($fd, 1) != -1 | ||
| 90 | && POSIX::dup2 ($fd, 2) != -1; | ||
| 91 | }, @cmd); | ||
| 92 | # }, '/bin/sh', '-c', 'echo hi 1>&2; sleep 5; echo bye'); | ||
| 93 | if (! defined $pid) { | ||
| 94 | my $err = Glib::strerror ($!); | ||
| 95 | my $status = $self->{'status'} | ||
| 96 | = __x('Cannot start subprocess: {strerror}', strerror => $err); | ||
| 97 | $self->message ("$status\n"); | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | require App::Chart::Proc::ChildPid; | ||
| 101 | $self->{'pidobj'} = App::Chart::Proc::ChildPid->new ($pid); | ||
| 102 | |||
| 103 | $sock_child->close; | ||
| 104 | $sock_parent->blocking(0); | ||
| 105 | ### self: "$self" | ||
| 106 | ### $pid | ||
| 107 | ### reader fd: $sock_parent->fileno | ||
| 108 | |||
| 109 | require PerlIO::via::EscStatus::Parser; | ||
| 110 | $self->{'status_parser'} = PerlIO::via::EscStatus::Parser->new; | ||
| 111 | $self->{'sock'} = $sock_parent; | ||
| 112 | $self->{'io_watch'} = Glib::Ex::SourceIds->new | ||
| 113 | (Glib::IO->add_watch ($sock_parent->fileno, ['in', 'hup', 'err'], | ||
| 114 | \&_do_read, App::Chart::Glib::Ex::MoreUtils::ref_weak($self))); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub FINALIZE_INSTANCE { | ||
| 118 | my ($self) = @_; | ||
| 119 | ### Subprocess FINALIZE_INSTANCE() | ||
| 120 | $self->stop; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub SET_PROPERTY { | ||
| 124 | my ($self, $pspec, $newval) = @_; | ||
| 125 | my $pname = $pspec->get_name; | ||
| 126 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 127 | |||
| 128 | if ($pname eq 'job') { | ||
| 129 | _update_idle_timer ($self); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | # 'notify' signal class closure | ||
| 134 | sub _do_notify { | ||
| 135 | my ($self, $pspec) = @_; | ||
| 136 | ### Subprocess notify: $pspec->get_name | ||
| 137 | $self->signal_chain_from_overridden ($pspec); | ||
| 138 | |||
| 139 | # emit 'status-changed' under notify so it's held up by freeze_notify | ||
| 140 | if ($pspec->get_name eq 'status') { | ||
| 141 | $self->signal_emit ('status-changed', $self->{'status'}); | ||
| 142 | _emit_row_changed ($self); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | sub pid { | ||
| 147 | my ($self) = @_; | ||
| 148 | my $pidobj = $self->{'pidobj'}; | ||
| 149 | return $pidobj && $pidobj->pid; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub message { | ||
| 153 | my ($self, $str) = @_; | ||
| 154 | if (my $job = $self->{'job'}) { | ||
| 155 | $job->message ($str); | ||
| 156 | } else { | ||
| 157 | print $str; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub start_job { | ||
| 162 | my ($self, $job) = @_; | ||
| 163 | ### Subprocess start_job(): "$job" | ||
| 164 | { | ||
| 165 | my $freezer = Glib::Ex::FreezeNotify->new ($self, $job); | ||
| 166 | $job->set (subprocess => $self); | ||
| 167 | $self->set (job => $job); | ||
| 168 | |||
| 169 | my $fh = $self->{'sock'}; | ||
| 170 | if ($fh) { | ||
| 171 | $job->set(status => __('Starting')); | ||
| 172 | $self->set (status => __x('Running job: {name}', | ||
| 173 | name => $job->get('name'))); | ||
| 174 | undef $freezer; | ||
| 175 | require Storable; | ||
| 176 | my $data = Storable::freeze ($job->get('args')); | ||
| 177 | print $fh length($data),"\n",$data; | ||
| 178 | $fh->flush; | ||
| 179 | } else { | ||
| 180 | _unset_job ($self, $self->{'status'}, $self->{'status'}); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | sub status { | ||
| 186 | my ($self) = @_; | ||
| 187 | return $self->{'status'}; | ||
| 188 | } | ||
| 189 | |||
| 190 | # return an idle Subprocess, possibly newly started, or undef if the | ||
| 191 | # subprocess maximum has been reached | ||
| 192 | # | ||
| 193 | sub find_idle { | ||
| 194 | my ($class) = @_; | ||
| 195 | ### Subprocess find_idle() | ||
| 196 | my @procs = grep { $_->pid } | ||
| 197 | Gtk2::Ex::TreeModelBits::column_contents ($store, 0); | ||
| 198 | if (my $proc = List::Util::first {$_->{'sock'} && ! $_->{'job'}} @procs) { | ||
| 199 | return $proc; | ||
| 200 | } | ||
| 201 | if (@procs >= MAX_RUNNING) { | ||
| 202 | return undef; | ||
| 203 | } | ||
| 204 | return $class->new; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub _unset_job { | ||
| 208 | my ($self, $job_status, $self_status) = @_; | ||
| 209 | # freeze_notify so job and subprocess are both updated before | ||
| 210 | # status-change stuff runs | ||
| 211 | my $freezer = Glib::Ex::FreezeNotify->new ($self); | ||
| 212 | if (my $job = $self->{'job'}) { | ||
| 213 | $freezer->add ($job); | ||
| 214 | $job->set (subprocess => undef, | ||
| 215 | done => 1, | ||
| 216 | status => $job_status); | ||
| 217 | } | ||
| 218 | $self->set (job => undef, | ||
| 219 | status => $self_status); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub stop { | ||
| 223 | my ($self) = @_; | ||
| 224 | ### Subprocess stop() | ||
| 225 | delete $self->{'io_watch'}; | ||
| 226 | delete $self->{'sock'}; | ||
| 227 | delete $self->{'pidobj'}; | ||
| 228 | _unset_job ($self, undef, __('Stopped')); | ||
| 229 | } | ||
| 230 | |||
| 231 | sub _do_read { | ||
| 232 | my ($fd, $conditions, $ref_weak_self) = @_; | ||
| 233 | my $self = $$ref_weak_self || return Glib::SOURCE_REMOVE; | ||
| 234 | #### Subprocess read: "$self" | ||
| 235 | my $sock = $self->{'sock'}; | ||
| 236 | my $status = undef; | ||
| 237 | |||
| 238 | for (;;) { | ||
| 239 | my $buf; | ||
| 240 | my $len = $sock->sysread ($buf, 8192); | ||
| 241 | #### got: $len | ||
| 242 | ### $! | ||
| 243 | |||
| 244 | if (! $len) { | ||
| 245 | if (! defined $len) { | ||
| 246 | if ($! == EWOULDBLOCK) { last; } # no more data for now | ||
| 247 | my $errmsg = Glib::strerror ($!); | ||
| 248 | $self->message ("Subprocess read error: $errmsg\n"); | ||
| 249 | $status = __('Read error'); | ||
| 250 | } else { | ||
| 251 | # end of file, child closed pipe | ||
| 252 | $status = __('Died'); | ||
| 253 | } | ||
| 254 | delete $self->{'io_watch'}; | ||
| 255 | delete $self->{'sock'}; | ||
| 256 | delete $self->{'pidobj'}; | ||
| 257 | _unset_job ($self, $status, $status); | ||
| 258 | return Glib::SOURCE_REMOVE; | ||
| 259 | } | ||
| 260 | |||
| 261 | my ($new_status, $message) = $self->{'status_parser'}->parse($buf); | ||
| 262 | $self->message ($message); | ||
| 263 | if (defined $new_status) { $status = $new_status; } | ||
| 264 | } | ||
| 265 | |||
| 266 | if (defined $status) { | ||
| 267 | if ($status eq 'Idle') { | ||
| 268 | _unset_job ($self, __('Done'), __('Idle')); | ||
| 269 | App::Chart::Gtk2::JobQueue->consider_run; | ||
| 270 | } else { | ||
| 271 | if (my $job = $self->{'job'}) { | ||
| 272 | $job->set (status => $status); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | } | ||
| 276 | return Glib::SOURCE_CONTINUE; | ||
| 277 | } | ||
| 278 | |||
| 279 | sub _update_idle_timer { | ||
| 280 | my ($self) = @_; | ||
| 281 | my $want_timer = ($self->pid && ! $self->{'job'}); | ||
| 282 | |||
| 283 | if ($want_timer) { | ||
| 284 | $self->{'timer_ids'} ||= Glib::Ex::SourceIds->new | ||
| 285 | (Glib::Timeout->add (IDLE_TIMEOUT_SECONDS * 1000, | ||
| 286 | \&_do_idle_timeout, | ||
| 287 | App::Chart::Glib::Ex::MoreUtils::ref_weak($self))); | ||
| 288 | } else { | ||
| 289 | $self->{'timer_ids'} = undef; | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | sub _do_idle_timeout { | ||
| 294 | my ($ref_weak_self) = @_; | ||
| 295 | my $self = $$ref_weak_self || return Glib::SOURCE_REMOVE; | ||
| 296 | $self->stop; | ||
| 297 | |||
| 298 | Gtk2::Ex::TreeModelBits::remove_matching_rows | ||
| 299 | ($store, sub { my ($store, $iter) = @_; | ||
| 300 | $store->get_value($iter,0) == $self }); | ||
| 301 | |||
| 302 | $self->{'timer_ids'} = undef; | ||
| 303 | return Glib::SOURCE_REMOVE; | ||
| 304 | } | ||
| 305 | |||
| 306 | # send out a 'row-changed' on the global $store for subprocesses $self | ||
| 307 | sub _emit_row_changed { | ||
| 308 | my ($self) = @_; | ||
| 309 | $store->foreach (sub { | ||
| 310 | my ($store, $path, $iter) = @_; | ||
| 311 | my $this = $store->get_value ($iter, 0); | ||
| 312 | if ($this && $this == $self) { | ||
| 313 | $store->row_changed ($path, $iter); | ||
| 314 | } | ||
| 315 | }); | ||
| 316 | } | ||
| 317 | |||
| 318 | sub all_subprocesses { | ||
| 319 | my ($class) = @_; | ||
| 320 | return Gtk2::Ex::TreeModelBits::column_contents ($store, 0); | ||
| 321 | } | ||
| 322 | |||
| 323 | sub remove_done { | ||
| 324 | my ($class) = @_; | ||
| 325 | Gtk2::Ex::TreeModelBits::remove_matching_rows | ||
| 326 | ($store, sub { my ($store, $iter) = @_; | ||
| 327 | my $proc = $store->get_value ($iter, 0); | ||
| 328 | return ! $proc->pid; | ||
| 329 | }); | ||
| 330 | } | ||
| 331 | |||
| 332 | #------------------------------------------------------------------------------ | ||
| 333 | # generic helpers | ||
| 334 | |||
| 335 | sub liststore_append_with_values { | ||
| 336 | my $store = shift; | ||
| 337 | $store->insert_with_values ($store->iter_n_children(undef), @_); | ||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 341 | 1; | ||
| 342 | __END__ |