… | |
… | |
356 | |
356 | |
357 | $coro |
357 | $coro |
358 | } |
358 | } |
359 | |
359 | |
360 | sub write_runtime { |
360 | sub write_runtime { |
361 | $TICK_WATCHER->is_active or cf::cleanup "mainloop frozen but runtime active", 1; |
|
|
362 | |
|
|
363 | my $runtime = cf::localdir . "/runtime"; |
361 | my $runtime = cf::localdir . "/runtime"; |
364 | |
362 | |
365 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
363 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
366 | or return; |
364 | or return; |
367 | |
365 | |
… | |
… | |
2372 | return; |
2370 | return; |
2373 | } |
2371 | } |
2374 | |
2372 | |
2375 | warn "reloading..."; |
2373 | warn "reloading..."; |
2376 | |
2374 | |
2377 | warn "cancelling server ticker"; |
2375 | warn "entering sync_job"; |
2378 | $TICK_WATCHER->cancel; |
|
|
2379 | |
2376 | |
|
|
2377 | sync_job { |
|
|
2378 | write_runtime; |
2380 | cf::emergency_save; |
2379 | cf::emergency_save; |
|
|
2380 | write_runtime; |
2381 | |
2381 | |
2382 | eval { |
2382 | warn "syncing database to disk"; |
|
|
2383 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2384 | |
2383 | # if anything goes wrong in here, we should simply crash as we already saved |
2385 | # if anything goes wrong in here, we should simply crash as we already saved |
2384 | |
2386 | |
2385 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2387 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2386 | for (Event::all_watchers) { |
2388 | for (Event::all_watchers) { |
2387 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2389 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2388 | } |
2390 | } |
2389 | |
|
|
2390 | warn "syncing database to disk"; |
|
|
2391 | BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
2392 | |
2391 | |
2393 | warn "flushing outstanding aio requests"; |
2392 | warn "flushing outstanding aio requests"; |
2394 | for (;;) { |
2393 | for (;;) { |
2395 | BDB::flush; |
2394 | BDB::flush; |
2396 | IO::AIO::flush; |
2395 | IO::AIO::flush; |
… | |
… | |
2470 | reattach $_ for values %MAP; |
2469 | reattach $_ for values %MAP; |
2471 | |
2470 | |
2472 | warn "loading reloadable resources"; |
2471 | warn "loading reloadable resources"; |
2473 | load_resources; |
2472 | load_resources; |
2474 | |
2473 | |
2475 | warn "restarting server ticker"; |
2474 | warn "leaving sync_job"; |
2476 | |
2475 | |
2477 | $TICK_WATCHER->start; |
2476 | 1 |
2478 | }; |
2477 | } or do { |
2479 | |
|
|
2480 | if ($@) { |
|
|
2481 | warn $@; |
2478 | warn $@; |
2482 | warn "error while reloading, exiting."; |
2479 | warn "error while reloading, exiting."; |
2483 | exit 1; |
2480 | exit 1; |
2484 | } |
2481 | }; |
2485 | |
2482 | |
2486 | warn "reloaded"; |
2483 | warn "reloaded"; |
2487 | }; |
2484 | }; |
2488 | |
2485 | |
2489 | our $RELOAD_WATCHER; # used only during reload |
2486 | our $RELOAD_WATCHER; # used only during reload |
… | |
… | |
2533 | $NEXT_TICK += $TICK; |
2530 | $NEXT_TICK += $TICK; |
2534 | |
2531 | |
2535 | $WAIT_FOR_TICK->broadcast; |
2532 | $WAIT_FOR_TICK->broadcast; |
2536 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2533 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2537 | |
2534 | |
2538 | Event::sweep; |
|
|
2539 | Coro::cede_notself; |
|
|
2540 | |
|
|
2541 | # my $AFTER = Event::time; |
2535 | # my $AFTER = Event::time; |
2542 | # warn $AFTER - $NOW;#d# |
2536 | # warn $AFTER - $NOW;#d# |
2543 | |
2537 | |
2544 | # if we are delayed by four ticks or more, skip them all |
2538 | # if we are delayed by four ticks or more, skip them all |
2545 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2539 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
… | |
… | |
2615 | } |
2609 | } |
2616 | |
2610 | |
2617 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2611 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2618 | reentrant => 0, |
2612 | reentrant => 0, |
2619 | data => WF_AUTOCANCEL, |
2613 | data => WF_AUTOCANCEL, |
|
|
2614 | parked => 1, |
2620 | after => 1, |
2615 | after => 1, |
2621 | interval => 10, |
2616 | interval => 10, |
2622 | prio => 6, # keep it lowest so it acts like a watchdog |
2617 | prio => 6, # keep it lowest so it acts like a watchdog |
2623 | cb => Coro::unblock_sub { |
2618 | cb => sub { |
|
|
2619 | $TICK_WATCHER->is_active or cf::cleanup "mainloop frozen but runtime active", 1; |
|
|
2620 | |
|
|
2621 | Coro::async_pool { |
2624 | write_runtime |
2622 | write_runtime |
2625 | or warn "ERROR: unable to write runtime file: $!"; |
2623 | or warn "ERROR: unable to write runtime file: $!"; |
|
|
2624 | }; |
2626 | }, |
2625 | }, |
2627 | ); |
2626 | ); |
2628 | |
2627 | |
2629 | END { cf::emergency_save } |
2628 | END { cf::emergency_save } |
2630 | |
2629 | |