| 1 | package POE::Component::Server::AsyncEndpoint; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | our @EXPORT = ( ); | ||
| 6 | use base qw(Exporter); | ||
| 7 | use vars qw($VERSION); | ||
| 8 | $VERSION = '0.10'; | ||
| 9 | |||
| 10 | |||
| 11 | use POE; | ||
| 12 | use POE::Wheel::Run; | ||
| 13 | use POE::Component::IKC::Server; | ||
| 14 | use POE::Component::Server::AsyncEndpoint::Config; | ||
| 15 | use POE::Component::Server::AsyncEndpoint::Endpoints; | ||
| 16 | use POE::Component::Logger; | ||
| 17 | use POE::Component::MessageQueue; | ||
| 18 | use POE::Component::MessageQueue::Storage::DBI; | ||
| 19 | use POE::Component::MessageQueue::Storage::FileSystem; | ||
| 20 | use Carp qw(croak); | ||
| 21 | use Switch; | ||
| 22 | |||
| 23 | use constant SHUTDOWN_SIGNALS => ('TERM', 'HUP', 'INT'); | ||
| 24 | |||
| 25 | autoflush STDOUT; | ||
| 26 | |||
| 27 | sub run { | ||
| 28 | POE::Kernel->run(); | ||
| 29 | exit(0); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | my $package = shift; | ||
| 34 | croak "$package requires an even number of parameters" if @_ & 1; | ||
| 35 | my %args = @_; | ||
| 36 | my $self = bless ({}, $package); | ||
| 37 | |||
| 38 | $args{Alias} = 'AES-Master' unless $args{Alias}; | ||
| 39 | |||
| 40 | $self->{CONFIG} = \%args; | ||
| 41 | $self->{alias} = $args{Alias}; | ||
| 42 | |||
| 43 | # init server configuration | ||
| 44 | $self->{config} = POE::Component::Server::AsyncEndpoint::Config->init(); | ||
| 45 | |||
| 46 | # get endpoint filenames | ||
| 47 | $self->{endpoints} = [POE::Component::Server::AsyncEndpoint::Endpoints->init()]; | ||
| 48 | |||
| 49 | # spawn the logger | ||
| 50 | POE::Component::Logger->spawn( | ||
| 51 | ConfigFile => $self->{config}->aes_log_conf, | ||
| 52 | Alias => 'MAIN-Logger' | ||
| 53 | ); | ||
| 54 | |||
| 55 | # spawn the MQ Server | ||
| 56 | my $mq = POE::Component::MessageQueue->new( | ||
| 57 | port => $self->{config}->aes_port, | ||
| 58 | address => $self->{config}->aes_addr, | ||
| 59 | hostname => $self->{config}->aes_host, | ||
| 60 | logger_alias => 'MAIN-Logger', | ||
| 61 | storage => POE::Component::MessageQueue::Storage::FileSystem->new( | ||
| 62 | info_storage => POE::Component::MessageQueue::Storage::DBI->new( | ||
| 63 | dsn => $self->{config}->mqdb_dsn, | ||
| 64 | username => $self->{config}->mqdb_username, | ||
| 65 | password => $self->{config}->mqdb_password, | ||
| 66 | ), | ||
| 67 | data_dir => $self->{config}->mqdb_data_dir, | ||
| 68 | ), | ||
| 69 | ); | ||
| 70 | |||
| 71 | $self->{mq} = $mq; | ||
| 72 | |||
| 73 | # IKC Server | ||
| 74 | POE::Component::IKC::Server->spawn( | ||
| 75 | address => $self->{config}->aes_ikc_addr, | ||
| 76 | port => $self->{config}->aes_ikc_port, | ||
| 77 | name => "IKCS", | ||
| 78 | ); | ||
| 79 | |||
| 80 | |||
| 81 | # AES main session | ||
| 82 | POE::Session->create( | ||
| 83 | object_states => [ | ||
| 84 | $self => { | ||
| 85 | _start => '_aes_start', | ||
| 86 | shutdown => 'shutdown_start', | ||
| 87 | _stop => '_aes_stop', | ||
| 88 | }, | ||
| 89 | $self => [ qw( init_endpoints endpoint_stdout endpoint_stderr | ||
| 90 | child_sig child_dead endpoint_restart | ||
| 91 | endpoint_start endpoint_stop endpoint_wdog | ||
| 92 | shutdown_complete ikc_event) ], | ||
| 93 | ], | ||
| 94 | (ref $args{options} eq 'HASH' ? (options => $args{options}) : () ), | ||
| 95 | ); | ||
| 96 | |||
| 97 | |||
| 98 | POE::Kernel->post('MAIN-Logger', 'alert', "AES Initialize complete, ready to run.\n"); | ||
| 99 | return $self; | ||
| 100 | |||
| 101 | } | ||
| 102 | |||
| 103 | # only for debug | ||
| 104 | sub child_sig { | ||
| 105 | $_[KERNEL]->post('MAIN-Logger','debug',"CHLD:".$_[ARG1]."\n"); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | sub _aes_start { | ||
| 110 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 111 | $kernel->alias_set($self->{alias}); | ||
| 112 | $kernel->alias_set('AES_SESSION'); # for IKC | ||
| 113 | $kernel->sig(CHLD => 'child_sig'); | ||
| 114 | foreach my $signal ( SHUTDOWN_SIGNALS ){ | ||
| 115 | $kernel->sig($signal => 'shutdown'); | ||
| 116 | } | ||
| 117 | $kernel->yield('init_endpoints'); | ||
| 118 | $kernel->post('IKC', 'publish', 'AES_SESSION', ['ikc_event']); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _aes_stop { | ||
| 122 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub init_endpoints { | ||
| 127 | my ($kernel, $session, $self) = @_[KERNEL, SESSION, OBJECT]; | ||
| 128 | $kernel->post('MAIN-Logger', 'alert', "Initializing endpoints...\n"); | ||
| 129 | my ($name, $pname, $wid, $pid) = undef; | ||
| 130 | foreach my $ep (@{$self->{endpoints}}) { | ||
| 131 | $name = $ep->{name}; | ||
| 132 | $pname = $ep->{pname}; | ||
| 133 | if($ep->{start} == 1){ | ||
| 134 | $ep->{wheel} = &new_wheel($pname); | ||
| 135 | $ep->{stat} = EP_STAT_OK; | ||
| 136 | $wid = $ep->{wheel}->ID; | ||
| 137 | $pid = $ep->{wheel}->PID; | ||
| 138 | $kernel->post('MAIN-Logger', 'alert', | ||
| 139 | "Started EP: $name in wheel id: $wid and PID: $pid\n"); | ||
| 140 | } | ||
| 141 | else{ | ||
| 142 | $ep->{stat} = EP_STOPPED; | ||
| 143 | $kernel->post('MAIN-Logger', 'alert', | ||
| 144 | "EP: $name not started at AES init per EP config\n"); | ||
| 145 | } | ||
| 146 | |||
| 147 | } | ||
| 148 | # reset watchdog | ||
| 149 | $kernel->delay_set('endpoint_wdog', $self->{config}->aes_cwdg); | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | # IKC messaging with Endpoints | ||
| 154 | # FUTURE: get rid of "Endpoint Protocol" and replace with | ||
| 155 | # more IKC published methods | ||
| 156 | sub ikc_event { | ||
| 157 | my ($kernel, $self, $input) = @_[KERNEL, OBJECT, ARG0]; | ||
| 158 | |||
| 159 | $kernel->post('MAIN-Logger', 'debug', "ikc_event input : $input\n"); | ||
| 160 | |||
| 161 | my ($cmd, $pid, $msg) = split /\|/,$input; | ||
| 162 | |||
| 163 | # Simple Endpoint Protocol via IKC | ||
| 164 | switch($cmd){ | ||
| 165 | # status update | ||
| 166 | case "CASTAT" { | ||
| 167 | my $newstat = EP_STAT_FA; | ||
| 168 | if ($msg eq "OK") { | ||
| 169 | $kernel->post( | ||
| 170 | 'MAIN-Logger', | ||
| 171 | 'notice', | ||
| 172 | "Enpoint with PID $pid is OK\n" | ||
| 173 | ); | ||
| 174 | $newstat = EP_STAT_OK; | ||
| 175 | } | ||
| 176 | if ($msg eq "FAIL") { | ||
| 177 | $kernel->post( | ||
| 178 | 'MAIN-Logger', | ||
| 179 | 'notice', | ||
| 180 | "Enpoint with PID $pid is alive but in FAIL state\n" | ||
| 181 | ); | ||
| 182 | } | ||
| 183 | # update endpoint status | ||
| 184 | foreach my $ep (@{$self->{endpoints}}) { | ||
| 185 | next if $ep->{stat} == EP_STOPPED; | ||
| 186 | my $wheel = $ep->{wheel}; | ||
| 187 | if ($wheel->PID == $pid) { | ||
| 188 | $ep->{stat} = $newstat; | ||
| 189 | $ep->{retries} = 0; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | # log to main logger | ||
| 194 | case "LOGIT" { | ||
| 195 | my ($log_alias, $log_level, $log_msg) = split /\;;/,$msg; | ||
| 196 | $kernel->post('MAIN-Logger', $log_level, "$log_alias: ".$log_msg); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | # any STDERR blurb gets logged | ||
| 203 | sub endpoint_stdout { | ||
| 204 | my ($kernel, $input, $wheel_id) = @_[KERNEL, ARG0, ARG1]; | ||
| 205 | $kernel->post('MAIN-Logger', 'alert', "ERROR: Enpoint in wheel $wheel_id wrote to STDOUT: $input\n"); | ||
| 206 | } | ||
| 207 | |||
| 208 | # any STDERR blurb gets logged | ||
| 209 | sub endpoint_stderr { | ||
| 210 | my ($kernel, $input, $wheel_id) = @_[KERNEL, ARG0, ARG1]; | ||
| 211 | $kernel->post('MAIN-Logger', 'alert', "ERROR: Enpoint in wheel $wheel_id wrote to STDERR: $input\n"); | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | # mark dead EP as failed | ||
| 216 | sub child_dead { | ||
| 217 | my ($kernel, $self, $wheel_id) = @_[KERNEL, OBJECT, ARG0]; | ||
| 218 | foreach my $ep (@{$self->{endpoints}}) { | ||
| 219 | next if $ep->{stat} == EP_STOPPED; | ||
| 220 | if ($ep->{wheel}->ID == $wheel_id) { | ||
| 221 | return if $self->{shutdown}; | ||
| 222 | my $pid = $ep->{wheel}->PID; | ||
| 223 | $kernel->post('MAIN-Logger', 'alert', "Set FAIL on EP of dead child wheel id:$wheel_id PID: $pid\n"); | ||
| 224 | $ep->{stat} = EP_STAT_FA; | ||
| 225 | $ep->{retries} = 0; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | # delayed EP restart procedure | ||
| 230 | $_[KERNEL]->delay_set('endpoint_restart',$self->{config}->aes_rstc,$_[ARG0]); | ||
| 231 | |||
| 232 | } | ||
| 233 | |||
| 234 | # Cleans the old wheel and starts a fresh endpoint in it's place. This | ||
| 235 | # sub is delayed (see main AES session) because the endpoint could be | ||
| 236 | # dying emmediately and hogging the whole system in an infinite loop. | ||
| 237 | sub endpoint_restart { | ||
| 238 | |||
| 239 | my ($kernel, $self, $wheel_id) = @_[KERNEL, OBJECT, ARG0]; | ||
| 240 | |||
| 241 | $kernel->post('MAIN-Logger', 'debug', "endpoint_restart()\n"); | ||
| 242 | |||
| 243 | # find the wheel and restart endpoint | ||
| 244 | foreach my $ep (@{$self->{endpoints}}) { | ||
| 245 | next if $ep->{stat} == EP_STOPPED; | ||
| 246 | my $wheel = $ep->{wheel}; | ||
| 247 | my $name = $ep->{name}; | ||
| 248 | my $pname = $ep->{pname}; | ||
| 249 | if ($wheel->ID == $wheel_id) { | ||
| 250 | $ep->{wheel} = &new_wheel($pname); | ||
| 251 | $ep->{stat} = EP_STAT_OK; | ||
| 252 | $kernel->post( | ||
| 253 | 'MAIN-Logger', | ||
| 254 | 'alert', | ||
| 255 | "Restarting EP: $name in wheel ". | ||
| 256 | $ep->{wheel}->ID." with new PID:".$ep->{wheel}->PID."\n"); | ||
| 257 | } | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | # starts a stopped endpoint | ||
| 263 | sub endpoint_start { | ||
| 264 | my ($kernel, $self, $name) = @_[KERNEL, OBJECT, ARG0]; | ||
| 265 | |||
| 266 | $kernel->post('MAIN-Logger', 'debug', "endpoint_start()\n"); | ||
| 267 | |||
| 268 | foreach my $ep (@{$self->{endpoints}}) { | ||
| 269 | if ($ep->{name} eq $name) { | ||
| 270 | if($ep->{stat} == EP_STOPPED){ | ||
| 271 | $ep->{wheel} = &new_wheel($ep->{pname}); | ||
| 272 | $ep->{stat} = EP_STAT_OK; | ||
| 273 | $kernel->post( | ||
| 274 | 'MAIN-Logger', 'alert', | ||
| 275 | "Restarting EP: $name in wheel ". | ||
| 276 | $ep->{wheel}->ID." with new PID:".$ep->{wheel}->PID."\n"); | ||
| 277 | } | ||
| 278 | else{ | ||
| 279 | $kernel->post( | ||
| 280 | 'MAIN-Logger', 'alert', | ||
| 281 | "Cannot start EP: $name with stat".$ep->{stat}."\n"); | ||
| 282 | } | ||
| 283 | } | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | # stop an endpoint in runtime | ||
| 288 | sub endpoint_stop { | ||
| 289 | my ($kernel, $self, $name) = @_[KERNEL, OBJECT, ARG0]; | ||
| 290 | |||
| 291 | $kernel->post('MAIN-Logger', 'debug', "endpoint_stop()\n"); | ||
| 292 | |||
| 293 | foreach my $ep (@{$self->{endpoints}}) { | ||
| 294 | my $wheel = $ep->{wheel}; | ||
| 295 | if ($ep->{name} eq $name) { | ||
| 296 | if($ep->{stat} == EP_STAT_OK){ | ||
| 297 | $ep->{stat} = EP_STOPPED; | ||
| 298 | $wheel->kill($SIG{TERM}); | ||
| 299 | $kernel->post( | ||
| 300 | 'MAIN-Logger', 'alert', | ||
| 301 | "Stopping EP: $name in wheel ". | ||
| 302 | $ep->{wheel}->ID." with new PID:".$ep->{wheel}->PID."\n"); | ||
| 303 | } | ||
| 304 | else{ | ||
| 305 | $kernel->post( | ||
| 306 | 'MAIN-Logger', 'alert', | ||
| 307 | "Cannot stop EP: $name with stat".$ep->{stat}."\n"); | ||
| 308 | } | ||
| 309 | } | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | |||
| 314 | # spawns new wheel | ||
| 315 | sub new_wheel { | ||
| 316 | my $pname = shift; | ||
| 317 | my $wheel = POE::Wheel::Run->new( | ||
| 318 | Program => $pname, | ||
| 319 | StdoutEvent => 'endpoint_stdout', | ||
| 320 | StderrEvent => 'endpoint_stderr', | ||
| 321 | CloseEvent => 'child_dead', | ||
| 322 | ); | ||
| 323 | return $wheel; | ||
| 324 | } | ||
| 325 | |||
| 326 | # the endpoint watchdog | ||
| 327 | sub endpoint_wdog { | ||
| 328 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 329 | |||
| 330 | return if $self->{shutdown}; | ||
| 331 | |||
| 332 | # watchdog blurb is notice level | ||
| 333 | $kernel->post('MAIN-Logger', 'notice', "--- ENTERING Endpoint Watchdog ---\n"); | ||
| 334 | |||
| 335 | foreach my $ep (@{$self->{endpoints}}) { | ||
| 336 | my $stat = \$ep->{stat}; | ||
| 337 | next if $$stat == EP_STOPPED; | ||
| 338 | |||
| 339 | my $name = $ep->{name}; | ||
| 340 | my $pname = $ep->{pname}; | ||
| 341 | my $retries = \$ep->{retries}; | ||
| 342 | my $wheel = $ep->{wheel}; | ||
| 343 | my $pid = $wheel->PID; | ||
| 344 | |||
| 345 | $$retries++; | ||
| 346 | |||
| 347 | # debug | ||
| 348 | $kernel->post('MAIN-Logger', 'debug', "$pname RETRIES: $$retries STATUS:$$stat\n"); | ||
| 349 | |||
| 350 | # time to kill non-answering process | ||
| 351 | if ( | ||
| 352 | ($$retries > $self->{config}->aes_wdgr) and | ||
| 353 | ($$stat == EP_STAT_WA) | ||
| 354 | ) { | ||
| 355 | # TODO: probable portability issue to non-unix | ||
| 356 | # systems. Not a prority for me right now, but a helping | ||
| 357 | # hand on non-unix machine is very welcome indeed. | ||
| 358 | |||
| 359 | # this blurb is surely critical | ||
| 360 | my $wid = $wheel->ID; | ||
| 361 | $kernel->post( | ||
| 362 | 'MAIN-Logger', | ||
| 363 | 'alert', | ||
| 364 | "------------------------------------------------------------\n". | ||
| 365 | "WATCHDOG ALERT: The Watchdog has been forced to kill process\n". | ||
| 366 | "with PID: $pid on wheel id: $wid, because it never answered\n". | ||
| 367 | "to STAT requests. EP Name: $pname\n". | ||
| 368 | "------------------------------------------------------------\n" | ||
| 369 | ); | ||
| 370 | |||
| 371 | # set EP to FAIL status | ||
| 372 | $$stat = EP_STAT_FA; | ||
| 373 | $$retries = 0; | ||
| 374 | |||
| 375 | # signal the child to die | ||
| 376 | $wheel->kill($SIG{KILL}); | ||
| 377 | |||
| 378 | } | ||
| 379 | else { | ||
| 380 | # request for status once again | ||
| 381 | $$stat = EP_STAT_WA; | ||
| 382 | $kernel->post( 'IKC', 'call', 'poe://'.$wheel->PID.'_IKCC/CA_SESSION/aesstat', undef, 'poe:ikc_event' ); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | # this watchdog blurb is debug level | ||
| 387 | $kernel->post('MAIN-Logger', 'debug', "--- LEAVING Endpoint Watchdog ---\n"); | ||
| 388 | |||
| 389 | # reset watchdog | ||
| 390 | $kernel->delay_set('endpoint_wdog', $self->{config}->aes_cwdg); | ||
| 391 | |||
| 392 | } | ||
| 393 | |||
| 394 | |||
| 395 | # shutdown stage 1 | ||
| 396 | sub shutdown_start { | ||
| 397 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 398 | $kernel->post('MAIN-Logger', 'alert', "Starting shutdown sequence...\n"); | ||
| 399 | $self->{shutdown} = 1; | ||
| 400 | |||
| 401 | my ($pid, $wid, $wheel, $pname, $stat) = undef; | ||
| 402 | # ask endpoints to exit kindly | ||
| 403 | foreach my $ep (@{$self->{endpoints}}) { | ||
| 404 | $pname = $ep->{pname}; | ||
| 405 | $stat = $ep->{stat}; | ||
| 406 | unless($stat == EP_STAT_NA or $stat == EP_STOPPED){ | ||
| 407 | $wheel = $ep->{wheel}; | ||
| 408 | $pid = $wheel->PID; | ||
| 409 | $wid = $wheel->ID; | ||
| 410 | } | ||
| 411 | else{ | ||
| 412 | $wheel = undef; | ||
| 413 | } | ||
| 414 | |||
| 415 | if(defined $wheel){ | ||
| 416 | $kernel->post( | ||
| 417 | 'MAIN-Logger','alert', | ||
| 418 | "SENDING TERM TO PID: $pid on wheel id: $wid Name: $pname\n" | ||
| 419 | ); | ||
| 420 | $wheel->kill($SIG{TERM}); | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | $kernel->delay_set('shutdown_complete', 3); | ||
| 425 | } | ||
| 426 | |||
| 427 | # shutdown everything else | ||
| 428 | sub shutdown_complete { | ||
| 429 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 430 | |||
| 431 | $kernel->post('MAIN-Logger', 'alert', "Completing shutdown sequence...\n"); | ||
| 432 | |||
| 433 | $self->{mq}->shutdown(); | ||
| 434 | $self->{mq} = undef; | ||
| 435 | $kernel->alias_remove($self->{alias}); | ||
| 436 | $kernel->alias_remove('AES_SESSION'); | ||
| 437 | $kernel->alias_remove('MAIN-Logger'); | ||
| 438 | |||
| 439 | } | ||
| 440 | |||
| 441 | |||
| 442 | |||
| 443 | # Syslog debug levels | ||
| 444 | #0 debug | ||
| 445 | #1 info | ||
| 446 | #2 notice | ||
| 447 | #3 warning | ||
| 448 | #4 error | ||
| 449 | #5 critical | ||
| 450 | #6 alert | ||
| 451 | #7 emergency | ||
| 452 | |||
| 453 | |||
| 454 | |||
| 455 | |||
| 456 | 1; | ||
| 457 | |||
| 458 | __END__ |