| 1 | #!/usr/local/bin/perl | ||
| 2 | 1 | ▶ | use 5.10.1; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | ## Core: | ||
| 7 | 1 | ▶ | use Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use DB_File; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Fcntl qw/:DEFAULT :flock/; | ||
| 10 | use Digest::SHA qw/sha1_hex/; | ||
| 11 | use File::Path qw/make_path/; | ||
| 12 | use Scalar::Util qw/blessed/; | ||
| 13 | use Storable qw/dclone/; | ||
| 14 | use POSIX (); | ||
| 15 | use Time::HiRes qw/tv_interval gettimeofday/; | ||
| 16 | require bytes; | ||
| 17 | |||
| 18 | ## POE + HTTP | ||
| 19 | use POE; | ||
| 20 | use POE::Wheel::Run; | ||
| 21 | use POE::Filter::Reference; | ||
| 22 | |||
| 23 | use URI; | ||
| 24 | use POEx::HTTP::Server; | ||
| 25 | |||
| 26 | ## Indexer: | ||
| 27 | use IRC::Indexer; | ||
| 28 | |||
| 29 | use IRC::Indexer::Conf; | ||
| 30 | use IRC::Indexer::Logger; | ||
| 31 | |||
| 32 | use IRC::Indexer::Trawl::Bot; | ||
| 33 | use IRC::Indexer::Trawl::Forking; | ||
| 34 | |||
| 35 | use IRC::Indexer::Report::Network; | ||
| 36 | |||
| 37 | use IRC::Indexer::Output::JSON; | ||
| 38 | |||
| 39 | use Compress::Zlib qw/memGzip memGunzip/; | ||
| 40 | |||
| 41 | my $opts = { | ||
| 42 | help => sub { | ||
| 43 | print( | ||
| 44 | "ircindexer-server-json\n\n", | ||
| 45 | |||
| 46 | " -v, --version\n", | ||
| 47 | " Display IRC::Indexer version\n\n", | ||
| 48 | |||
| 49 | " -c, --config=PATH\n", | ||
| 50 | " Configuration file for this server.\n\n", | ||
| 51 | |||
| 52 | " -d, --detach\n", | ||
| 53 | " Run as a daemon process.\n\n", | ||
| 54 | |||
| 55 | " -p, --pidfile\n", | ||
| 56 | " Write our PID to the specified path.\n", | ||
| 57 | " Overrides configuration file.\n\n", | ||
| 58 | |||
| 59 | " -l, --loglevel=LEVEL\n", | ||
| 60 | " Log verbosity, one of: debug, info, warn\n", | ||
| 61 | " Overrides configuration file.\n\n", | ||
| 62 | |||
| 63 | " -i, --interval=SECS\n", | ||
| 64 | " Interval between trawling servers on a specific network.\n", | ||
| 65 | " Overrides configuration file.\n", | ||
| 66 | " Defaults to 600 seconds (10 minutes)\n", | ||
| 67 | ); | ||
| 68 | exit 0 | ||
| 69 | }, | ||
| 70 | |||
| 71 | version => sub { | ||
| 72 | print( | ||
| 73 | "ircindexer-server-json (IRC::Indexer $IRC::Indexer::VERSION)\n", | ||
| 74 | " POEx::HTTP::Server $POEx::HTTP::Server::VERSION\n", | ||
| 75 | ); | ||
| 76 | exit 0 | ||
| 77 | }, | ||
| 78 | |||
| 79 | detach => 0, | ||
| 80 | |||
| 81 | interval => 600, | ||
| 82 | }; | ||
| 83 | |||
| 84 | use Getopt::Long; | ||
| 85 | GetOptions( $opts, | ||
| 86 | qw/ | ||
| 87 | help | ||
| 88 | version | ||
| 89 | |||
| 90 | config=s | ||
| 91 | |||
| 92 | detach! | ||
| 93 | |||
| 94 | pidfile=s | ||
| 95 | |||
| 96 | loglevel=s | ||
| 97 | |||
| 98 | interval=i | ||
| 99 | |||
| 100 | showsource | ||
| 101 | /, | ||
| 102 | ); | ||
| 103 | |||
| 104 | my $obj = { | ||
| 105 | Log => undef, | ||
| 106 | Cfg => undef, | ||
| 107 | active_trawlers => {}, | ||
| 108 | workers_by_pid => {}, | ||
| 109 | workers_by_wid => {}, | ||
| 110 | }; | ||
| 111 | |||
| 112 | my $state = { | ||
| 113 | timers => { trawlers => {} }, | ||
| 114 | stats => { | ||
| 115 | ## Basic outline; guaranteed present in /stats output. | ||
| 116 | RunID => sha1_hex(Time::HiRes::time() . rand), | ||
| 117 | Version => $IRC::Indexer::VERSION, | ||
| 118 | StartedAt => time, | ||
| 119 | |||
| 120 | LoopTick => 0, | ||
| 121 | |||
| 122 | TotalNetworks => 0, | ||
| 123 | TotalServers => 0, | ||
| 124 | |||
| 125 | TrawlersExecuted => 0, | ||
| 126 | TrawlersCreated => 0, | ||
| 127 | |||
| 128 | LastTrawl => 0, | ||
| 129 | LastTrawledServer => undef, | ||
| 130 | LastTrawlerReaped => undef, | ||
| 131 | |||
| 132 | HTTP_Responses => 0, | ||
| 133 | }, | ||
| 134 | }; | ||
| 135 | |||
| 136 | my $cfg = { | ||
| 137 | ## Defaults. | ||
| 138 | ## These will be pulled from our httpd.conf in get_cfg. | ||
| 139 | NetworkDir => undef, | ||
| 140 | BindAddr => '0.0.0.0', | ||
| 141 | ServerPort => 8700, | ||
| 142 | ListChans => undef, | ||
| 143 | |||
| 144 | CacheDir => undef, | ||
| 145 | PidFile => undef, | ||
| 146 | |||
| 147 | TrawlInterval => 600, | ||
| 148 | |||
| 149 | Forking => 0, | ||
| 150 | MaxEncoders => 20, | ||
| 151 | MaxTrawlers => 20, | ||
| 152 | |||
| 153 | LogFile => undef, | ||
| 154 | LogLevel => undef, | ||
| 155 | LogHTTP => 1, | ||
| 156 | LogIRC => 1, | ||
| 157 | |||
| 158 | ## Constructed by get_nets: | ||
| 159 | Networks => {}, | ||
| 160 | }; | ||
| 161 | |||
| 162 | ## for managing trawler creation | ||
| 163 | ## keyed on network name | ||
| 164 | ## value is array of arrays containing: | ||
| 165 | ## [ 'network', 'server', $trawler_opts_hashref ] | ||
| 166 | my $ArrayConf = { }; | ||
| 167 | |||
| 168 | ## If we're missing CacheDir: | ||
| 169 | my $jsmemcache = { }; | ||
| 170 | |||
| 171 | sub get_nets { | ||
| 172 | ## Called out of either get_cfg() or a rehash. | ||
| 173 | my $conf_obj = $obj->{Cfg}; | ||
| 174 | |||
| 175 | die "Configuration directive missing: NetworkDir\n" | ||
| 176 | unless $cfg->{NetworkDir}; | ||
| 177 | |||
| 178 | my $nethash = $conf_obj->parse_nets($cfg->{NetworkDir}); | ||
| 179 | $cfg->{Networks} = $nethash; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub get_cfg { | ||
| 183 | ## Set up $cfg, networks, and logging | ||
| 184 | ## Called out of start_indexer() | ||
| 185 | |||
| 186 | die "No --config specified.\n" unless $opts->{config}; | ||
| 187 | |||
| 188 | die "Specified conf nonexistant: $opts->{config}" | ||
| 189 | unless -e $opts->{config}; | ||
| 190 | |||
| 191 | die "Specified conf not readable: $opts->{config}" | ||
| 192 | unless -r $opts->{config}; | ||
| 193 | |||
| 194 | ## Try to set up primary conf: | ||
| 195 | my $cfpath = $opts->{config}; | ||
| 196 | my $conf = IRC::Indexer::Conf->new; | ||
| 197 | $obj->{Cfg} = $conf; | ||
| 198 | my $cfhash; | ||
| 199 | |||
| 200 | { | ||
| 201 | local $@; | ||
| 202 | eval { $cfhash = $conf->parse_conf($cfpath) }; | ||
| 203 | die "Could not parse conf: $@" if $@; | ||
| 204 | } | ||
| 205 | |||
| 206 | ## Only copies our defined params, see $cfg | ||
| 207 | for my $thisopt (keys %$cfg) { | ||
| 208 | $cfg->{$thisopt} = $cfhash->{$thisopt}; | ||
| 209 | } | ||
| 210 | |||
| 211 | ## Set up logging: | ||
| 212 | my $loglevel = $opts->{loglevel} || $cfg->{LogLevel} || 'info' ; | ||
| 213 | |||
| 214 | if ($cfg->{LogFile}) { | ||
| 215 | my $handler = IRC::Indexer::Logger->new( | ||
| 216 | LogFile => $cfg->{LogFile}, | ||
| 217 | LogLevel => $loglevel, | ||
| 218 | ); | ||
| 219 | $obj->{Log} = $handler->logger; | ||
| 220 | } | ||
| 221 | |||
| 222 | if ($opts->{detach}) { | ||
| 223 | open 'STDERR', '>', $cfg->{LogFile} | ||
| 224 | if $cfg->{LogFile}; | ||
| 225 | } else { | ||
| 226 | ## not detached, log to STDOUT | ||
| 227 | unless (blessed $obj->{Log}) { | ||
| 228 | my $handler = IRC::Indexer::Logger->new( | ||
| 229 | DevNull => 1, | ||
| 230 | ); | ||
| 231 | $obj->{Log} = $handler->logger; | ||
| 232 | } | ||
| 233 | $obj->{Log}->add( | ||
| 234 | screen => { | ||
| 235 | log_to => "STDOUT", | ||
| 236 | maxlevel => $loglevel || 'info', | ||
| 237 | timeformat => "%Y/%m/%d %H:%M:%S", | ||
| 238 | message_layout => "[%T] %L %m", | ||
| 239 | }, | ||
| 240 | ); | ||
| 241 | } | ||
| 242 | |||
| 243 | $cfg->{ServerPort} //= 8700; | ||
| 244 | |||
| 245 | ## Read server specifications: | ||
| 246 | get_nets(); | ||
| 247 | } | ||
| 248 | |||
| 249 | |||
| 250 | sub log_to { | ||
| 251 | ## log_to('http', 'warn', ...) | ||
| 252 | ## log_to('irc', 'info', ...) | ||
| 253 | ## null-op if there's no logger present | ||
| 254 | my ($type, $level, @lines) = @_; | ||
| 255 | return unless @lines; | ||
| 256 | my $log = $obj->{Log}; | ||
| 257 | return unless blessed $log; | ||
| 258 | |||
| 259 | given (lc $type) { | ||
| 260 | |||
| 261 | when ("http") { | ||
| 262 | return unless $cfg->{LogHTTP}; | ||
| 263 | $log->$level("$type ", @lines); | ||
| 264 | } | ||
| 265 | |||
| 266 | when ("irc") { | ||
| 267 | return unless $cfg->{LogIRC}; | ||
| 268 | $log->$level("$type ", @lines); | ||
| 269 | } | ||
| 270 | |||
| 271 | default { | ||
| 272 | ## anything else goes right to the log: | ||
| 273 | $log->$level("$type ", @lines); | ||
| 274 | } | ||
| 275 | |||
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | sub poco_cfg_httpd { | ||
| 280 | ## Called out of _start | ||
| 281 | $state->{htevents} = [ map { 'h_'.$_ } qw/ | ||
| 282 | error_404 | ||
| 283 | src | ||
| 284 | stats | ||
| 285 | list | ||
| 286 | server | ||
| 287 | servlist | ||
| 288 | network | ||
| 289 | netlist | ||
| 290 | / ]; | ||
| 291 | push(@{$state->{htevents}}, 'post_request'); | ||
| 292 | |||
| 293 | |||
| 294 | POEx::HTTP::Server->spawn( | ||
| 295 | retry => 0, | ||
| 296 | inet => { | ||
| 297 | LocalAddr => $cfg->{BindAddr}, | ||
| 298 | LocalPort => $cfg->{ServerPort}, | ||
| 299 | }, | ||
| 300 | |||
| 301 | handlers => [ | ||
| 302 | '^/$' => 'poe:indexerhttp/h_stats', | ||
| 303 | '^/stats' => 'poe:indexerhttp/h_stats', | ||
| 304 | |||
| 305 | '^/list' => 'poe:indexerhttp/h_list', | ||
| 306 | |||
| 307 | '^/network' => 'poe:indexerhttp/h_network', | ||
| 308 | '^/netlist' => 'poe:indexerhttp/h_netlist', | ||
| 309 | |||
| 310 | '^/src' => 'poe:indexerhttp/h_src', | ||
| 311 | |||
| 312 | '' => 'poe:indexerhttp/h_error_404', | ||
| 313 | |||
| 314 | 'post_request' => 'poe:indexerhttp/post_request', | ||
| 315 | ], | ||
| 316 | ); | ||
| 317 | |||
| 318 | log_to('http', 'info', 'HTTP configured'); | ||
| 319 | |||
| 320 | return 1 | ||
| 321 | } | ||
| 322 | |||
| 323 | sub poco_init_session { | ||
| 324 | ## Create 'indexerhttp' session | ||
| 325 | ## Called out of start_indexer() | ||
| 326 | POE::Session->create( | ||
| 327 | package_states => [ | ||
| 328 | main => [ qw/ | ||
| 329 | _start | ||
| 330 | _stop | ||
| 331 | |||
| 332 | session_sig_int | ||
| 333 | session_sig_hup | ||
| 334 | |||
| 335 | e_init_nets | ||
| 336 | e_rehash | ||
| 337 | |||
| 338 | e_timer_chk | ||
| 339 | e_trawler_run | ||
| 340 | e_trawler_process | ||
| 341 | |||
| 342 | js_push_pending | ||
| 343 | js_worker_stdout | ||
| 344 | js_worker_stderr | ||
| 345 | js_worker_error | ||
| 346 | js_worker_closed | ||
| 347 | js_worker_sigchld | ||
| 348 | |||
| 349 | / ], | ||
| 350 | ], | ||
| 351 | ); | ||
| 352 | |||
| 353 | ## e_* events bridge trawler sessions. | ||
| 354 | ## js_* events handle forked encoders. | ||
| 355 | ## h_* events bridge HTTP; they are added via state() in _start. | ||
| 356 | |||
| 357 | log_to('session', 'debug', 'Session initialized'); | ||
| 358 | } | ||
| 359 | |||
| 360 | sub _start { | ||
| 361 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 362 | $kernel->alias_set('indexerhttp'); | ||
| 363 | |||
| 364 | $kernel->sig('INT', 'session_sig_int'); | ||
| 365 | $kernel->sig('TERM', 'session_sig_int'); | ||
| 366 | $kernel->sig('HUP', 'session_sig_hup'); | ||
| 367 | |||
| 368 | log_to('session', 'info', 'Session started'); | ||
| 369 | |||
| 370 | ## init httpd | ||
| 371 | poco_cfg_httpd(); | ||
| 372 | |||
| 373 | $kernel->post( $_[SESSION], 'e_init_nets' ); | ||
| 374 | |||
| 375 | ## add states from $state->{htevents} | ||
| 376 | $kernel->state($_, __PACKAGE__) for @{$state->{htevents}}; | ||
| 377 | } | ||
| 378 | |||
| 379 | sub _stop { | ||
| 380 | log_to('session', 'warn', "_stop received"); | ||
| 381 | |||
| 382 | ## Run some cleanup if needed. | ||
| 383 | |||
| 384 | for my $pidof (keys %{ $obj->{workers_by_pid} }) { | ||
| 385 | my $wheel = delete $obj->{workers_by_pid}->{$pidof}; | ||
| 386 | next unless ref $wheel; | ||
| 387 | my $wid = $wheel->ID; | ||
| 388 | delete $obj->{workers_by_wid}->{$wid}; | ||
| 389 | $wheel->kill(9); | ||
| 390 | } | ||
| 391 | |||
| 392 | if ($state->{openDB}) { | ||
| 393 | for my $network (keys %{ $state->{openDB} }) { | ||
| 394 | db_close($network); | ||
| 395 | } | ||
| 396 | } | ||
| 397 | |||
| 398 | if ($state->{wrote_pid_file}) { | ||
| 399 | unlink($state->{wrote_pid_file}) | ||
| 400 | or warn "could not clean up pidfile: $!"; | ||
| 401 | } | ||
| 402 | |||
| 403 | ## HTTP server should pick up shutdown and clean up. | ||
| 404 | $_[KERNEL]->signal( $_[KERNEL], 'shutdown' ); | ||
| 405 | } | ||
| 406 | |||
| 407 | sub session_sig_hup { | ||
| 408 | log_to('session', 'warn', 'SIGHUP, reloading'); | ||
| 409 | $_[KERNEL]->sig_handled(); | ||
| 410 | $_[KERNEL]->call( $_[SESSION], 'e_rehash'); | ||
| 411 | } | ||
| 412 | |||
| 413 | sub session_sig_int { | ||
| 414 | warn "Exiting due to signal"; | ||
| 415 | $_[KERNEL]->yield('_stop'); | ||
| 416 | } | ||
| 417 | |||
| 418 | sub e_init_nets { | ||
| 419 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 420 | |||
| 421 | ## Called by _start & rehash. | ||
| 422 | |||
| 423 | ## clear configured trawlers in $ArrayConf | ||
| 424 | $ArrayConf = { }; | ||
| 425 | |||
| 426 | my($total_nets, $total_servers); | ||
| 427 | NETWORK: for my $network (keys %{ $cfg->{Networks} }) { | ||
| 428 | ++$total_nets; | ||
| 429 | my $this_net = $cfg->{Networks}->{$network}; | ||
| 430 | SERVER: for my $server (keys %$this_net) { | ||
| 431 | ++$total_servers; | ||
| 432 | my $this_cf = $this_net->{$server}; | ||
| 433 | |||
| 434 | log_to('irc', 'info', "Init trawler: $network -> $server"); | ||
| 435 | |||
| 436 | ## Push configuration to array | ||
| 437 | ## we'll create_trawler() in e_trawler_run | ||
| 438 | my %trawlopts = %$this_cf; | ||
| 439 | push(@{$ArrayConf->{$network}}, | ||
| 440 | [ $network, $server, { Server => $server, %trawlopts } ] | ||
| 441 | ); | ||
| 442 | |||
| 443 | log_to('irc', 'debug', "Trawler configured for $server"); | ||
| 444 | } ## SERVER | ||
| 445 | |||
| 446 | } ## NETWORK | ||
| 447 | |||
| 448 | log_to('irc', 'info', | ||
| 449 | "Trawling $total_servers servers across $total_nets networks" | ||
| 450 | ); | ||
| 451 | |||
| 452 | $state->{stats}->{TotalNetworks} = $total_nets; | ||
| 453 | $state->{stats}->{TotalServers} = $total_servers; | ||
| 454 | |||
| 455 | ## Reset our HasRun. | ||
| 456 | ## This may be a rehash. | ||
| 457 | $state->{hasrun} = {}; | ||
| 458 | |||
| 459 | $kernel->yield('e_timer_chk'); | ||
| 460 | } | ||
| 461 | |||
| 462 | sub e_rehash { | ||
| 463 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 464 | |||
| 465 | ## Suspend our check loop, e_init_nets will reset it. | ||
| 466 | $kernel->alarm('e_timer_chk'); | ||
| 467 | |||
| 468 | ## Stop any running trawler runs. | ||
| 469 | ## Running encoders for dead nets are automatically discarded. | ||
| 470 | ## (You might bottleneck for a few if they're throttled hard, though.) | ||
| 471 | NETWORK: for my $network (keys %{ $obj->{active_trawlers} }) { | ||
| 472 | log_to('irc', 'debug', "Suspending trawler for $network"); | ||
| 473 | |||
| 474 | my $trawler = delete $obj->{active_trawlers}->{$network}; | ||
| 475 | |||
| 476 | ## If there's no Session ID, this trawler isn't running. | ||
| 477 | my $sessid = $trawler->ID // next NETWORK; | ||
| 478 | $kernel->post( $sessid, 'shutdown' ); | ||
| 479 | } ## NETWORK | ||
| 480 | |||
| 481 | |||
| 482 | ## Reload Networks cfg | ||
| 483 | my @prev_nets = keys %{ $cfg->{Networks} }; | ||
| 484 | get_nets(); | ||
| 485 | |||
| 486 | ## Check if any networks have disappeared. | ||
| 487 | ## If they have, we don't need their cache. | ||
| 488 | CFG: for my $old_net (@prev_nets) { | ||
| 489 | unless (defined $cfg->{Networks}->{$old_net}) { | ||
| 490 | cache_clear($old_net); | ||
| 491 | } | ||
| 492 | } | ||
| 493 | |||
| 494 | ## Re-initialize runs right now. | ||
| 495 | $kernel->call( $_[SESSION], 'e_init_nets'); | ||
| 496 | } | ||
| 497 | |||
| 498 | sub e_timer_chk { | ||
| 499 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 500 | my $state_run = $state->{hasrun}; | ||
| 501 | |||
| 502 | ++$state->{stats}->{LoopTick}; | ||
| 503 | |||
| 504 | my @trawlers; | ||
| 505 | |||
| 506 | ## PID write is here so we can fail out properly if HTTPd can't start: | ||
| 507 | unless ($state->{wrote_pid}) { | ||
| 508 | my $pidfile = $opts->{pidfile} || $cfg->{PidFile} || undef; | ||
| 509 | if ($pidfile) { | ||
| 510 | open my $fh, '>', $pidfile | ||
| 511 | or die "PID file specified but unable to write: $pidfile: $!"; | ||
| 512 | print $fh $$; | ||
| 513 | close $fh; | ||
| 514 | } | ||
| 515 | ++$state->{wrote_pid}; | ||
| 516 | $state->{wrote_pid_path} = $pidfile; | ||
| 517 | } | ||
| 518 | |||
| 519 | my $sp = 0; | ||
| 520 | NETWORK: for my $network (keys %$ArrayConf) { | ||
| 521 | my $last_trawled = $state_run->{$network}; | ||
| 522 | unless ($last_trawled) { | ||
| 523 | ## No trawlers have been run for this network, yet. | ||
| 524 | ## Spin one up, set HasRun time. | ||
| 525 | $state_run->{$network} = time(); | ||
| 526 | |||
| 527 | unless (@{ $ArrayConf->{$network} }) { | ||
| 528 | log_to('irc', 'warn', "No trawlers to start for $network"); | ||
| 529 | next NETWORK | ||
| 530 | } | ||
| 531 | |||
| 532 | ## Space out our initial runs. | ||
| 533 | log_to('irc', 'debug', "Scheduling initial trawler for $network"); | ||
| 534 | $kernel->alarm_add( 'e_trawler_run', time + ($sp+=1), | ||
| 535 | $ArrayConf->{$network}->[0] | ||
| 536 | ); | ||
| 537 | } | ||
| 538 | } ## NETWORK | ||
| 539 | |||
| 540 | TRAWL: for my $network (keys %{ $obj->{active_trawlers} }) { | ||
| 541 | my $trawler = delete $obj->{active_trawlers}->{$network}; | ||
| 542 | if ($trawler->done) { | ||
| 543 | ## A trawler has finished. | ||
| 544 | ## Rotate ArrayConf and schedule a new one. | ||
| 545 | my $prev = shift @{ $ArrayConf->{$network} }; | ||
| 546 | push(@{ $ArrayConf->{$network} }, $prev ); | ||
| 547 | |||
| 548 | $kernel->yield( 'e_trawler_process', $network, $trawler ); | ||
| 549 | |||
| 550 | ## Schedule the next one for this net to run in ten minutes. | ||
| 551 | my $next_trawler = $ArrayConf->{$network}->[0]; | ||
| 552 | if (defined $next_trawler) { | ||
| 553 | my $interval = $opts->{interval} || $cfg->{TrawlInterval} || 600; | ||
| 554 | my $next_run_at = time + $interval; | ||
| 555 | $kernel->alarm_add( | ||
| 556 | 'e_trawler_run', $next_run_at, $next_trawler | ||
| 557 | ); | ||
| 558 | log_to('irc', 'debug', | ||
| 559 | "Scheduling next trawler for $network" | ||
| 560 | ); | ||
| 561 | } else { | ||
| 562 | log_to('irc', 'warn', "ArrayConf disappeared for $network" ); | ||
| 563 | } | ||
| 564 | } else { | ||
| 565 | ## Still pending. | ||
| 566 | $obj->{active_trawlers}->{$network} = $trawler; | ||
| 567 | } | ||
| 568 | } ## TRAWL | ||
| 569 | |||
| 570 | $kernel->alarm('e_timer_chk' => time + 1); | ||
| 571 | } | ||
| 572 | |||
| 573 | sub e_trawler_run { | ||
| 574 | ## takes an arrayref: | ||
| 575 | ## [ network, server, conf-as-hashref ] | ||
| 576 | my ($kernel, $heap, $confitem) = @_[KERNEL, HEAP, ARG0]; | ||
| 577 | |||
| 578 | unless (ref $confitem eq 'ARRAY' && $ArrayConf->{ $confitem->[0] }) { | ||
| 579 | ## e_trawler_run was scheduled, but we lost the network in a rehash. | ||
| 580 | ## this is a hack; we should be tracking/removing alarm_add IDs. | ||
| 581 | log_to('irc', 'debug', | ||
| 582 | "skipped e_trawler_run for cancelled trawler" | ||
| 583 | ); | ||
| 584 | return | ||
| 585 | } | ||
| 586 | |||
| 587 | my ($network, $server, $trawlcf) = @$confitem; | ||
| 588 | |||
| 589 | ## Too many trawlers running. | ||
| 590 | my $current_count = keys %{$obj->{active_trawlers}}; | ||
| 591 | if ($cfg->{MaxTrawlers} && $current_count >= $cfg->{MaxTrawlers}) { | ||
| 592 | ++$state->{stats}->{ThrottledTrawler}; | ||
| 593 | $kernel->alarm_add( 'e_trawler_run', time + 1, $confitem ); | ||
| 594 | return | ||
| 595 | } | ||
| 596 | |||
| 597 | my $trawler = create_trawler(%$trawlcf); | ||
| 598 | $trawler->run(); | ||
| 599 | log_to('irc', 'info', "Trawler run: $network: $server"); | ||
| 600 | |||
| 601 | ## Add to active trawlers for this network | ||
| 602 | $obj->{active_trawlers}->{$network} = $trawler; | ||
| 603 | |||
| 604 | ++$state->{stats}->{TrawlersExecuted}; | ||
| 605 | $state->{stats}->{LastTrawl} = time; | ||
| 606 | $state->{stats}->{LastTrawledServer} = $server; | ||
| 607 | } | ||
| 608 | |||
| 609 | sub e_trawler_process { | ||
| 610 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 611 | my ($network, $trawler) = @_[ARG0, ARG1]; | ||
| 612 | |||
| 613 | ## passed trawler objects marked as done (from _chk) | ||
| 614 | |||
| 615 | my $orig_server = $trawler->trawler_for; | ||
| 616 | log_to('irc', 'debug', "Processing trawler: $network: $orig_server"); | ||
| 617 | $state->{stats}->{LastTrawlerReaped} = $network; | ||
| 618 | |||
| 619 | if (my $err = $trawler->failed) { | ||
| 620 | log_to('irc', 'warn', | ||
| 621 | "Trawler failed: $network ($orig_server): $err" | ||
| 622 | ); | ||
| 623 | |||
| 624 | $state->{stats}->{LastFailure}->{$network} = { | ||
| 625 | Target => $orig_server, | ||
| 626 | Error => $err, | ||
| 627 | TS => time, | ||
| 628 | }; | ||
| 629 | |||
| 630 | ++$state->{stats}->{Fail}->{$network}->{$orig_server}; | ||
| 631 | } else { | ||
| 632 | my $info = $trawler->info; | ||
| 633 | my $server = $info->servername; | ||
| 634 | ++$state->{stats}->{Success}->{$network}->{$server}; | ||
| 635 | |||
| 636 | log_to('irc', 'info', | ||
| 637 | "Completed trawler: $network ($server)" | ||
| 638 | ); | ||
| 639 | |||
| 640 | ## sort ListChans if configured: | ||
| 641 | $info->channels if $cfg->{ListChans}; | ||
| 642 | |||
| 643 | my $s_info = $info->netinfo; | ||
| 644 | |||
| 645 | my $netinfo_obj; | ||
| 646 | if ($obj->{NetInfo}->{$network}) { | ||
| 647 | my $stored = $obj->{NetInfo}->{$network}; | ||
| 648 | $netinfo_obj = IRC::Indexer::Report::Network->new( | ||
| 649 | FromHash => $stored, | ||
| 650 | NoChannels => 1, | ||
| 651 | ); | ||
| 652 | } else { | ||
| 653 | $netinfo_obj = IRC::Indexer::Report::Network->new( | ||
| 654 | NoChannels => 1, | ||
| 655 | ); | ||
| 656 | } | ||
| 657 | |||
| 658 | ## feed it this server's Info object: | ||
| 659 | $netinfo_obj->add_server($info); | ||
| 660 | undef $info; | ||
| 661 | |||
| 662 | ## process and grab new hash: | ||
| 663 | my $n_info = $netinfo_obj->netinfo; | ||
| 664 | undef $netinfo_obj; | ||
| 665 | |||
| 666 | ## cheating here a bit to save some memory ... | ||
| 667 | ## preserve hash for later Network reconstruction, w/out chans | ||
| 668 | ## then merge this server report's hashchans: | ||
| 669 | $obj->{NetInfo}->{$network} = dclone($n_info); | ||
| 670 | $n_info->{HashChans} = $s_info->{HashChans}; | ||
| 671 | |||
| 672 | $kernel->call( $_[SESSION], 'js_push_pending', | ||
| 673 | [ $n_info, $network ] | ||
| 674 | ); | ||
| 675 | undef $n_info; | ||
| 676 | |||
| 677 | $kernel->call( $_[SESSION], 'js_push_pending', | ||
| 678 | [ $s_info, $network, $server ], | ||
| 679 | ); | ||
| 680 | } | ||
| 681 | } | ||
| 682 | |||
| 683 | ## Helpers | ||
| 684 | |||
| 685 | sub create_trawler { | ||
| 686 | my %trawlopts = @_; | ||
| 687 | my $this_cf = \%trawlopts; | ||
| 688 | |||
| 689 | my $trawler_class; | ||
| 690 | if ($cfg->{Forking}) { | ||
| 691 | $trawler_class = 'Trawl::Forking'; | ||
| 692 | } else { | ||
| 693 | $trawler_class = 'Trawl::Bot'; | ||
| 694 | } | ||
| 695 | $trawler_class = 'IRC::Indexer::'.$trawler_class; | ||
| 696 | |||
| 697 | my $trawler = $trawler_class->new( | ||
| 698 | Server => $this_cf->{Server}, | ||
| 699 | Port => $this_cf->{Port}, | ||
| 700 | Nickname => $this_cf->{Nickname}, | ||
| 701 | Timeout => $this_cf->{Timeout}, | ||
| 702 | Interval => $this_cf->{Interval}, | ||
| 703 | BindAddr => $this_cf->{BindAddr}, | ||
| 704 | UseIPV6 => $this_cf->{UseIPV6}, | ||
| 705 | ); | ||
| 706 | |||
| 707 | ++$state->{stats}->{TrawlersCreated}; | ||
| 708 | |||
| 709 | return $trawler | ||
| 710 | } | ||
| 711 | |||
| 712 | sub db_open { | ||
| 713 | my ($network, %dbopts) = @_; | ||
| 714 | $dbopts{lc $_} = $dbopts{$_} for keys %dbopts; | ||
| 715 | |||
| 716 | my $cachedir; | ||
| 717 | die "db_open called but no CacheDir configured" | ||
| 718 | unless $cachedir = $cfg->{CacheDir}; | ||
| 719 | |||
| 720 | make_path($cachedir) unless -e $cachedir; | ||
| 721 | |||
| 722 | my $dbpath = join '/', $cachedir, $network; | ||
| 723 | my $lockpath = $dbpath . '.lock'; | ||
| 724 | |||
| 725 | open my $lock_fh, '>', $lockpath | ||
| 726 | or die "lockfile open failed: $!"; | ||
| 727 | |||
| 728 | my $timer = 0; | ||
| 729 | my $timeout = 3; | ||
| 730 | until ( flock $lock_fh, LOCK_EX | LOCK_NB ) { | ||
| 731 | if ($timer > $timeout) { | ||
| 732 | log_to('DB', 'warn', "Lock timed out."); | ||
| 733 | return | ||
| 734 | } | ||
| 735 | select undef, undef, undef, 0.1; | ||
| 736 | $timer += 0.1; | ||
| 737 | } | ||
| 738 | print $lock_fh $$; | ||
| 739 | |||
| 740 | $state->{locks}->{$network} = $lock_fh; | ||
| 741 | |||
| 742 | my %this_cache; | ||
| 743 | tie %this_cache, "DB_File", $dbpath, | ||
| 744 | O_CREAT|O_RDWR, 0666, $DB_HASH | ||
| 745 | or die "Failed DB tie: $dbpath: $!"; | ||
| 746 | |||
| 747 | my $runid = $state->{stats}->{RunID}; | ||
| 748 | if ($this_cache{'_RUNID'} && $this_cache{'_RUNID'} ne $runid) { | ||
| 749 | log_to('DB', 'warn', "Clearing stale DB: $network"); | ||
| 750 | %this_cache = (); | ||
| 751 | } | ||
| 752 | $this_cache{'_RUNID'} = $runid; | ||
| 753 | |||
| 754 | unless ($dbopts{gzip} || $dbopts{raw}) { | ||
| 755 | (tied %this_cache)->filter_fetch_value( | ||
| 756 | sub { $_ = memGunzip($_) } | ||
| 757 | ); | ||
| 758 | (tied %this_cache)->filter_store_value( | ||
| 759 | sub { $_ = memGzip($_) } | ||
| 760 | ); | ||
| 761 | } | ||
| 762 | |||
| 763 | $state->{openDB}->{$network} = \%this_cache; | ||
| 764 | ++$state->{stats}->{DB}->{Open}; | ||
| 765 | } | ||
| 766 | |||
| 767 | sub db_write { | ||
| 768 | my ($data, $network, $server) = @_; | ||
| 769 | |||
| 770 | ++$state->{stats}->{DB}->{Write}; | ||
| 771 | |||
| 772 | my $opendb = $state->{openDB}->{$network}; | ||
| 773 | |||
| 774 | unless ($opendb && tied %$opendb) { | ||
| 775 | die "Attempted db_write on unopened DB ($network)" | ||
| 776 | } | ||
| 777 | |||
| 778 | if ($server) { | ||
| 779 | $opendb->{$server} = $data; | ||
| 780 | } else { | ||
| 781 | $opendb->{'_NETMETA'} = $data; | ||
| 782 | } | ||
| 783 | return 1 | ||
| 784 | } | ||
| 785 | |||
| 786 | sub db_read { | ||
| 787 | my ($network, $server) = @_; | ||
| 788 | |||
| 789 | ++$state->{stats}->{DB}->{Read}; | ||
| 790 | |||
| 791 | my $opendb = $state->{openDB}->{$network}; | ||
| 792 | |||
| 793 | unless ($opendb && tied %$opendb) { | ||
| 794 | die "Attempted db_read on unopened DB ($network)" | ||
| 795 | } | ||
| 796 | |||
| 797 | my $data; | ||
| 798 | if ($server) { | ||
| 799 | $data = $opendb->{$server}; | ||
| 800 | } else { | ||
| 801 | $data = $opendb->{'_NETMETA'}; | ||
| 802 | } | ||
| 803 | return $data | ||
| 804 | } | ||
| 805 | |||
| 806 | sub db_close { | ||
| 807 | my ($network) = @_; | ||
| 808 | |||
| 809 | ++$state->{stats}->{DB}->{Close}; | ||
| 810 | |||
| 811 | my $cachedir; | ||
| 812 | die "db_close called but no CacheDir configured" | ||
| 813 | unless $cachedir = $cfg->{CacheDir}; | ||
| 814 | |||
| 815 | my $dbpath = join '/', $cachedir, $network; | ||
| 816 | my $lockpath = $dbpath . '.lock'; | ||
| 817 | |||
| 818 | my $lock_fh = delete $state->{locks}->{$network}; | ||
| 819 | my $opendb = delete $state->{openDB}->{$network}; | ||
| 820 | |||
| 821 | if ($lock_fh) { | ||
| 822 | close $lock_fh; | ||
| 823 | unlink $lockpath; | ||
| 824 | } | ||
| 825 | |||
| 826 | if ($opendb) { | ||
| 827 | untie %$opendb; | ||
| 828 | } | ||
| 829 | } | ||
| 830 | |||
| 831 | sub db_unlink { | ||
| 832 | my ($network) = @_; | ||
| 833 | |||
| 834 | ++$state->{stats}->{DB}->{Unlink}; | ||
| 835 | |||
| 836 | my $cachedir; | ||
| 837 | die "db_unlink called but no CacheDir configured" | ||
| 838 | unless $cachedir = $cfg->{CacheDir}; | ||
| 839 | |||
| 840 | my $dbpath = join '/', $cachedir, $network; | ||
| 841 | |||
| 842 | log_to('DB', 'debug', "db_unlink: $dbpath"); | ||
| 843 | |||
| 844 | ## Not sure if we should die() ... | ||
| 845 | unlink($dbpath) or log_to('DB', 'warn', "db_unlink: $network: $!"); | ||
| 846 | } | ||
| 847 | |||
| 848 | sub cachedir_write { | ||
| 849 | my ($zipped, $network, $server) = @_; | ||
| 850 | return unless $network; | ||
| 851 | |||
| 852 | my $cachedir; | ||
| 853 | die "cachedir_write called but no CacheDir configured" | ||
| 854 | unless $cachedir = $cfg->{CacheDir}; | ||
| 855 | |||
| 856 | my $timer0 = [gettimeofday]; | ||
| 857 | |||
| 858 | db_open($network, Raw => 1); | ||
| 859 | db_write($zipped, $network, $server); | ||
| 860 | db_close($network); | ||
| 861 | |||
| 862 | my $interval = tv_interval($timer0); | ||
| 863 | |||
| 864 | $state->{stats}->{DB}->{'WR_Total'} += (sprintf("%.6f", $interval)+0); | ||
| 865 | $state->{stats}->{DB}->{'WR_Longest'} = $interval | ||
| 866 | if $interval > ($state->{stats}->{DB}->{'WR_Longest'}//0); | ||
| 867 | |||
| 868 | my $runs = $state->{stats}->{DB}->{Write} // 1; | ||
| 869 | my $avg = $state->{stats}->{DB}->{'WR_Total'} / $runs; | ||
| 870 | |||
| 871 | $state->{stats}->{DB}->{'WR_Avg'} = sprintf("%.6f", $avg) + 0; | ||
| 872 | |||
| 873 | return 1 | ||
| 874 | } | ||
| 875 | |||
| 876 | sub cachedir_read { | ||
| 877 | my ($gzipped, $network, $server) = @_; | ||
| 878 | return unless $network; | ||
| 879 | $gzipped = 0 if $gzipped eq 'json'; | ||
| 880 | |||
| 881 | my $cachedir; | ||
| 882 | die "cachedir_read called but no CacheDir configured" | ||
| 883 | unless $cachedir = $cfg->{CacheDir}; | ||
| 884 | |||
| 885 | my $timer0 = [gettimeofday]; | ||
| 886 | |||
| 887 | db_open($network, Raw => $gzipped); | ||
| 888 | my $data = db_read($network, $server); | ||
| 889 | db_close($network); | ||
| 890 | |||
| 891 | my $interval = tv_interval($timer0); | ||
| 892 | |||
| 893 | $state->{stats}->{DB}->{'RD_Total'} += (sprintf("%.6f", $interval)+0); | ||
| 894 | $state->{stats}->{DB}->{'RD_Longest'} = $interval | ||
| 895 | if $interval > ($state->{stats}->{DB}->{'RD_Longest'}//0); | ||
| 896 | |||
| 897 | my $read = $state->{stats}->{DB}->{Read} // 1; | ||
| 898 | my $avg = $state->{stats}->{DB}->{'RD_Total'} / $read; | ||
| 899 | |||
| 900 | $state->{stats}->{DB}->{'RD_Avg'} = sprintf("%.6f", $avg) + 0; | ||
| 901 | |||
| 902 | return $data | ||
| 903 | } | ||
| 904 | |||
| 905 | sub cache_clear { | ||
| 906 | my ($network) = @_; | ||
| 907 | return unless $network; | ||
| 908 | |||
| 909 | log_to('DB', 'warn', "Clearing cache: $network"); | ||
| 910 | |||
| 911 | if ($cfg->{CacheDir}) { | ||
| 912 | db_unlink($network); | ||
| 913 | } else { | ||
| 914 | delete $jsmemcache->{Networks}->{$network} | ||
| 915 | } | ||
| 916 | } | ||
| 917 | |||
| 918 | sub cache_json { | ||
| 919 | my ($gzip, $network, $server) = @_; | ||
| 920 | |||
| 921 | if ($server) { | ||
| 922 | ## store a server's json | ||
| 923 | if ($cfg->{CacheDir}) { | ||
| 924 | cachedir_write($gzip, $network, $server); | ||
| 925 | } else { | ||
| 926 | $jsmemcache->{Servers}->{$network}->{$server} = memGunzip($gzip); | ||
| 927 | } | ||
| 928 | } else { | ||
| 929 | ## store a network's json | ||
| 930 | if ($cfg->{CacheDir}) { | ||
| 931 | cachedir_write($gzip, $network); | ||
| 932 | } else { | ||
| 933 | $jsmemcache->{Networks}->{$network} = memGunzip($gzip); | ||
| 934 | } | ||
| 935 | } | ||
| 936 | } | ||
| 937 | |||
| 938 | sub restore_cache { | ||
| 939 | my ($gzipped, $network, $server) = @_; | ||
| 940 | $gzipped = 0 if $gzipped eq 'json'; | ||
| 941 | |||
| 942 | if ($server) { | ||
| 943 | |||
| 944 | if ($cfg->{CacheDir}) { | ||
| 945 | return cachedir_read($gzipped, $network, $server) | ||
| 946 | } else { | ||
| 947 | return unless exists $jsmemcache->{Servers}->{$network}; | ||
| 948 | my $cachedjs = $jsmemcache->{Servers}->{$network}->{$server}; | ||
| 949 | return memGzip($cachedjs) if $cachedjs and $gzipped; | ||
| 950 | return $cachedjs | ||
| 951 | } | ||
| 952 | } else { | ||
| 953 | if ($cfg->{CacheDir}) { | ||
| 954 | return cachedir_read($gzipped, $network) | ||
| 955 | } else { | ||
| 956 | my $cachedjs = $jsmemcache->{Networks}->{$network}; | ||
| 957 | return memGzip($cachedjs) if $cachedjs and $gzipped; | ||
| 958 | return $cachedjs | ||
| 959 | } | ||
| 960 | } | ||
| 961 | } | ||
| 962 | |||
| 963 | ## HTTP states | ||
| 964 | |||
| 965 | sub h_error_404 { | ||
| 966 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 967 | my ($req, $resp, $reason) = @_[ARG0, ARG1, ARG2]; | ||
| 968 | |||
| 969 | my $uri = URI->new( $req->uri ); | ||
| 970 | my $path = $uri->path; | ||
| 971 | my $conn = $req->connection; | ||
| 972 | my $addr = $conn->remote_host; | ||
| 973 | |||
| 974 | log_to('http', 'info', "$addr 404 $path"); | ||
| 975 | |||
| 976 | $reason = "Undefined action: ".$req->uri unless $reason; | ||
| 977 | $resp->error(404, $reason); | ||
| 978 | } | ||
| 979 | |||
| 980 | sub h_src { | ||
| 981 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 982 | my ($req, $resp) = @_[ARG0, ARG1]; | ||
| 983 | |||
| 984 | ## Silly little undocumented proof-of-concept. | ||
| 985 | ## Won't do anything if --showsource wasn't specified. | ||
| 986 | unless ($opts->{showsource}) { | ||
| 987 | $kernel->yield( h_error_404 => $req, $resp ); | ||
| 988 | return | ||
| 989 | } | ||
| 990 | |||
| 991 | my $src; | ||
| 992 | seek(DATA, 0, 0); | ||
| 993 | { | ||
| 994 | local $/; $src = <DATA>; | ||
| 995 | } | ||
| 996 | |||
| 997 | $resp->content_type('text/plain'); | ||
| 998 | $resp->content($src); | ||
| 999 | $resp->respond; | ||
| 1000 | |||
| 1001 | my $conn = $req->connection; | ||
| 1002 | my $addr = $conn->remote_host; | ||
| 1003 | log_to('http', 'info', "$addr 200 h_src"); | ||
| 1004 | |||
| 1005 | $resp->done; | ||
| 1006 | } | ||
| 1007 | |||
| 1008 | sub h_stats { | ||
| 1009 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 1010 | my ($req, $resp) = @_[ARG0, ARG1]; | ||
| 1011 | |||
| 1012 | my $uri = URI->new($req->uri); | ||
| 1013 | my @chunks = $uri->path_segments; | ||
| 1014 | splice @chunks, 0, 2; | ||
| 1015 | |||
| 1016 | my ($method) = @chunks; | ||
| 1017 | $method = 'json' unless $method; | ||
| 1018 | |||
| 1019 | if ($method eq 'text' || $method eq 'plain') { | ||
| 1020 | my $n_count = $state->{stats}->{TotalNetworks}; | ||
| 1021 | my $s_count = $state->{stats}->{TotalServers}; | ||
| 1022 | $resp->content_type('text/plain'); | ||
| 1023 | $resp->content( | ||
| 1024 | "Trawling $s_count servers across $n_count networks\n\n" | ||
| 1025 | . "Version: $IRC::Indexer::VERSION\n\n" | ||
| 1026 | ); | ||
| 1027 | } else { | ||
| 1028 | my @pending = keys %{ $obj->{active_trawlers} }; | ||
| 1029 | my $ref = { ActiveTrawlers => \@pending, %{$state->{stats}} }; | ||
| 1030 | my $json = JSON::XS->new->utf8(1)->pretty->encode($ref); | ||
| 1031 | $resp->content_type('application/json'); | ||
| 1032 | $resp->content($json); | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | $resp->respond; | ||
| 1036 | |||
| 1037 | my $conn = $req->connection; | ||
| 1038 | my $addr = $conn->remote_host; | ||
| 1039 | log_to('http', 'info', "$addr 200 h_stats"); | ||
| 1040 | |||
| 1041 | $resp->done; | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | sub h_server { | ||
| 1045 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 1046 | my ($req, $resp) = @_[ARG0, ARG1]; | ||
| 1047 | my ($network, $server) = @_[ARG2, ARG3]; | ||
| 1048 | ## handed off by h_network | ||
| 1049 | |||
| 1050 | my $zipped; | ||
| 1051 | unless ( $zipped = restore_cache('zip', $network, $server) ) { | ||
| 1052 | ## no JSON for this server, 404 | ||
| 1053 | my $reason; | ||
| 1054 | |||
| 1055 | if (defined $cfg->{Networks}->{$network} | ||
| 1056 | && defined $cfg->{Networks}->{$network}->{$server} | ||
| 1057 | ) { | ||
| 1058 | ## configured but not trawled (yet) | ||
| 1059 | $reason = "PENDING $server ($network) pending trawler run"; | ||
| 1060 | } else { | ||
| 1061 | $reason = "NO_SUCH No such server"; | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | $kernel->yield( h_error_404 => $req, $resp, $reason ); | ||
| 1065 | return | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | my $uri = URI->new($req->uri); | ||
| 1069 | if ($uri->query && $uri->query =~ /^gzip/i) { | ||
| 1070 | $resp->content_type('application/x-gzip'); | ||
| 1071 | $resp->content($zipped); | ||
| 1072 | } else { | ||
| 1073 | my $json = memGunzip($zipped); | ||
| 1074 | undef $zipped; | ||
| 1075 | $resp->content_type('application/json'); | ||
| 1076 | $resp->content($json); | ||
| 1077 | } | ||
| 1078 | |||
| 1079 | $resp->respond; | ||
| 1080 | |||
| 1081 | my $conn = $req->connection; | ||
| 1082 | my $addr = $conn->remote_host; | ||
| 1083 | log_to('http', 'info', "$addr 200 h_server $network $server"); | ||
| 1084 | |||
| 1085 | $resp->done; | ||
| 1086 | } | ||
| 1087 | |||
| 1088 | sub h_servlist { | ||
| 1089 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 1090 | my ($req, $resp, $network) = @_[ARG0, ARG1, ARG2]; | ||
| 1091 | ## handed off by h_network | ||
| 1092 | |||
| 1093 | my $servlist = [keys %{$state->{stats}->{Success}->{$network}}]; | ||
| 1094 | |||
| 1095 | my $jsify = IRC::Indexer::Output::JSON->new( | ||
| 1096 | Input => $servlist, | ||
| 1097 | ); | ||
| 1098 | my $json = $jsify->dump; | ||
| 1099 | |||
| 1100 | $resp->content_type('application/json'); | ||
| 1101 | $resp->content($json); | ||
| 1102 | |||
| 1103 | $resp->respond; | ||
| 1104 | |||
| 1105 | my $conn = $req->connection; | ||
| 1106 | my $addr = $conn->remote_host; | ||
| 1107 | log_to('http', 'info', "$addr 200 h_servlist $network"); | ||
| 1108 | |||
| 1109 | $resp->done; | ||
| 1110 | } | ||
| 1111 | |||
| 1112 | sub h_netlist { | ||
| 1113 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 1114 | my ($req, $resp) = @_[ARG0, ARG1]; | ||
| 1115 | |||
| 1116 | ## Return JSONified array of pullable networks: | ||
| 1117 | |||
| 1118 | my $netlist = { | ||
| 1119 | Configured => [ keys %$ArrayConf ], | ||
| 1120 | Trawled => [ keys %{$state->{stats}->{Success}} ], | ||
| 1121 | }; | ||
| 1122 | |||
| 1123 | my $jsify = IRC::Indexer::Output::JSON->new( | ||
| 1124 | Input => $netlist, | ||
| 1125 | ); | ||
| 1126 | my $json = $jsify->dump; | ||
| 1127 | |||
| 1128 | $resp->content_type('application/json'); | ||
| 1129 | $resp->content($json); | ||
| 1130 | |||
| 1131 | $resp->respond; | ||
| 1132 | |||
| 1133 | my $conn = $req->connection; | ||
| 1134 | my $addr = $conn->remote_host; | ||
| 1135 | log_to('http', 'info', "$addr 200 h_netlist"); | ||
| 1136 | |||
| 1137 | $resp->done; | ||
| 1138 | } | ||
| 1139 | |||
| 1140 | sub h_list { | ||
| 1141 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 1142 | my ($req, $resp) = @_[ARG0, ARG1]; | ||
| 1143 | |||
| 1144 | ## Return all configured networks/servers | ||
| 1145 | |||
| 1146 | my $jsify = IRC::Indexer::Output::JSON->new( | ||
| 1147 | Input => $cfg->{Networks}, | ||
| 1148 | ); | ||
| 1149 | my $json = $jsify->dump; | ||
| 1150 | |||
| 1151 | $resp->content_type('application/json'); | ||
| 1152 | $resp->content($json); | ||
| 1153 | |||
| 1154 | $resp->respond; | ||
| 1155 | |||
| 1156 | my $conn = $req->connection; | ||
| 1157 | my $addr = $conn->remote_host; | ||
| 1158 | log_to('http', 'info', "$addr 200 h_list"); | ||
| 1159 | |||
| 1160 | $resp->done; | ||
| 1161 | } | ||
| 1162 | |||
| 1163 | sub h_network { | ||
| 1164 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 1165 | my ($req, $resp) = @_[ARG0, ARG1]; | ||
| 1166 | ## handler for /network/... | ||
| 1167 | |||
| 1168 | my $uri = URI->new($req->uri); | ||
| 1169 | my @chunks = $uri->path_segments; | ||
| 1170 | splice @chunks, 0, 2; | ||
| 1171 | |||
| 1172 | my ($network) = @chunks; | ||
| 1173 | unless ($network) { | ||
| 1174 | ## supply JSONified network list: | ||
| 1175 | $kernel->yield( h_netlist => $req, $resp ); | ||
| 1176 | return | ||
| 1177 | } | ||
| 1178 | |||
| 1179 | my $zipped; | ||
| 1180 | ## We don't know this network: | ||
| 1181 | unless ( $zipped = restore_cache('zip', $network) ) { | ||
| 1182 | my $reason; | ||
| 1183 | if (defined $cfg->{Networks}->{$network}) { | ||
| 1184 | ## configured but not trawled | ||
| 1185 | $reason = "PENDING $network pending trawler run"; | ||
| 1186 | } else { | ||
| 1187 | $reason = "NO_SUCH No such network"; | ||
| 1188 | } | ||
| 1189 | $kernel->yield( h_error_404 => $req, $resp, $reason ); | ||
| 1190 | return | ||
| 1191 | } | ||
| 1192 | |||
| 1193 | ## if this is a /network/<net>/server, hand it off: | ||
| 1194 | if ($chunks[1] && $chunks[1] eq "server") { | ||
| 1195 | my $server = $chunks[2]; | ||
| 1196 | unless ($server) { | ||
| 1197 | ## supply available server list: | ||
| 1198 | $kernel->yield( h_servlist => $req, $resp, $network ); | ||
| 1199 | return | ||
| 1200 | } | ||
| 1201 | $kernel->yield( h_server => $req, $resp, $network, $server ); | ||
| 1202 | return | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | ## otherwise, try to serve a net hash: | ||
| 1206 | |||
| 1207 | if ($uri->query && $uri->query =~ /^gzip/i) { | ||
| 1208 | $resp->content_type('application/x-gzip'); | ||
| 1209 | $resp->content($zipped); | ||
| 1210 | } else { | ||
| 1211 | my $json = memGunzip($zipped); | ||
| 1212 | undef $zipped; | ||
| 1213 | $resp->content_type('application/json'); | ||
| 1214 | $resp->content($json); | ||
| 1215 | } | ||
| 1216 | |||
| 1217 | $resp->respond; | ||
| 1218 | |||
| 1219 | my $conn = $req->connection; | ||
| 1220 | my $addr = $conn->remote_host; | ||
| 1221 | log_to('http', 'info', "$addr 200 h_network $network"); | ||
| 1222 | $resp->done; | ||
| 1223 | } | ||
| 1224 | |||
| 1225 | sub post_request { | ||
| 1226 | ++$state->{stats}->{HTTP_Responses}; | ||
| 1227 | } | ||
| 1228 | |||
| 1229 | ## Forkable worker states | ||
| 1230 | sub js_push_pending { | ||
| 1231 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 1232 | my $item = $_[ARG0]; | ||
| 1233 | |||
| 1234 | ## yield back if too many workers running | ||
| 1235 | my $current_workers = scalar keys %{ $obj->{workers_by_pid}//{} }; | ||
| 1236 | if ($cfg->{MaxEncoders} && $cfg->{MaxEncoders} <= $current_workers) { | ||
| 1237 | ++$state->{stats}->{ThrottledEncoder}; | ||
| 1238 | $kernel->alarm_add('js_push_pending', Time::HiRes::time + 0.5, $item); | ||
| 1239 | return | ||
| 1240 | } | ||
| 1241 | |||
| 1242 | ## otherwise fork worker and put() | ||
| 1243 | my ($network) = @$item; | ||
| 1244 | |||
| 1245 | log_to('session', 'debug', "Encoder spawn: $network"); | ||
| 1246 | |||
| 1247 | ## borrowed from PoCo Resolver: | ||
| 1248 | my $perlpath = $Config{perlpath}; | ||
| 1249 | if ($^O ne 'VMS') { | ||
| 1250 | $perlpath .= $Config{_exe} | ||
| 1251 | unless $perlpath =~ m/$Config{_exe}$/i; | ||
| 1252 | } | ||
| 1253 | |||
| 1254 | my $forkable; | ||
| 1255 | if ($^O eq 'MSWin32') { | ||
| 1256 | ## sucks to be you! | ||
| 1257 | require IRC::Indexer::Process::JSONify; | ||
| 1258 | $forkable = \&IRC::Indexer::Process::JSONify::worker; | ||
| 1259 | } else { | ||
| 1260 | $forkable = [ | ||
| 1261 | $perlpath, (map { "-I$_" } @INC), | ||
| 1262 | '-MIRC::Indexer::Process::JSONify', '-e', | ||
| 1263 | 'IRC::Indexer::Process::JSONify->worker()' | ||
| 1264 | ]; | ||
| 1265 | } | ||
| 1266 | |||
| 1267 | my $wheel = POE::Wheel::Run->new( | ||
| 1268 | Program => $forkable, | ||
| 1269 | ErrorEvent => 'js_worker_error', | ||
| 1270 | StdoutEvent => 'js_worker_stdout', | ||
| 1271 | StderrEvent => 'js_worker_stderr', | ||
| 1272 | CloseEvent => 'js_worker_closed', | ||
| 1273 | StdioFilter => POE::Filter::Reference->new(), | ||
| 1274 | ); | ||
| 1275 | |||
| 1276 | ++$state->{stats}->{EncoderWheels}->{Created}; | ||
| 1277 | |||
| 1278 | my $wheelid = $wheel->ID; | ||
| 1279 | my $pidof = $wheel->PID; | ||
| 1280 | |||
| 1281 | $kernel->sig_child($pidof, 'js_worker_sigchld'); | ||
| 1282 | |||
| 1283 | $obj->{workers_by_pid}->{$pidof} = $wheel; | ||
| 1284 | $obj->{workers_by_wid}->{$wheelid} = $wheel; | ||
| 1285 | |||
| 1286 | ## Feed this worker. | ||
| 1287 | $wheel->put($item); | ||
| 1288 | } | ||
| 1289 | |||
| 1290 | sub js_worker_stdout { | ||
| 1291 | my ($kernel, $item) = @_[KERNEL, ARG0]; | ||
| 1292 | |||
| 1293 | log_to('session', 'debug', "Handling input from encoder"); | ||
| 1294 | |||
| 1295 | my ($zip, $network, $server) = splice @$item, 0, 4; | ||
| 1296 | unless ($network) { | ||
| 1297 | log_to('session', 'warn', "Invalid input from encoder!"); | ||
| 1298 | ++$state->{stats}->{EncoderWheels}->{BadResult}; | ||
| 1299 | return | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | ++$state->{stats}->{EncoderWheels}->{Success}; | ||
| 1303 | |||
| 1304 | ## got JSON back, cache_json it unless it's stale | ||
| 1305 | if ($cfg->{Networks}->{$network}) { | ||
| 1306 | cache_json($zip, $network, $server); | ||
| 1307 | my $logstr = "Cached JSON for $network"; | ||
| 1308 | $logstr .= " ($server)" if $server; | ||
| 1309 | log_to('irc', 'debug', $logstr); | ||
| 1310 | } else { | ||
| 1311 | log_to('irc', 'info', "Discarding JSON for stale network: $network"); | ||
| 1312 | } | ||
| 1313 | } | ||
| 1314 | |||
| 1315 | sub js_worker_stderr { | ||
| 1316 | my ($kernel, $err, $id) = @_[KERNEL, ARG0, ARG1]; | ||
| 1317 | log_to('session', 'warn', "Encoder reported err: $err"); | ||
| 1318 | |||
| 1319 | ++$state->{stats}->{EncoderWheels}->{Died}; | ||
| 1320 | ## these will die() and go away. | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | sub js_worker_closed { | ||
| 1324 | my ($kernel, $wheelid) = @_[KERNEL, ARG0]; | ||
| 1325 | my $wheel = delete $obj->{workers_by_wid}->{$wheelid}; | ||
| 1326 | |||
| 1327 | if (defined $wheel) { | ||
| 1328 | ++$state->{stats}->{EncoderWheels}->{Closed}; | ||
| 1329 | my $pidof = $wheel->PID; | ||
| 1330 | delete $obj->{workers_by_pid}->{$pidof}; | ||
| 1331 | $wheel->kill(9); ## just in case ... | ||
| 1332 | } | ||
| 1333 | } | ||
| 1334 | |||
| 1335 | sub js_worker_error { | ||
| 1336 | ## null-op; these should sigchld and go away | ||
| 1337 | ## (and you get no data either way) | ||
| 1338 | } | ||
| 1339 | |||
| 1340 | sub js_worker_sigchld { | ||
| 1341 | ## child's gone | ||
| 1342 | my ($kernel, $pidof) = @_[KERNEL, ARG1]; | ||
| 1343 | |||
| 1344 | log_to('session', 'debug', "Worker sig_chld: $pidof"); | ||
| 1345 | |||
| 1346 | ++$state->{stats}->{EncoderWheels}->{SIGCHLD}; | ||
| 1347 | |||
| 1348 | my $wheel; | ||
| 1349 | return unless $wheel = delete $obj->{workers_by_pid}->{$pidof}; | ||
| 1350 | |||
| 1351 | my $wheelid = $wheel->ID; | ||
| 1352 | delete $obj->{workers_by_wid}->{$wheelid}; | ||
| 1353 | } | ||
| 1354 | |||
| 1355 | sub start_indexer { | ||
| 1356 | if ($opts->{detach}) { | ||
| 1357 | print "Starting detached indexer.\n"; | ||
| 1358 | |||
| 1359 | my $fork = fork; | ||
| 1360 | exit 1 if not defined $fork; | ||
| 1361 | exit 0 if $fork; | ||
| 1362 | POSIX::setsid(); | ||
| 1363 | $fork = fork; | ||
| 1364 | exit 1 if not defined $fork; | ||
| 1365 | exit 0 if $fork; | ||
| 1366 | open(STDIN, '<', '/dev/null'); | ||
| 1367 | open(STDOUT, '>', '/dev/null'); | ||
| 1368 | open(STDERR, '>', '/dev/null'); | ||
| 1369 | umask(022); | ||
| 1370 | } | ||
| 1371 | |||
| 1372 | get_cfg(); | ||
| 1373 | poco_init_session(); | ||
| 1374 | POE::Kernel->run; | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | start_indexer(); | ||
| 1378 | |||
| 1379 | __END__ | ||