| 1 | package Zing::Cli; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use lib '.'; | ||
| 9 | |||
| 10 | use parent 'Data::Object::Cli'; | ||
| 11 | |||
| 12 | use Zing; | ||
| 13 | use Zing::Channel; | ||
| 14 | use Zing::Daemon; | ||
| 15 | use Zing::Registry; | ||
| 16 | |||
| 17 | use FlightRecorder; | ||
| 18 | use File::Spec; | ||
| 19 | |||
| 20 | our $VERSION = '0.11'; # VERSION | ||
| 21 | |||
| 22 | our $name = 'zing <{command}> [<{app}>] [options]'; | ||
| 23 | |||
| 24 | # METHODS | ||
| 25 | |||
| 26 | sub auto { | ||
| 27 | { | ||
| 28 | clean => '_handle_clean', | ||
| 29 | logs => '_handle_logs', | ||
| 30 | pid => '_handle_pid', | ||
| 31 | restart => '_handle_restart', | ||
| 32 | update => '_handle_update', | ||
| 33 | start => '_handle_start', | ||
| 34 | stop => '_handle_stop', | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | sub subs { | ||
| 39 | { | ||
| 40 | clean => 'Prune the process registry', | ||
| 41 | logs => 'Tap logs and output to STDOUT', | ||
| 42 | pid => 'Display an application process ID', | ||
| 43 | restart => 'Restart the specified application', | ||
| 44 | update => 'Hot-reload application processes', | ||
| 45 | start => 'Start the specified application', | ||
| 46 | stop => 'Stop the specified application', | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | sub spec { | ||
| 51 | { | ||
| 52 | appdir => { | ||
| 53 | desc => 'Directory of the app file', | ||
| 54 | type => 'string', | ||
| 55 | flag => 'a', | ||
| 56 | }, | ||
| 57 | backlog => { | ||
| 58 | desc => 'Produce log output using the backlog', | ||
| 59 | type => 'flag', | ||
| 60 | flag => 'b', | ||
| 61 | }, | ||
| 62 | global => { | ||
| 63 | desc => 'Tap the "global" log channel (defaults to "local")', | ||
| 64 | type => 'flag', | ||
| 65 | flag => 'g', | ||
| 66 | }, | ||
| 67 | process => { | ||
| 68 | desc => 'Reduce log output by process name', | ||
| 69 | type => 'string', | ||
| 70 | flag => 'p', | ||
| 71 | }, | ||
| 72 | level => { | ||
| 73 | desc => 'Reduce log output by log-level', | ||
| 74 | type => 'string', | ||
| 75 | flag => 'l', | ||
| 76 | }, | ||
| 77 | libdir => { | ||
| 78 | desc => 'Directory for @INC', | ||
| 79 | type => 'string', | ||
| 80 | flag => 'I', | ||
| 81 | args => '@', | ||
| 82 | }, | ||
| 83 | piddir => { | ||
| 84 | desc => 'Directory for the pid file', | ||
| 85 | type => 'string', | ||
| 86 | flag => 'd', | ||
| 87 | }, | ||
| 88 | search => { | ||
| 89 | desc => 'Reduce log output by search string', | ||
| 90 | type => 'string', | ||
| 91 | flag => 's', | ||
| 92 | args => '@', | ||
| 93 | }, | ||
| 94 | tag => { | ||
| 95 | desc => 'Reduce log output by process tag', | ||
| 96 | type => 'string', | ||
| 97 | flag => 't', | ||
| 98 | }, | ||
| 99 | verbose => { | ||
| 100 | desc => 'Produce verbose log output', | ||
| 101 | type => 'flag', | ||
| 102 | flag => 'v', | ||
| 103 | }, | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | sub _handle_clean { | ||
| 108 | my ($self) = @_; | ||
| 109 | |||
| 110 | my $r = Zing::Registry->new; | ||
| 111 | |||
| 112 | for my $id (@{$r->ids}) { | ||
| 113 | my $data = $r->store->recv($id) or next; | ||
| 114 | my $pid = $data->{process} or next; | ||
| 115 | $r->store->drop($id) unless kill 0, $pid; | ||
| 116 | } | ||
| 117 | |||
| 118 | return $self; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _handle_logs { | ||
| 122 | my ($self) = @_; | ||
| 123 | |||
| 124 | my $c = Zing::Channel->new( | ||
| 125 | name => '$journal', | ||
| 126 | (target => $self->opts->global ? 'global' : 'local') | ||
| 127 | ); | ||
| 128 | |||
| 129 | $c->reset if $self->opts->backlog; | ||
| 130 | |||
| 131 | while (1) { | ||
| 132 | next unless my $info = $c->recv; | ||
| 133 | |||
| 134 | my $from = $info->{from}; | ||
| 135 | my $data = $info->{data}; | ||
| 136 | my $logs = $data->{logs}; | ||
| 137 | |||
| 138 | $logs->{level} = $self->opts->level if $self->opts->level; | ||
| 139 | |||
| 140 | my $logger = FlightRecorder->new($logs); | ||
| 141 | my $report = $self->opts->verbose ? 'verbose' : 'simple'; | ||
| 142 | my $lines = $logger->$report->lines; | ||
| 143 | my $tag = $data->{tag} || '--'; | ||
| 144 | |||
| 145 | if (my $filter = $self->opts->from) { | ||
| 146 | next unless $from =~ /$filter/; | ||
| 147 | } | ||
| 148 | |||
| 149 | if (my $filter = $self->opts->tag) { | ||
| 150 | next unless $tag =~ /$filter/; | ||
| 151 | } | ||
| 152 | |||
| 153 | if (my $search = $self->opts->search) { | ||
| 154 | for my $search (@$search) { | ||
| 155 | @$lines = grep /$search/, @$lines; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | print STDOUT $from, ' ', $tag, ' ', $_, "\n" for @$lines; | ||
| 160 | } | ||
| 161 | |||
| 162 | return $self; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub _handle_pid { | ||
| 166 | my ($self) = @_; | ||
| 167 | |||
| 168 | my $app = $self->args->app; | ||
| 169 | |||
| 170 | if (!$app) { | ||
| 171 | print $self->help, "\n"; | ||
| 172 | return $self->okay; | ||
| 173 | } | ||
| 174 | |||
| 175 | my $piddir = $self->opts->piddir | ||
| 176 | || $ENV{ZING_PIDDIR} | ||
| 177 | || $ENV{ZING_HOME} | ||
| 178 | || File::Spec->curdir; | ||
| 179 | |||
| 180 | my $pidfile = File::Spec->catfile($piddir, "$app.pid"); | ||
| 181 | |||
| 182 | if (! -e $pidfile) { | ||
| 183 | print "Can't find pid $pidfile\n"; | ||
| 184 | return $self->fail; | ||
| 185 | } | ||
| 186 | |||
| 187 | my $pid = do { local $@; eval { do $pidfile } }; | ||
| 188 | |||
| 189 | if (!$pid || ref $pid) { | ||
| 190 | print "File didn't return a process ID: $pidfile\n"; | ||
| 191 | return $self->fail; | ||
| 192 | } | ||
| 193 | |||
| 194 | print "Process ID: $pid\n"; | ||
| 195 | |||
| 196 | return $self; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub _handle_restart { | ||
| 200 | my ($self) = @_; | ||
| 201 | |||
| 202 | $self->_handle_stop; | ||
| 203 | $self->_handle_start; | ||
| 204 | |||
| 205 | return $self; | ||
| 206 | } | ||
| 207 | |||
| 208 | sub _handle_start { | ||
| 209 | my ($self) = @_; | ||
| 210 | |||
| 211 | my $app = $self->args->app; | ||
| 212 | |||
| 213 | if (!$app) { | ||
| 214 | print $self->help, "\n"; | ||
| 215 | return $self->okay; | ||
| 216 | } | ||
| 217 | |||
| 218 | my $appdir = $self->opts->appdir; | ||
| 219 | my $piddir = $self->opts->piddir; | ||
| 220 | my $libdir = $self->opts->libdir; | ||
| 221 | |||
| 222 | unshift @INC, @$libdir if $libdir; | ||
| 223 | |||
| 224 | $appdir ||= $ENV{ZING_APPDIR} || $ENV{ZING_HOME} || File::Spec->curdir; | ||
| 225 | |||
| 226 | my $appfile = File::Spec->catfile($appdir, $app); | ||
| 227 | |||
| 228 | if (! -e $appfile) { | ||
| 229 | print "Can't find app: $appfile\n"; | ||
| 230 | return $self->fail; | ||
| 231 | } | ||
| 232 | |||
| 233 | my $scheme = do { local $@; eval { do $appfile } }; | ||
| 234 | |||
| 235 | if (ref $scheme ne 'ARRAY') { | ||
| 236 | print "File didn't return an app scheme: $appfile\n"; | ||
| 237 | return $self->fail; | ||
| 238 | } | ||
| 239 | |||
| 240 | my $daemon = Zing::Daemon->new( | ||
| 241 | name => $app, | ||
| 242 | app => Zing->new(scheme => $scheme), | ||
| 243 | ($piddir ? (pid_dir => $piddir) : ()), | ||
| 244 | ); | ||
| 245 | |||
| 246 | $daemon->start; | ||
| 247 | |||
| 248 | return $self; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub _handle_stop { | ||
| 252 | my ($self) = @_; | ||
| 253 | |||
| 254 | my $app = $self->args->app; | ||
| 255 | |||
| 256 | if (!$app) { | ||
| 257 | print $self->help, "\n"; | ||
| 258 | return $self->okay; | ||
| 259 | } | ||
| 260 | |||
| 261 | my $piddir = $self->opts->piddir | ||
| 262 | || $ENV{ZING_PIDDIR} | ||
| 263 | || $ENV{ZING_HOME} | ||
| 264 | || File::Spec->curdir; | ||
| 265 | |||
| 266 | my $pidfile = File::Spec->catfile($piddir, "$app.pid"); | ||
| 267 | |||
| 268 | if (! -e $pidfile) { | ||
| 269 | print "Can't find pid $pidfile\n"; | ||
| 270 | return $self->fail; | ||
| 271 | } | ||
| 272 | |||
| 273 | my $pid = do { local $@; eval { do $pidfile } }; | ||
| 274 | |||
| 275 | if (!$pid || ref $pid) { | ||
| 276 | print "File didn't return a process ID: $pidfile\n"; | ||
| 277 | return $self->fail; | ||
| 278 | } | ||
| 279 | |||
| 280 | kill 'TERM', $pid; | ||
| 281 | |||
| 282 | unlink $pidfile; | ||
| 283 | |||
| 284 | return $self; | ||
| 285 | } | ||
| 286 | |||
| 287 | sub _handle_update { | ||
| 288 | my ($self) = @_; | ||
| 289 | |||
| 290 | my $app = $self->args->app; | ||
| 291 | |||
| 292 | if (!$app) { | ||
| 293 | print $self->help, "\n"; | ||
| 294 | return $self->okay; | ||
| 295 | } | ||
| 296 | |||
| 297 | my $piddir = $self->opts->piddir | ||
| 298 | || $ENV{ZING_PIDDIR} | ||
| 299 | || $ENV{ZING_HOME} | ||
| 300 | || File::Spec->curdir; | ||
| 301 | |||
| 302 | my $pidfile = File::Spec->catfile($piddir, "$app.pid"); | ||
| 303 | |||
| 304 | if (! -e $pidfile) { | ||
| 305 | print "Can't find pid $pidfile\n"; | ||
| 306 | return $self->fail; | ||
| 307 | } | ||
| 308 | |||
| 309 | my $pid = do { local $@; eval { do $pidfile } }; | ||
| 310 | |||
| 311 | if (!$pid || ref $pid) { | ||
| 312 | print "File didn't return a process ID: $pidfile\n"; | ||
| 313 | return $self->fail; | ||
| 314 | } | ||
| 315 | |||
| 316 | kill 'USR2', $pid; | ||
| 317 | |||
| 318 | return $self; | ||
| 319 | } | ||
| 320 | |||
| 321 | 1; | ||
| 322 | |||
| 323 | |||
| 324 | =encoding utf8 | ||
| 325 | |||
| 326 | =head1 NAME | ||
| 327 | |||
| 328 | Zing::Cli - Command-line Interface | ||
| 329 | |||
| 330 | =cut | ||
| 331 | |||
| 332 | =head1 ABSTRACT | ||
| 333 | |||
| 334 | Command-line Process Management | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | =head1 SYNOPSIS | ||
| 339 | |||
| 340 | use Zing::Cli; | ||
| 341 | |||
| 342 | my $cli = Zing::Cli->new; | ||
| 343 | |||
| 344 | # $cli->handle('main'); | ||
| 345 | |||
| 346 | =cut | ||
| 347 | |||
| 348 | =head1 DESCRIPTION | ||
| 349 | |||
| 350 | This package provides a command-line interface for managing L<Zing> | ||
| 351 | applications. See the L<zing> documentation for interface arguments and | ||
| 352 | options. | ||
| 353 | |||
| 354 | =cut | ||
| 355 | |||
| 356 | =head1 INHERITS | ||
| 357 | |||
| 358 | This package inherits behaviors from: | ||
| 359 | |||
| 360 | L<Data::Object::Cli> | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | =head1 LIBRARIES | ||
| 365 | |||
| 366 | This package uses type constraints from: | ||
| 367 | |||
| 368 | L<Zing::Types> | ||
| 369 | |||
| 370 | =cut | ||
| 371 | |||
| 372 | =head1 METHODS | ||
| 373 | |||
| 374 | This package implements the following methods: | ||
| 375 | |||
| 376 | =cut | ||
| 377 | |||
| 378 | =head2 main | ||
| 379 | |||
| 380 | main() : Any | ||
| 381 | |||
| 382 | The main method executes the command-line interface and displays help text or | ||
| 383 | launches applications. | ||
| 384 | |||
| 385 | =over 4 | ||
| 386 | |||
| 387 | =item main example #1 | ||
| 388 | |||
| 389 | # given: synopsis | ||
| 390 | |||
| 391 | # e.g. | ||
| 392 | # zing start once -I t/lib -a t/app | ||
| 393 | # pass | ||
| 394 | |||
| 395 | $cli->handle('main'); | ||
| 396 | |||
| 397 | =back | ||
| 398 | |||
| 399 | =over 4 | ||
| 400 | |||
| 401 | =item main example #2 | ||
| 402 | |||
| 403 | # given: synopsis | ||
| 404 | |||
| 405 | # e.g. | ||
| 406 | # zing start unce -I t/lib -a t/app | ||
| 407 | # fail (not exist) | ||
| 408 | |||
| 409 | $cli->handle('main'); | ||
| 410 | |||
| 411 | =back | ||
| 412 | |||
| 413 | =cut | ||
| 414 | |||
| 415 | =head1 AUTHOR | ||
| 416 | |||
| 417 | Al Newkirk, C<awncorp@cpan.org> | ||
| 418 | |||
| 419 | =head1 LICENSE | ||
| 420 | |||
| 421 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 422 | |||
| 423 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 424 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 425 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 426 | |||
| 427 | =head1 PROJECT | ||
| 428 | |||
| 429 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 430 | |||
| 431 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 432 | |||
| 433 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 434 | |||
| 435 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 436 | |||
| 437 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 438 | |||
| 439 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 440 | |||
| 441 | =cut | ||
| 442 | __DATA__ |