| 1 | package Parallel::Dragons; | ||
| 2 | |||
| 3 | use 5.022001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | our $VERSION = '0.9.4'; | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | use Socket; | ||
| 11 | use IO::Select; | ||
| 12 | use IO::Socket::UNIX; | ||
| 13 | use Time::HiRes qw( usleep ); | ||
| 14 | |||
| 15 | use POSIX qw(WNOHANG); | ||
| 16 | |||
| 17 | use Data::Dumper; | ||
| 18 | |||
| 19 | ####################################################################### | ||
| 20 | # DEBUG and ERRORs | ||
| 21 | ####################################################################### | ||
| 22 | |||
| 23 | BEGIN { | ||
| 24 | my $level = ($ENV{DRAGONS_DEBUG}||2) + 0; | ||
| 25 | my $cron = $ENV{DRAGONS_CRONJOB}||$ENV{IS_CRONJOB}||0; | ||
| 26 | my $debug = $level>2 ? 1 : 0; | ||
| 27 | my $info = $level ; | ||
| 28 | my $trace = $level>9 ? 1 : 0; | ||
| 29 | eval <<EoC; | ||
| 30 | sub _debug { $debug } | ||
| 31 | sub _info { $info } | ||
| 32 | sub _trace { $trace } | ||
| 33 | sub IS_CRONJOB { $cron } | ||
| 34 | EoC | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub DEBUG { | ||
| 39 | return unless _debug; | ||
| 40 | return unless @_; | ||
| 41 | my $mask = shift; | ||
| 42 | local $Data::Dumper::Indent = 0; | ||
| 43 | map { ref $_ and $_ = Dumper($_) } @_; | ||
| 44 | my @args = $mask =~ m{\%} ? (sprintf $mask, @_) : ($mask, @_); | ||
| 45 | print STDERR "$$: ", time, " DEBUG: ", @args,"\n"; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub INFO { | ||
| 49 | return unless _info; | ||
| 50 | return unless @_; | ||
| 51 | my $mask = shift; | ||
| 52 | local $Data::Dumper::Indent = 0; | ||
| 53 | map { ref $_ and $_ = Dumper($_) } @_; | ||
| 54 | my @args = $mask =~ m{\%} ? (sprintf $mask, @_) : ($mask, @_); | ||
| 55 | print STDERR "$$: ", time, " INFO: ", @args,"\n"; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub TRACE { | ||
| 59 | return unless _trace; | ||
| 60 | return unless @_; | ||
| 61 | my $mask = shift; | ||
| 62 | local $Data::Dumper::Indent = 0; | ||
| 63 | map { ref $_ and $_ = Dumper($_) } @_; | ||
| 64 | my @args = $mask =~ m{\%} ? (sprintf $mask, @_) : ($mask, @_); | ||
| 65 | print STDERR "$$: ", time, " TRACE: ", @args,"\n"; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub FATAL { | ||
| 69 | croak @_; | ||
| 70 | } | ||
| 71 | |||
| 72 | ####################################################################### | ||
| 73 | # Main code | ||
| 74 | ####################################################################### | ||
| 75 | |||
| 76 | |||
| 77 | sub new { | ||
| 78 | my $class = shift; | ||
| 79 | |||
| 80 | my $self = bless {}, $class; | ||
| 81 | |||
| 82 | $self->init(); | ||
| 83 | |||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub init { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | $self->{max_childs} = $self->can('max_childs') | ||
| 91 | ? $self->max_childs | ||
| 92 | : $self->server_cores; | ||
| 93 | |||
| 94 | $self->{need_childs} = $self->{max_childs}; | ||
| 95 | |||
| 96 | $self->{tmp_basename} = $self->sockfile(); | ||
| 97 | |||
| 98 | $self->{_wait} = $self->can('wait_before_restart') | ||
| 99 | ? $self->wait_before_restart | ||
| 100 | : 3; | ||
| 101 | $self->{_restart_next} = 0; | ||
| 102 | |||
| 103 | $self->{_dynamic_wait} = $self->can('restart_next') | ||
| 104 | ? 1 | ||
| 105 | : 0; | ||
| 106 | |||
| 107 | $self->{max_tasks} = $self->can('restart_after') | ||
| 108 | ? $self->restart_after | ||
| 109 | : 0; | ||
| 110 | |||
| 111 | $self->{max_memory} = $self->can('max_memory_per_child') | ||
| 112 | ? $self->max_memory_per_child | ||
| 113 | : 0; | ||
| 114 | |||
| 115 | if ($self->can('post_child_exit')) { | ||
| 116 | $self->{__post_exit} = sub { | ||
| 117 | $self->post_child_exit(@_); | ||
| 118 | }; | ||
| 119 | } | ||
| 120 | |||
| 121 | $self->{_destroyed} = 0; | ||
| 122 | $self->{_childs} = []; | ||
| 123 | $self->{resident} = 0; | ||
| 124 | |||
| 125 | $self->{_monitor_called} = 0; | ||
| 126 | |||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub _destroy { | ||
| 131 | my ($self) = @_; | ||
| 132 | |||
| 133 | return if $self->{_destroyed}; | ||
| 134 | |||
| 135 | if ( $self->resident() ) { | ||
| 136 | INFO "Stopping running child processed"; | ||
| 137 | |||
| 138 | my @pids_to_stop = map { $_->{pid} } @{$self->{_childs}}; | ||
| 139 | TRACE "Killing pids: @pids_to_stop"; | ||
| 140 | kill 'INT', @pids_to_stop; | ||
| 141 | |||
| 142 | INFO "Removing communication socket %s", $self->sockfile; | ||
| 143 | my $sfile = $self->sockfile; | ||
| 144 | $sfile .= '.sock'; | ||
| 145 | unlink $sfile; | ||
| 146 | } | ||
| 147 | |||
| 148 | $self->{_destroyed} = 1; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub DESTROY { | ||
| 152 | my $self = shift; | ||
| 153 | |||
| 154 | $self->_destroy; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub set_resident { | ||
| 158 | $_[0]->{resident} = $_[1]; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub resident { | ||
| 162 | $_[0]->{resident}; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub _get_pid { | ||
| 166 | my $self = shift; | ||
| 167 | |||
| 168 | my $pidfile = $self->{tmp_basename}.'.pid'; | ||
| 169 | if (-e $pidfile) { | ||
| 170 | open my $fh, '<', $pidfile; | ||
| 171 | my ($pid) = <$fh>; | ||
| 172 | |||
| 173 | $pid+=0; | ||
| 174 | |||
| 175 | return $pid; | ||
| 176 | } | ||
| 177 | |||
| 178 | return 0; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub check_pid { | ||
| 182 | my $self = shift; | ||
| 183 | |||
| 184 | my $pid = $self->_get_pid; | ||
| 185 | |||
| 186 | if ($pid and $pid == $$) { | ||
| 187 | return 1; | ||
| 188 | } | ||
| 189 | |||
| 190 | return 0; | ||
| 191 | } | ||
| 192 | |||
| 193 | my %commands = ( | ||
| 194 | start => '_cmd_start_daemon', | ||
| 195 | startfg => '_cmd_start_foreground', | ||
| 196 | startdocker => '_cmd_start_docker', | ||
| 197 | stop => '_cmd_stop_daemon', | ||
| 198 | help => '_cmd_help_daemon', | ||
| 199 | |||
| 200 | __default__ => '_cmd_forward_to_daemon', | ||
| 201 | ); | ||
| 202 | $commands{foreground} = $commands{startfg}; | ||
| 203 | |||
| 204 | sub run { | ||
| 205 | my $self = shift; | ||
| 206 | |||
| 207 | TRACE "starting run with %s", join(" ", @ARGV); | ||
| 208 | |||
| 209 | my @args = @ARGV ? @ARGV : ('help'); | ||
| 210 | |||
| 211 | while (@args) { | ||
| 212 | my $cmd = shift @args; | ||
| 213 | $cmd = lc($cmd); | ||
| 214 | my $method; | ||
| 215 | if ( $commands{$cmd} ) { | ||
| 216 | TRACE "Running command '$cmd'"; | ||
| 217 | $method = $commands{$cmd}; | ||
| 218 | } else { | ||
| 219 | TRACE "Running command '$cmd' with __default__"; | ||
| 220 | $method = $commands{ __default__ }; | ||
| 221 | } | ||
| 222 | $self->$method( $cmd, \@args) if $method | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | sub server_cores { | ||
| 227 | open my $fh, '<', '/proc/stat'; | ||
| 228 | my $cores = 0; | ||
| 229 | while (my $ln = <$fh>) { | ||
| 230 | if ($ln=~m{^cpu(\d+)}) { | ||
| 231 | $cores = 1+ $1; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | return $cores; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub vmsize { | ||
| 239 | local $/ = undef; | ||
| 240 | open my $fh, '<', "/proc/$$/status"; | ||
| 241 | |||
| 242 | my $size = (<$fh> =~ m{^VmSize:\s*(\d+)}m)[0]; | ||
| 243 | return $size * 1024; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub childs_running { | ||
| 247 | my $self = shift; | ||
| 248 | |||
| 249 | return scalar @{ $self->{_childs} || [] }; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub sockfile { | ||
| 253 | my $self = shift; | ||
| 254 | |||
| 255 | my ($fname) = $ENV{DRAGON_SOCKET_FILE}; | ||
| 256 | if ($fname) { | ||
| 257 | $fname =~ s{\W+}{-}g; | ||
| 258 | } else { | ||
| 259 | ($fname) = $0 =~ m{([^/]+)(?:.pl)$}; | ||
| 260 | $fname = 'there-are-dragons' unless $fname; | ||
| 261 | } | ||
| 262 | |||
| 263 | return "/tmp/$fname"; | ||
| 264 | } | ||
| 265 | |||
| 266 | sub get_client_socket { | ||
| 267 | my $self = shift; | ||
| 268 | |||
| 269 | return $self->{client_socket} | ||
| 270 | if $self->{client_socket}; | ||
| 271 | |||
| 272 | my $sockfile = $self->sockfile; | ||
| 273 | $sockfile .= '.sock'; | ||
| 274 | return unless -e $sockfile; | ||
| 275 | |||
| 276 | return $self->{client_socket} | ||
| 277 | = IO::Socket::UNIX->new( | ||
| 278 | Peer => $sockfile, | ||
| 279 | Timeout => 3, | ||
| 280 | ); | ||
| 281 | } | ||
| 282 | |||
| 283 | sub get_listen_socket { | ||
| 284 | my $self = shift; | ||
| 285 | return $self->{server_socket} | ||
| 286 | if $self->{server_socket}; | ||
| 287 | |||
| 288 | my $sfile = $self->sockfile; | ||
| 289 | $sfile .= '.sock'; | ||
| 290 | |||
| 291 | unlink $sfile if -e $sfile; | ||
| 292 | |||
| 293 | $self->{server_socket} = IO::Socket::UNIX->new( | ||
| 294 | Local => $sfile, | ||
| 295 | Type => SOCK_STREAM, | ||
| 296 | Listen => 5 | ||
| 297 | ); | ||
| 298 | |||
| 299 | chmod 0777, $sfile; | ||
| 300 | |||
| 301 | my $pfile = $self->sockfile; | ||
| 302 | $pfile .= '.pid'; | ||
| 303 | open my $fh, '>', $pfile; | ||
| 304 | print $fh $$; | ||
| 305 | close $pfile; | ||
| 306 | |||
| 307 | return $self->{server_socket}; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub daemon_name { | ||
| 311 | my $self = shift; | ||
| 312 | |||
| 313 | my $name = $self->sockfile; | ||
| 314 | ($name) = (split m{/}, $name)[-1]; | ||
| 315 | return "Dragons of $name"; | ||
| 316 | } | ||
| 317 | |||
| 318 | sub check_daemon { | ||
| 319 | my $self = shift; | ||
| 320 | |||
| 321 | if (my $pid = $self->_get_pid()) { | ||
| 322 | my $cnt = kill 0, $pid; | ||
| 323 | return $cnt; | ||
| 324 | |||
| 325 | } else { | ||
| 326 | my $sock = $self->get_client_socket(); | ||
| 327 | |||
| 328 | if ($sock) { | ||
| 329 | print $sock "ping\n"; | ||
| 330 | while ( my $res = <$sock> ) { | ||
| 331 | if ($res =~ m{^pong\s*$}i) { | ||
| 332 | return 1; | ||
| 333 | } | ||
| 334 | } | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | return 0; | ||
| 339 | } | ||
| 340 | |||
| 341 | sub stop { | ||
| 342 | my $self = shift; | ||
| 343 | $self->{stopping} = 1; | ||
| 344 | |||
| 345 | my @pids_to_stop = map { $_->{pid} } @{$self->{_childs}}; | ||
| 346 | TRACE "Notifying the family: @pids_to_stop"; | ||
| 347 | kill 'USR1', @pids_to_stop if @pids_to_stop; | ||
| 348 | } | ||
| 349 | |||
| 350 | sub fork_child { | ||
| 351 | my ($self, $sub) = @_; | ||
| 352 | |||
| 353 | my $pid= fork; | ||
| 354 | if ($pid) { | ||
| 355 | # we are in the parent, and we got a kid | ||
| 356 | my %pid = ( | ||
| 357 | pid => $pid, | ||
| 358 | start_time => time, | ||
| 359 | ); | ||
| 360 | |||
| 361 | push @{ $self->{_childs} }, \%pid; | ||
| 362 | |||
| 363 | return \%pid; | ||
| 364 | } elsif (defined $pid) { | ||
| 365 | srand(); | ||
| 366 | eval { | ||
| 367 | $sub->(); | ||
| 368 | 1; | ||
| 369 | } or do { | ||
| 370 | my $error = $@ || 'Zombie error'; | ||
| 371 | FATAL "Error in child process: %s", $error; | ||
| 372 | }; | ||
| 373 | |||
| 374 | exit 0; | ||
| 375 | } | ||
| 376 | |||
| 377 | FATAL "Could not fork child process: $!"; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub wait_for_children { | ||
| 381 | my $self = shift; | ||
| 382 | |||
| 383 | my @done; | ||
| 384 | |||
| 385 | @{ $self->{_childs} } = grep { | ||
| 386 | my $pid= waitpid $_->{pid}, WNOHANG; | ||
| 387 | |||
| 388 | if ($pid) { | ||
| 389 | my $ec= $?; | ||
| 390 | if ($ec & 127) { | ||
| 391 | $_->{exit_code}= $ec; | ||
| 392 | $_->{exit_with_signal}= ($ec & 127); | ||
| 393 | $_->{exit_with_coredump}= !!($ec & 128); | ||
| 394 | } else { | ||
| 395 | $_->{exit_code}= $ec >> 8; | ||
| 396 | } | ||
| 397 | |||
| 398 | $_->{exit_time}= time; | ||
| 399 | $_->{elapsed_time}= $_->{exit_time} - $_->{start_time}; | ||
| 400 | |||
| 401 | push @done, $_; | ||
| 402 | } | ||
| 403 | |||
| 404 | !$pid; | ||
| 405 | } @{ $self->{_childs} }; | ||
| 406 | |||
| 407 | return \@done; | ||
| 408 | } | ||
| 409 | |||
| 410 | sub is_foreground { | ||
| 411 | my ($self) = @_; | ||
| 412 | |||
| 413 | $self->{_foreground}; | ||
| 414 | } | ||
| 415 | |||
| 416 | ################################################################ | ||
| 417 | ## Child code - init and run the child process/task | ||
| 418 | ################################################################ | ||
| 419 | |||
| 420 | sub halt { | ||
| 421 | my $self = shift; | ||
| 422 | |||
| 423 | $self->{_halt} = 1; | ||
| 424 | } | ||
| 425 | |||
| 426 | sub child_main_run { | ||
| 427 | my $self = shift; | ||
| 428 | |||
| 429 | $SIG{USR1} = sub { | ||
| 430 | $self->halt; | ||
| 431 | }; | ||
| 432 | |||
| 433 | TRACE "Starting child_main_run"; | ||
| 434 | |||
| 435 | $self->init_child | ||
| 436 | if $self->can('init_child'); | ||
| 437 | |||
| 438 | if ($self->can('data_consumer')) { | ||
| 439 | TRACE "Going to get a data consumer"; | ||
| 440 | my $dc = $self->data_consumer(); | ||
| 441 | $dc->consume( sub { $self->_dc_wrapper( @_ ) } ); | ||
| 442 | |||
| 443 | } elsif ( $self->can('claim_task') ) { | ||
| 444 | TRACE "Using claim task"; | ||
| 445 | $self->_claim_wrapper(); | ||
| 446 | |||
| 447 | } else { | ||
| 448 | $self->_nodc_wrapper(); | ||
| 449 | } | ||
| 450 | |||
| 451 | $self->end_child | ||
| 452 | if $self->can('end_child'); | ||
| 453 | } | ||
| 454 | |||
| 455 | sub _dc_wrapper { | ||
| 456 | my ($self, $dc, $id) = @_; | ||
| 457 | |||
| 458 | my $error = ''; | ||
| 459 | eval { | ||
| 460 | $self->main($id); | ||
| 461 | 1; | ||
| 462 | } or do { | ||
| 463 | $error = $@ || 'zombie error'; | ||
| 464 | }; | ||
| 465 | |||
| 466 | $self->_task_done($error); | ||
| 467 | $dc->halt if $self->{_halt}; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub _claim_wrapper { | ||
| 471 | my $self = shift; | ||
| 472 | |||
| 473 | TASK: | ||
| 474 | while (!$self->{_halt}) { | ||
| 475 | my $error; | ||
| 476 | |||
| 477 | my $task; | ||
| 478 | eval { | ||
| 479 | $task = $self->claim_task(); 1 | ||
| 480 | } or do { | ||
| 481 | $error = $@ || 'zombie error'; | ||
| 482 | FATAL "error claiming task: $error"; | ||
| 483 | }; | ||
| 484 | |||
| 485 | last unless $task; | ||
| 486 | eval { | ||
| 487 | $self->main($task); | ||
| 488 | $self->task_done($task) | ||
| 489 | if $self->can('task_done'); | ||
| 490 | 1; | ||
| 491 | } or do { | ||
| 492 | $error = $@ || 'zombie error'; | ||
| 493 | $self->task_failed($task) | ||
| 494 | if $self->can('task_failed'); | ||
| 495 | }; | ||
| 496 | |||
| 497 | $self->_task_done($error); | ||
| 498 | } | ||
| 499 | } | ||
| 500 | |||
| 501 | sub _nodc_wrapper { | ||
| 502 | my $self = shift; | ||
| 503 | |||
| 504 | # let's assume that if we don't have a Data::Consumer, | ||
| 505 | # main know what it needs to do. | ||
| 506 | eval { | ||
| 507 | $self->main(); | ||
| 508 | 1; | ||
| 509 | } or do { | ||
| 510 | my $error = $@ || 'zombie error'; | ||
| 511 | warn $error,"\n"; | ||
| 512 | }; | ||
| 513 | } | ||
| 514 | |||
| 515 | sub _task_done { | ||
| 516 | my ($self, $error) = @_; | ||
| 517 | |||
| 518 | $self->{tasks_done}++; | ||
| 519 | $self->{vmsize} = vmsize; | ||
| 520 | |||
| 521 | TRACE "TASKDONE: $self->{tasks_done}: $self->{vmsize}"; | ||
| 522 | INFO " ==> %s \n===END OF ERROR", $error if $error; | ||
| 523 | |||
| 524 | if ($self->{max_tasks} and $self->{tasks_done} >= $self->{max_tasks}) { | ||
| 525 | INFO "STOPPING CHILD: done $self->{tasks_done} tasks"; | ||
| 526 | $self->halt; | ||
| 527 | } | ||
| 528 | if ($self->{max_memory} and $self->{vmsize} >= $self->{max_memory}) { | ||
| 529 | INFO "STOPPING CHILD: exceeded max memory '$self->{vmsize}' of '$self->{max_memory}' used"; | ||
| 530 | $self->halt; | ||
| 531 | } | ||
| 532 | |||
| 533 | return; | ||
| 534 | } | ||
| 535 | |||
| 536 | |||
| 537 | ################################################################# | ||
| 538 | ## Daemon control methods - command line handlers | ||
| 539 | ################################################################# | ||
| 540 | |||
| 541 | sub _cmd_start_daemon { | ||
| 542 | my $self = shift; | ||
| 543 | |||
| 544 | if ($self->check_daemon()) { | ||
| 545 | INFO "Daemon is running - not starting again" unless IS_CRONJOB; | ||
| 546 | } else { | ||
| 547 | if ($self->can('pre_start')) { | ||
| 548 | $self->pre_start(); | ||
| 549 | if ($self->{stopping}) { | ||
| 550 | INFO "Start prevented by pre_start" unless IS_CRONJOB; | ||
| 551 | return; | ||
| 552 | } | ||
| 553 | } | ||
| 554 | my $pid = fork(); | ||
| 555 | if ($pid) { | ||
| 556 | sleep 1; | ||
| 557 | my $cnt= 30; | ||
| 558 | $cnt = 60 if IS_CRONJOB; | ||
| 559 | while ($cnt--) { | ||
| 560 | if ($self->check_daemon()) { | ||
| 561 | INFO "Daemon Started" unless IS_CRONJOB; | ||
| 562 | return; #done starting | ||
| 563 | } | ||
| 564 | sleep 1; | ||
| 565 | } | ||
| 566 | FATAL "Not able to start the daemon"; | ||
| 567 | } elsif (defined $pid) { | ||
| 568 | |||
| 569 | my $bfname = $self->sockfile; | ||
| 570 | open (STDIN, '<','/dev/null'); | ||
| 571 | open STDOUT, '>>', $bfname.'.out' | ||
| 572 | or die "Failed to redirect STDOUT"; | ||
| 573 | open STDERR, '>>', $bfname.'.err' | ||
| 574 | or die "Failled to redirect STDERR"; | ||
| 575 | |||
| 576 | $self->_server_main(); | ||
| 577 | } else { | ||
| 578 | FATAL "Not able to start the daemon"; | ||
| 579 | } | ||
| 580 | } | ||
| 581 | } | ||
| 582 | |||
| 583 | sub _cmd_start_docker { | ||
| 584 | my $self = shift; | ||
| 585 | |||
| 586 | if ($self->can('pre_start')) { | ||
| 587 | $self->pre_start(); | ||
| 588 | if ($self->{stopping}) { | ||
| 589 | INFO "Start prevented by pre_start" unless IS_CRONJOB; | ||
| 590 | return; | ||
| 591 | } | ||
| 592 | } | ||
| 593 | |||
| 594 | $self->_server_main(); | ||
| 595 | } | ||
| 596 | |||
| 597 | sub _cmd_start_foreground { | ||
| 598 | my $self = shift; | ||
| 599 | |||
| 600 | TRACE "Starting in the foreground"; | ||
| 601 | |||
| 602 | $self->{_foreground} = 1; | ||
| 603 | |||
| 604 | if ($self->can('pre_start')) { | ||
| 605 | $self->pre_start(); | ||
| 606 | if ($self->{stopping}) { | ||
| 607 | INFO "Start prevented by pre_start" unless IS_CRONJOB; | ||
| 608 | return; | ||
| 609 | } | ||
| 610 | } | ||
| 611 | if ($self->can('pre_fork')) { | ||
| 612 | TRACE "Calling pre_fork"; | ||
| 613 | eval { | ||
| 614 | $self->pre_fork(); | ||
| 615 | 1; | ||
| 616 | } or do { | ||
| 617 | my $error = $@; | ||
| 618 | FATAL "error on pre_fork: $error\n"; | ||
| 619 | } | ||
| 620 | } | ||
| 621 | |||
| 622 | $self->child_main_run(); | ||
| 623 | } | ||
| 624 | |||
| 625 | sub _cmd_stop_daemon { | ||
| 626 | my $self = shift; | ||
| 627 | |||
| 628 | my $sock = $self->get_client_socket(); | ||
| 629 | |||
| 630 | # If there is no socket it is not running, so just exit. | ||
| 631 | do { | ||
| 632 | DEBUG "Daemon is not running"; | ||
| 633 | exit; | ||
| 634 | } unless $sock; | ||
| 635 | |||
| 636 | # If there is socket, forward the command to the daemon | ||
| 637 | $self->_cmd_forward_to_daemon(@_); | ||
| 638 | } | ||
| 639 | |||
| 640 | sub _cmd_forward_to_daemon { | ||
| 641 | my $self = shift; | ||
| 642 | my ($cmd, $others) = @_; | ||
| 643 | |||
| 644 | my $sock = $self->get_client_socket(); | ||
| 645 | |||
| 646 | FATAL "Can't connect to the Daemon" | ||
| 647 | unless $sock; | ||
| 648 | |||
| 649 | print $sock join ' ', $cmd, @$others, "\n"; | ||
| 650 | while (my $res = <$sock>) { | ||
| 651 | print STDERR $res if -t STDERR; | ||
| 652 | #reply are already \n terminated | ||
| 653 | } | ||
| 654 | |||
| 655 | @$others = (); | ||
| 656 | return; | ||
| 657 | } | ||
| 658 | |||
| 659 | sub _cmd_help_daemon { | ||
| 660 | print STDERR <<EoH; | ||
| 661 | Usage: $0 (help|startfg|start|stop|ping) | ||
| 662 | |||
| 663 | * help - prints this screen | ||
| 664 | * startfg - starts the dameon, but single processed, in the foreground | ||
| 665 | * start - starts and daemonizes | ||
| 666 | * stop - stops a running daemon | ||
| 667 | * ping - checks if there is a running daemon (prints pong is true) | ||
| 668 | |||
| 669 | |||
| 670 | EoH | ||
| 671 | |||
| 672 | return; | ||
| 673 | } | ||
| 674 | |||
| 675 | ####################################################################### | ||
| 676 | # Daemon main | ||
| 677 | ####################################################################### | ||
| 678 | |||
| 679 | sub _server_main { | ||
| 680 | my $self = shift; | ||
| 681 | |||
| 682 | my $sock = $self->get_listen_socket; | ||
| 683 | |||
| 684 | my $select = IO::Select->new(); | ||
| 685 | $select->add( $sock ); | ||
| 686 | |||
| 687 | $self->set_resident(1); | ||
| 688 | |||
| 689 | INFO "Daemon Started"; | ||
| 690 | |||
| 691 | $self->{_restart_next} = time + 3; | ||
| 692 | # let the parent know that we are alive! | ||
| 693 | |||
| 694 | while ( 1 ) { | ||
| 695 | if (!$self->check_pid and !$self->{stopping}) { | ||
| 696 | $self->stop; | ||
| 697 | INFO "Stopping - other pid running\n"; | ||
| 698 | } | ||
| 699 | |||
| 700 | $self->communicate() | ||
| 701 | if $select->can_read( 0.01 ); | ||
| 702 | |||
| 703 | $self->_sm_start_child(); | ||
| 704 | |||
| 705 | usleep 100_000; | ||
| 706 | } | ||
| 707 | } | ||
| 708 | |||
| 709 | sub _sm_start_child { | ||
| 710 | my $self = shift; | ||
| 711 | |||
| 712 | my $done = 0; | ||
| 713 | |||
| 714 | my $finished = wait_for_children( $self ); | ||
| 715 | if ($finished and @$finished and $self->can('post_child_exit')) { | ||
| 716 | $self->post_child_exit( $_ ) | ||
| 717 | for @$finished; | ||
| 718 | |||
| 719 | $done += @$finished; | ||
| 720 | } | ||
| 721 | |||
| 722 | if ( $self->{_monitor_called} < time ) { | ||
| 723 | $self->{_monitor_called} = time; | ||
| 724 | $self->monitor | ||
| 725 | if $self->can('monitor'); | ||
| 726 | |||
| 727 | $done++; | ||
| 728 | } | ||
| 729 | |||
| 730 | if ($self->{stopping}) { | ||
| 731 | unless ( scalar @{$self->{_childs}} ) { | ||
| 732 | INFO "Stopping Daemon"; | ||
| 733 | $self->pre_exit | ||
| 734 | if $self->can('pre_exit'); | ||
| 735 | |||
| 736 | $self->_destroy; | ||
| 737 | exit; | ||
| 738 | } | ||
| 739 | |||
| 740 | return; | ||
| 741 | } | ||
| 742 | |||
| 743 | return $self->_idle( $done ) | ||
| 744 | if $self->{_restart_next} > time; | ||
| 745 | |||
| 746 | return $self->_idle( $done ) | ||
| 747 | if scalar @{$self->{_childs}} >= $self->{max_childs}; | ||
| 748 | |||
| 749 | $self->{need_childs} = $self->can('need_childs') | ||
| 750 | ? $self->need_childs | ||
| 751 | : $self->{max_childs}; | ||
| 752 | |||
| 753 | return $self->_idle( $done ) | ||
| 754 | if scalar @{$self->{_childs}} >= $self->{need_childs}; | ||
| 755 | |||
| 756 | TRACE "Going to start a child %d => %d", | ||
| 757 | scalar @{$self->{_childs}}, $self->{need_childs}; | ||
| 758 | |||
| 759 | TRACE "can pre_fork: %s => %s", $self->can('pre_fork'), | ||
| 760 | $self->can('pre_fork')?1:0; | ||
| 761 | |||
| 762 | if ($self->can('pre_fork')) { | ||
| 763 | TRACE "Calling pre_fork"; | ||
| 764 | eval { | ||
| 765 | $self->pre_fork(); | ||
| 766 | 1; | ||
| 767 | } or do { | ||
| 768 | my $error = $@; | ||
| 769 | FATAL "error on pre_fork: $error\n"; | ||
| 770 | } | ||
| 771 | } | ||
| 772 | |||
| 773 | my $child = fork_child( # fork_child never returns on the child | ||
| 774 | $self, | ||
| 775 | sub { | ||
| 776 | $self->set_resident(0); | ||
| 777 | |||
| 778 | TRACE "Starting child_main_run"; | ||
| 779 | |||
| 780 | $self->child_main_run(); | ||
| 781 | } | ||
| 782 | ); | ||
| 783 | |||
| 784 | if ($self->can('post_child_start') ) { | ||
| 785 | TRACE "Calling post_child_start"; | ||
| 786 | eval { | ||
| 787 | $self->post_child_start( $child ); | ||
| 788 | 1; | ||
| 789 | } or do { | ||
| 790 | my $error = $@; | ||
| 791 | FATAL "error on post_child_start: $error\n"; | ||
| 792 | } | ||
| 793 | } | ||
| 794 | |||
| 795 | if ($self->{_dynamic_wait}) { | ||
| 796 | $self->{_restart_next} = $self->restart_next; | ||
| 797 | } else { | ||
| 798 | $self->{_restart_next} = time + $self->{_wait}; | ||
| 799 | } | ||
| 800 | } | ||
| 801 | |||
| 802 | sub _idle { | ||
| 803 | my ($self,$done) = @_; | ||
| 804 | |||
| 805 | return if $done; | ||
| 806 | |||
| 807 | $self->idle() | ||
| 808 | if $self->can('idle'); | ||
| 809 | |||
| 810 | return; | ||
| 811 | } | ||
| 812 | |||
| 813 | ####################################################################### | ||
| 814 | # UnixSocket Client Session | ||
| 815 | ####################################################################### | ||
| 816 | |||
| 817 | my %daemon_commands = ( | ||
| 818 | stop => \&_us_cmd_stop_daemon, | ||
| 819 | pause => \&_us_cmd_pause_daemon, | ||
| 820 | play => \&_us_cmd_play_daemon, | ||
| 821 | ping => \&_us_cmd_ping, | ||
| 822 | info => \&_us_cmd_info, | ||
| 823 | ); | ||
| 824 | |||
| 825 | sub communicate { | ||
| 826 | my $self = shift; | ||
| 827 | |||
| 828 | my $socket = $self->get_listen_socket(); | ||
| 829 | my $conn = $socket->accept(); | ||
| 830 | |||
| 831 | local $SIG{ALRM} = sub { TRACE "Timeout in communication attempt. Killing server process\n"; exit 1; }; | ||
| 832 | |||
| 833 | alarm(10); | ||
| 834 | |||
| 835 | my $input = <$conn>; | ||
| 836 | |||
| 837 | my @args = split m{\s+}, $input; | ||
| 838 | |||
| 839 | while (my $cmd = shift @args) { | ||
| 840 | $cmd = lc $cmd; | ||
| 841 | if ($daemon_commands{ $cmd } ) { | ||
| 842 | my @res = $daemon_commands{ $cmd }->( | ||
| 843 | $self, $cmd, \@args | ||
| 844 | ); | ||
| 845 | |||
| 846 | TRACE "Replying to $cmd: ", @res; | ||
| 847 | |||
| 848 | print $conn "$cmd reply:\n"; | ||
| 849 | print $conn $_,"\n" for @res; | ||
| 850 | } else { | ||
| 851 | print $conn "Command '$cmd' is not known\n"; | ||
| 852 | } | ||
| 853 | |||
| 854 | if ($self->{stopping}) { | ||
| 855 | TRACE "Stopping now"; | ||
| 856 | exit 0; | ||
| 857 | } | ||
| 858 | } | ||
| 859 | |||
| 860 | alarm 0; | ||
| 861 | $conn->close; | ||
| 862 | } | ||
| 863 | |||
| 864 | ###################################################################### | ||
| 865 | # UnixSocket command handlers - handle piped commands | ||
| 866 | ###################################################################### | ||
| 867 | |||
| 868 | sub _us_cmd_stop_daemon { | ||
| 869 | my $self = shift; | ||
| 870 | my $cmd = shift; | ||
| 871 | my $args = shift; | ||
| 872 | |||
| 873 | INFO "Got a request to stop - waiting for childs and exiting"; | ||
| 874 | $self->stop; | ||
| 875 | |||
| 876 | return ('stopping'); | ||
| 877 | } | ||
| 878 | |||
| 879 | sub _us_cmd_ping { | ||
| 880 | my $self = shift; | ||
| 881 | my ($cmd, $args) = @_; | ||
| 882 | |||
| 883 | return ('pong'); | ||
| 884 | } | ||
| 885 | |||
| 886 | sub _us_cmd_info { | ||
| 887 | my $self = shift; | ||
| 888 | |||
| 889 | my $name = $self->daemon_name; | ||
| 890 | my $childs = scalar @{ $self->{_childs} }; | ||
| 891 | |||
| 892 | my $next = localtime($self->{_restart_next}); | ||
| 893 | my $stopping = $self->{stopping} ? "\nDaemon is Stopping\n":""; | ||
| 894 | |||
| 895 | my $res = <<EoI; | ||
| 896 | $name [server pid: $$] | ||
| 897 | |||
| 898 | Children: $childs of $self->{max_childs} running | ||
| 899 | Next Start: $self->{_restart_next} [$next] | ||
| 900 | |||
| 901 | MaxTasks: $self->{max_tasks} | ||
| 902 | MaxMemory: $self->{max_memory} | ||
| 903 | $stopping | ||
| 904 | |||
| 905 | |||
| 906 | EoI | ||
| 907 | |||
| 908 | return ($res); | ||
| 909 | } | ||
| 910 | |||
| 911 | |||
| 912 | 1; | ||
| 913 | __END__ |