… | |
… | |
2300 | cfg_load; |
2300 | cfg_load; |
2301 | db_init; |
2301 | db_init; |
2302 | load_extensions; |
2302 | load_extensions; |
2303 | |
2303 | |
2304 | $TICK_WATCHER->start; |
2304 | $TICK_WATCHER->start; |
|
|
2305 | $WRITE_RUNTIME_WATCHER->start; |
2305 | Event::loop; |
2306 | Event::loop; |
2306 | } |
2307 | } |
2307 | |
2308 | |
2308 | ############################################################################# |
2309 | ############################################################################# |
2309 | # initialisation and cleanup |
2310 | # initialisation and cleanup |
… | |
… | |
2354 | }; |
2355 | }; |
2355 | |
2356 | |
2356 | warn "leave emergency perl save\n"; |
2357 | warn "leave emergency perl save\n"; |
2357 | } |
2358 | } |
2358 | |
2359 | |
|
|
2360 | sub post_cleanup { |
|
|
2361 | my ($make_core) = @_; |
|
|
2362 | |
|
|
2363 | warn Carp::longmess "post_cleanup backtrace" |
|
|
2364 | if $make_core; |
|
|
2365 | } |
|
|
2366 | |
2359 | sub reload() { |
2367 | sub reload() { |
2360 | # can/must only be called in main |
2368 | # can/must only be called in main |
2361 | if ($Coro::current != $Coro::main) { |
2369 | if ($Coro::current != $Coro::main) { |
2362 | warn "can only reload from main coroutine"; |
2370 | warn "can only reload from main coroutine"; |
2363 | return; |
2371 | return; |
2364 | } |
2372 | } |
2365 | |
2373 | |
2366 | warn "reloading..."; |
2374 | warn "reloading..."; |
2367 | |
2375 | |
2368 | warn "cancelling server ticker"; |
2376 | warn "entering sync_job"; |
2369 | $TICK_WATCHER->cancel; |
|
|
2370 | |
2377 | |
|
|
2378 | cf::sync_job { |
|
|
2379 | cf::write_runtime; |
2371 | cf::emergency_save; |
2380 | cf::emergency_save; |
|
|
2381 | cf::write_runtime; |
2372 | |
2382 | |
2373 | eval { |
2383 | warn "syncing database to disk"; |
|
|
2384 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2385 | |
2374 | # if anything goes wrong in here, we should simply crash as we already saved |
2386 | # if anything goes wrong in here, we should simply crash as we already saved |
2375 | |
2387 | |
2376 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2388 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2377 | for (Event::all_watchers) { |
2389 | for (Event::all_watchers) { |
2378 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2390 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2379 | } |
2391 | } |
2380 | |
|
|
2381 | warn "syncing database to disk"; |
|
|
2382 | BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
2383 | |
2392 | |
2384 | warn "flushing outstanding aio requests"; |
2393 | warn "flushing outstanding aio requests"; |
2385 | for (;;) { |
2394 | for (;;) { |
2386 | BDB::flush; |
2395 | BDB::flush; |
2387 | IO::AIO::flush; |
2396 | IO::AIO::flush; |
… | |
… | |
2461 | reattach $_ for values %MAP; |
2470 | reattach $_ for values %MAP; |
2462 | |
2471 | |
2463 | warn "loading reloadable resources"; |
2472 | warn "loading reloadable resources"; |
2464 | load_resources; |
2473 | load_resources; |
2465 | |
2474 | |
2466 | warn "restarting server ticker"; |
2475 | warn "leaving sync_job"; |
2467 | |
2476 | |
2468 | $TICK_WATCHER->start; |
2477 | 1 |
2469 | }; |
2478 | } or do { |
2470 | |
|
|
2471 | if ($@) { |
|
|
2472 | warn $@; |
2479 | warn $@; |
2473 | warn "error while reloading, exiting."; |
2480 | warn "error while reloading, exiting."; |
2474 | exit 1; |
2481 | exit 1; |
2475 | } |
2482 | }; |
2476 | |
2483 | |
2477 | warn "reloaded"; |
2484 | warn "reloaded"; |
2478 | }; |
2485 | }; |
2479 | |
2486 | |
2480 | our $RELOAD_WATCHER; # used only during reload |
2487 | our $RELOAD_WATCHER; # used only during reload |
… | |
… | |
2524 | $NEXT_TICK += $TICK; |
2531 | $NEXT_TICK += $TICK; |
2525 | |
2532 | |
2526 | $WAIT_FOR_TICK->broadcast; |
2533 | $WAIT_FOR_TICK->broadcast; |
2527 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2534 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2528 | |
2535 | |
2529 | Event::sweep; |
|
|
2530 | Coro::cede_notself; |
|
|
2531 | |
|
|
2532 | # my $AFTER = Event::time; |
2536 | # my $AFTER = Event::time; |
2533 | # warn $AFTER - $NOW;#d# |
2537 | # warn $AFTER - $NOW;#d# |
2534 | |
2538 | |
2535 | # if we are delayed by four ticks or more, skip them all |
2539 | # if we are delayed by four ticks or more, skip them all |
2536 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2540 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
… | |
… | |
2606 | } |
2610 | } |
2607 | |
2611 | |
2608 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2612 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2609 | reentrant => 0, |
2613 | reentrant => 0, |
2610 | data => WF_AUTOCANCEL, |
2614 | data => WF_AUTOCANCEL, |
|
|
2615 | parked => 1, |
2611 | after => 1, |
2616 | after => 1, |
2612 | interval => 10, |
2617 | interval => 10, |
2613 | prio => 6, # keep it lowest so it acts like a watchdog |
2618 | prio => 6, # keep it lowest so it acts like a watchdog |
2614 | cb => Coro::unblock_sub { |
2619 | cb => sub { |
|
|
2620 | $TICK_WATCHER->is_active or cf::cleanup "mainloop frozen but runtime active", 1; |
|
|
2621 | |
|
|
2622 | Coro::async_pool { |
2615 | write_runtime |
2623 | write_runtime |
2616 | or warn "ERROR: unable to write runtime file: $!"; |
2624 | or warn "ERROR: unable to write runtime file: $!"; |
|
|
2625 | }; |
2617 | }, |
2626 | }, |
2618 | ); |
2627 | ); |
2619 | |
2628 | |
2620 | END { cf::emergency_save } |
2629 | END { cf::emergency_save } |
2621 | |
2630 | |