ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.531 by root, Wed Apr 28 21:07:41 2010 UTC vs.
Revision 1.532 by root, Thu Apr 29 07:32:34 2010 UTC

252 252
253=back 253=back
254 254
255=cut 255=cut
256 256
257sub error(@) { LOG llevError, join "", @_ }
258sub warn (@) { LOG llevWarn , join "", @_ }
259sub info (@) { LOG llevInfo , join "", @_ }
260sub debug(@) { LOG llevDebug, join "", @_ }
261sub trace(@) { LOG llevTrace, join "", @_ }
262
257$Coro::State::WARNHOOK = sub { 263$Coro::State::WARNHOOK = sub {
258 my $msg = join "", @_; 264 my $msg = join "", @_;
259 265
260 $msg .= "\n" 266 $msg .= "\n"
261 unless $msg =~ /\n$/; 267 unless $msg =~ /\n$/;
262 268
263 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 269 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
264 270
265 LOG llevError, $msg; 271 LOG llevWarn, $msg;
266}; 272};
267 273
268$Coro::State::DIEHOOK = sub { 274$Coro::State::DIEHOOK = sub {
269 return unless $^S eq 0; # "eq", not "==" 275 return unless $^S eq 0; # "eq", not "=="
270 276
271 warn Carp::longmess $_[0]; 277 error Carp::longmess $_[0];
272 278
273 if (in_main) {#d# 279 if (in_main) {#d#
274 warn "DIEHOOK called in main context, Coro bug?\n";#d# 280 error "DIEHOOK called in main context, Coro bug?\n";#d#
275 return;#d# 281 return;#d#
276 }#d# 282 }#d#
277 283
278 # kill coroutine otherwise 284 # kill coroutine otherwise
279 Coro::terminate 285 Coro::terminate
528 my @res; 534 my @res;
529 535
530 (async { 536 (async {
531 $Coro::current->desc ("sync job coro"); 537 $Coro::current->desc ("sync job coro");
532 @res = eval { $job->() }; 538 @res = eval { $job->() };
533 warn $@ if $@; 539 error $@ if $@;
534 undef $busy; 540 undef $busy;
535 })->prio (Coro::PRIO_MAX); 541 })->prio (Coro::PRIO_MAX);
536 542
537 while ($busy) { 543 while ($busy) {
538 if (Coro::nready) { 544 if (Coro::nready) {
749 755
750 my $t1 = Time::HiRes::time; 756 my $t1 = Time::HiRes::time;
751 my $data = $process->(\@data); 757 my $data = $process->(\@data);
752 my $t2 = Time::HiRes::time; 758 my $t2 = Time::HiRes::time;
753 759
754 warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; 760 info "cache: '$id' processed in ", $t2 - $t1, "s\n";
755 761
756 db_put cache => "$id/data", $data; 762 db_put cache => "$id/data", $data;
757 db_put cache => "$id/md5" , $md5; 763 db_put cache => "$id/md5" , $md5;
758 db_put cache => "$id/meta", $meta; 764 db_put cache => "$id/meta", $meta;
759 765
769 775
770=cut 776=cut
771 777
772sub datalog($@) { 778sub datalog($@) {
773 my ($type, %kv) = @_; 779 my ($type, %kv) = @_;
774 warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); 780 info "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type });
775} 781}
776 782
777=back 783=back
778 784
779=cut 785=cut
974 980
975 } elsif (exists $cb_id{$type}) { 981 } elsif (exists $cb_id{$type}) {
976 _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; 982 _attach_cb $registry, $cb_id{$type}, $prio, shift @arg;
977 983
978 } elsif (ref $type) { 984 } elsif (ref $type) {
979 warn "attaching objects not supported, ignoring.\n"; 985 error "attaching objects not supported, ignoring.\n";
980 986
981 } else { 987 } else {
982 shift @arg; 988 shift @arg;
983 warn "attach argument '$type' not supported, ignoring.\n"; 989 error "attach argument '$type' not supported, ignoring.\n";
984 } 990 }
985 } 991 }
986} 992}
987 993
988sub _object_attach { 994sub _object_attach {
998 _attach $registry, $klass, @attach; 1004 _attach $registry, $klass, @attach;
999 } 1005 }
1000 1006
1001 $obj->{$name} = \%arg; 1007 $obj->{$name} = \%arg;
1002 } else { 1008 } else {
1003 warn "object uses attachment '$name' which is not available, postponing.\n"; 1009 info "object uses attachment '$name' which is not available, postponing.\n";
1004 } 1010 }
1005 1011
1006 $obj->{_attachment}{$name} = undef; 1012 $obj->{_attachment}{$name} = undef;
1007} 1013}
1008 1014
1067 1073
1068 for (@$callbacks) { 1074 for (@$callbacks) {
1069 eval { &{$_->[1]} }; 1075 eval { &{$_->[1]} };
1070 1076
1071 if ($@) { 1077 if ($@) {
1072 warn "$@";
1073 warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; 1078 error "$@", "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n";
1074 override; 1079 override;
1075 } 1080 }
1076 1081
1077 return 1 if $override; 1082 return 1 if $override;
1078 } 1083 }
1157 for (@$attach) { 1162 for (@$attach) {
1158 my ($klass, @attach) = @$_; 1163 my ($klass, @attach) = @$_;
1159 _attach $registry, $klass, @attach; 1164 _attach $registry, $klass, @attach;
1160 } 1165 }
1161 } else { 1166 } else {
1162 warn "object uses attachment '$name' that is not available, postponing.\n"; 1167 info "object uses attachment '$name' that is not available, postponing.\n";
1163 } 1168 }
1164 } 1169 }
1165} 1170}
1166 1171
1167cf::attachable->attach ( 1172cf::attachable->attach (
1194 my ($filename, $rdata, $objs) = @_; 1199 my ($filename, $rdata, $objs) = @_;
1195 1200
1196 sync_job { 1201 sync_job {
1197 if (length $$rdata) { 1202 if (length $$rdata) {
1198 utf8::decode (my $decname = $filename); 1203 utf8::decode (my $decname = $filename);
1199 warn sprintf "saving %s (%d,%d)\n", 1204 trace sprintf "saving %s (%d,%d)\n",
1200 $decname, length $$rdata, scalar @$objs 1205 $decname, length $$rdata, scalar @$objs
1201 if $VERBOSE_IO; 1206 if $VERBOSE_IO;
1202 1207
1203 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { 1208 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
1204 aio_chmod $fh, SAVE_MODE; 1209 aio_chmod $fh, SAVE_MODE;
1205 aio_write $fh, 0, (length $$rdata), $$rdata, 0; 1210 aio_write $fh, 0, (length $$rdata), $$rdata, 0;
1228 aio_rename "$filename~", $filename; 1233 aio_rename "$filename~", $filename;
1229 1234
1230 $filename =~ s%/[^/]+$%%; 1235 $filename =~ s%/[^/]+$%%;
1231 aio_pathsync $filename if $cf::USE_FSYNC; 1236 aio_pathsync $filename if $cf::USE_FSYNC;
1232 } else { 1237 } else {
1233 warn "unable to save objects: $filename~: $!\n"; 1238 error "unable to save objects: $filename~: $!\n";
1234 } 1239 }
1235 } else { 1240 } else {
1236 aio_unlink $filename; 1241 aio_unlink $filename;
1237 aio_unlink "$filename.pst"; 1242 aio_unlink "$filename.pst";
1238 } 1243 }
1262 my $st = eval { Coro::Storable::thaw $av }; 1267 my $st = eval { Coro::Storable::thaw $av };
1263 $av = $st->{objs}; 1268 $av = $st->{objs};
1264 } 1269 }
1265 1270
1266 utf8::decode (my $decname = $filename); 1271 utf8::decode (my $decname = $filename);
1267 warn sprintf "loading %s (%d,%d)\n", 1272 trace sprintf "loading %s (%d,%d)\n",
1268 $decname, length $data, scalar @{$av || []} 1273 $decname, length $data, scalar @{$av || []}
1269 if $VERBOSE_IO; 1274 if $VERBOSE_IO;
1270 1275
1271 ($data, $av) 1276 ($data, $av)
1272} 1277}
1273 1278
1366 1371
1367 $pl->ext_reply ($reply, @reply) 1372 $pl->ext_reply ($reply, @reply)
1368 if $reply; 1373 if $reply;
1369 1374
1370 } else { 1375 } else {
1371 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 1376 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
1372 } 1377 }
1373 1378
1374 cf::override; 1379 cf::override;
1375 }, 1380 },
1376); 1381);
1387 1392
1388 $grp 1393 $grp
1389} 1394}
1390 1395
1391sub load_extensions { 1396sub load_extensions {
1397 info "loading extensions...";
1398
1392 cf::sync_job { 1399 cf::sync_job {
1393 my %todo; 1400 my %todo;
1394 1401
1395 for my $path (<$LIBDIR/*.ext>) { 1402 for my $path (<$LIBDIR/*.ext>) {
1396 next unless -r $path; 1403 next unless -r $path;
1436 for (split /,\s*/, $v->{meta}{depends}) { 1443 for (split /,\s*/, $v->{meta}{depends}) {
1437 next ext 1444 next ext
1438 unless exists $done{$_}; 1445 unless exists $done{$_};
1439 } 1446 }
1440 1447
1441 warn "... pass $pass, loading '$k' into '$v->{pkg}'\n"; 1448 trace "... pass $pass, loading '$k' into '$v->{pkg}'\n";
1442 1449
1443 my $active = eval $v->{source}; 1450 my $active = eval $v->{source};
1444 1451
1445 if (length $@) { 1452 if (length $@) {
1446 warn "$v->{path}: $@\n"; 1453 error "$v->{path}: $@\n";
1447 1454
1448 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1455 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1449 if exists $v->{meta}{mandatory}; 1456 if exists $v->{meta}{mandatory};
1450 1457
1451 warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; 1458 warn "$v->{base}: optional extension cannot be loaded, skipping.\n";
1453 } else { 1460 } else {
1454 $done{$k} = delete $todo{$k}; 1461 $done{$k} = delete $todo{$k};
1455 push @EXTS, $v->{pkg}; 1462 push @EXTS, $v->{pkg};
1456 $progress = 1; 1463 $progress = 1;
1457 1464
1458 warn "$v->{base}: extension inactive.\n" 1465 info "$v->{base}: extension inactive.\n"
1459 unless $active; 1466 unless $active;
1460 } 1467 }
1461 } 1468 }
1462 1469
1463 unless ($progress) { 1470 unless ($progress) {
2314 2321
2315 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2322 my $lock = cf::lock_acquire "map_data:$self->{path}";
2316 2323
2317 return if $self->players; 2324 return if $self->players;
2318 2325
2319 warn "resetting map ", $self->path, "\n"; 2326 cf::trace "resetting map ", $self->path, "\n";
2320 2327
2321 $self->in_memory (cf::MAP_SWAPPED); 2328 $self->in_memory (cf::MAP_SWAPPED);
2322 2329
2323 # need to save uniques path 2330 # need to save uniques path
2324 unless ($self->{deny_save}) { 2331 unless ($self->{deny_save}) {
2689 2696
2690sub cf::object::player::goto { 2697sub cf::object::player::goto {
2691 my ($self, $path, $x, $y, $check, $done) = @_; 2698 my ($self, $path, $x, $y, $check, $done) = @_;
2692 2699
2693 if ($self->{_link_recursion} >= $MAX_LINKS) { 2700 if ($self->{_link_recursion} >= $MAX_LINKS) {
2694 warn "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; 2701 error "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting.";
2695 $self->failmsg ("Something went wrong inside the server - please contact an administrator!"); 2702 $self->failmsg ("Something went wrong inside the server - please contact an administrator!");
2696 ($path, $x, $y) = @$EMERGENCY_POSITION; 2703 ($path, $x, $y) = @$EMERGENCY_POSITION;
2697 } 2704 }
2698 2705
2699 # do generation counting so two concurrent goto's will be executed in-order 2706 # do generation counting so two concurrent goto's will be executed in-order
2847 $self->message ("Something went wrong deep within the deliantra server. " 2854 $self->message ("Something went wrong deep within the deliantra server. "
2848 . "I'll try to bring you back to the map you were before. " 2855 . "I'll try to bring you back to the map you were before. "
2849 . "Please report this to the dungeon master!", 2856 . "Please report this to the dungeon master!",
2850 cf::NDI_UNIQUE | cf::NDI_RED); 2857 cf::NDI_UNIQUE | cf::NDI_RED);
2851 2858
2852 warn "ERROR in enter_exit: $@"; 2859 error "ERROR in enter_exit: $@";
2853 $self->leave_link; 2860 $self->leave_link;
2854 } 2861 }
2855 })->prio (1); 2862 })->prio (1);
2856} 2863}
2857 2864
3170 3177
3171 $ns->ext_reply ($reply, @reply) 3178 $ns->ext_reply ($reply, @reply)
3172 if $reply; 3179 if $reply;
3173 3180
3174 } else { 3181 } else {
3175 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 3182 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
3176 } 3183 }
3177 3184
3178 cf::override; 3185 cf::override;
3179 }, 3186 },
3180); 3187);
3310 local @cf::_safe_eval_args = values %vars; 3317 local @cf::_safe_eval_args = values %vars;
3311 @res = wantarray ? eval eval : scalar eval $eval; 3318 @res = wantarray ? eval eval : scalar eval $eval;
3312 } 3319 }
3313 3320
3314 if ($@) { 3321 if ($@) {
3315 warn "$@"; 3322 warn "$@",
3316 warn "while executing safe code '$code'\n"; 3323 "while executing safe code '$code'\n",
3317 warn "with arguments " . (join " ", %vars) . "\n"; 3324 "with arguments " . (join " ", %vars) . "\n";
3318 } 3325 }
3319 3326
3320 wantarray ? @res : $res[0] 3327 wantarray ? @res : $res[0]
3321} 3328}
3322 3329
3356 # for this (global event?) 3363 # for this (global event?)
3357 %ext::player_env::MUSIC_FACE_CACHE = (); 3364 %ext::player_env::MUSIC_FACE_CACHE = ();
3358 3365
3359 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3366 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3360 3367
3361 warn "loading facedata from $path\n"; 3368 trace "loading facedata from $path\n";
3362 3369
3363 my $facedata; 3370 my $facedata;
3364 0 < aio_load $path, $facedata 3371 0 < aio_load $path, $facedata
3365 or die "$path: $!"; 3372 or die "$path: $!";
3366 3373
3400 3407
3401 if (my $smooth = cf::face::find $info->{smooth}) { 3408 if (my $smooth = cf::face::find $info->{smooth}) {
3402 cf::face::set_smooth $idx, $smooth; 3409 cf::face::set_smooth $idx, $smooth;
3403 cf::face::set_smoothlevel $idx, $info->{smoothlevel}; 3410 cf::face::set_smoothlevel $idx, $info->{smoothlevel};
3404 } else { 3411 } else {
3405 warn "smooth face '$info->{smooth}' not found for face '$face'"; 3412 error "smooth face '$info->{smooth}' not found for face '$face'";
3406 } 3413 }
3407 3414
3408 cf::cede_to_tick; 3415 cf::cede_to_tick;
3409 } 3416 }
3410 } 3417 }
3488 load_resource_file "$DATADIR/treasures" 3495 load_resource_file "$DATADIR/treasures"
3489 or die "unable to load treasurelists\n"; 3496 or die "unable to load treasurelists\n";
3490} 3497}
3491 3498
3492sub reload_sound { 3499sub reload_sound {
3493 warn "loading sound config from $DATADIR/sound\n"; 3500 trace "loading sound config from $DATADIR/sound\n";
3494 3501
3495 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data 3502 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data
3496 or die "$DATADIR/sound $!"; 3503 or die "$DATADIR/sound $!";
3497 3504
3498 my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); 3505 my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data);
3511 cf::sound::set $k => $face; 3518 cf::sound::set $k => $face;
3512 } 3519 }
3513} 3520}
3514 3521
3515sub reload_resources { 3522sub reload_resources {
3516 warn "reloading resource files...\n"; 3523 trace "reloading resource files...\n";
3517 3524
3518 reload_facedata; 3525 reload_facedata;
3519 reload_sound; 3526 reload_sound;
3520 reload_archetypes; 3527 reload_archetypes;
3521 reload_regions; 3528 reload_regions;
3522 reload_treasures; 3529 reload_treasures;
3523 3530
3524 warn "finished reloading resource files\n"; 3531 trace "finished reloading resource files\n";
3525} 3532}
3526 3533
3527sub reload_config { 3534sub reload_config {
3528 warn "reloading config file...\n"; 3535 trace "reloading config file...\n";
3529 3536
3530 open my $fh, "<:utf8", "$CONFDIR/config" 3537 open my $fh, "<:utf8", "$CONFDIR/config"
3531 or return; 3538 or return;
3532 3539
3533 local $/; 3540 local $/;
3544 and die "WARNING: m(un)lockall failed: $!\n"; 3551 and die "WARNING: m(un)lockall failed: $!\n";
3545 }; 3552 };
3546 warn $@ if $@; 3553 warn $@ if $@;
3547 } 3554 }
3548 3555
3549 warn "finished reloading resource files\n"; 3556 trace "finished reloading resource files\n";
3550} 3557}
3551 3558
3552sub pidfile() { 3559sub pidfile() {
3553 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3560 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3554 or die "$PIDFILE: $!"; 3561 or die "$PIDFILE: $!";
3567 seek $fh, 0, 0; 3574 seek $fh, 0, 0;
3568 print $fh $$; 3575 print $fh $$;
3569} 3576}
3570 3577
3571sub main_loop { 3578sub main_loop {
3572 warn "EV::loop starting\n"; 3579 trace "EV::loop starting\n";
3573 if (1) { 3580 if (1) {
3574 EV::loop; 3581 EV::loop;
3575 } 3582 }
3576 warn "EV::loop returned\n"; 3583 trace "EV::loop returned\n";
3577 goto &main_loop unless $REALLY_UNLOOP; 3584 goto &main_loop unless $REALLY_UNLOOP;
3578} 3585}
3579 3586
3580sub main { 3587sub main {
3581 cf::init_globals; # initialise logging 3588 cf::init_globals; # initialise logging
3629 3636
3630 (pop @POST_INIT)->(0) while @POST_INIT; 3637 (pop @POST_INIT)->(0) while @POST_INIT;
3631 }; 3638 };
3632 3639
3633 cf::object::thawer::errors_are_fatal 0; 3640 cf::object::thawer::errors_are_fatal 0;
3634 warn "parse errors in files are no longer fatal from this point on.\n"; 3641 info "parse errors in files are no longer fatal from this point on.\n";
3635 3642
3636 main_loop; 3643 main_loop;
3637} 3644}
3638 3645
3639############################################################################# 3646#############################################################################
3680 or return; 3687 or return;
3681 3688
3682 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE 3689 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3683 and return; 3690 and return;
3684 3691
3685 warn sprintf "runtime file written (%gs).\n", AE::time - $t0; 3692 trace sprintf "runtime file written (%gs).\n", AE::time - $t0;
3686 3693
3687 1 3694 1
3688} 3695}
3689 3696
3690our $uuid_lock; 3697our $uuid_lock;
3702 or return; 3709 or return;
3703 3710
3704 my $value = uuid_seq uuid_cur; 3711 my $value = uuid_seq uuid_cur;
3705 3712
3706 unless ($value) { 3713 unless ($value) {
3707 warn "cowardly refusing to write zero uuid value!\n"; 3714 info "cowardly refusing to write zero uuid value!\n";
3708 return; 3715 return;
3709 } 3716 }
3710 3717
3711 my $value = uuid_str $value + $uuid_skip; 3718 my $value = uuid_str $value + $uuid_skip;
3712 $uuid_skip = 0; 3719 $uuid_skip = 0;
3722 or return; 3729 or return;
3723 3730
3724 aio_rename "$uuid~", $uuid 3731 aio_rename "$uuid~", $uuid
3725 and return; 3732 and return;
3726 3733
3727 warn "uuid file written ($value).\n"; 3734 trace "uuid file written ($value).\n";
3728 3735
3729 1 3736 1
3730 3737
3731} 3738}
3732 3739
3738} 3745}
3739 3746
3740sub emergency_save() { 3747sub emergency_save() {
3741 my $freeze_guard = cf::freeze_mainloop; 3748 my $freeze_guard = cf::freeze_mainloop;
3742 3749
3743 warn "emergency_perl_save: enter\n"; 3750 info "emergency_perl_save: enter\n";
3744 3751
3745 cf::sync_job { 3752 cf::sync_job {
3746 # this is a trade-off: we want to be very quick here, so 3753 # this is a trade-off: we want to be very quick here, so
3747 # save all maps without fsync, and later call a global sync 3754 # save all maps without fsync, and later call a global sync
3748 # (which in turn might be very very slow) 3755 # (which in turn might be very very slow)
3749 local $USE_FSYNC = 0; 3756 local $USE_FSYNC = 0;
3750 3757
3751 # use a peculiar iteration method to avoid tripping on perl 3758 # use a peculiar iteration method to avoid tripping on perl
3752 # refcount bugs in for. also avoids problems with players 3759 # refcount bugs in for. also avoids problems with players
3753 # and maps saved/destroyed asynchronously. 3760 # and maps saved/destroyed asynchronously.
3754 warn "emergency_perl_save: begin player save\n"; 3761 info "emergency_perl_save: begin player save\n";
3755 for my $login (keys %cf::PLAYER) { 3762 for my $login (keys %cf::PLAYER) {
3756 my $pl = $cf::PLAYER{$login} or next; 3763 my $pl = $cf::PLAYER{$login} or next;
3757 $pl->valid or next; 3764 $pl->valid or next;
3758 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3765 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3759 $pl->save; 3766 $pl->save;
3760 } 3767 }
3761 warn "emergency_perl_save: end player save\n"; 3768 info "emergency_perl_save: end player save\n";
3762 3769
3763 warn "emergency_perl_save: begin map save\n"; 3770 info "emergency_perl_save: begin map save\n";
3764 for my $path (keys %cf::MAP) { 3771 for my $path (keys %cf::MAP) {
3765 my $map = $cf::MAP{$path} or next; 3772 my $map = $cf::MAP{$path} or next;
3766 $map->valid or next; 3773 $map->valid or next;
3767 $map->save; 3774 $map->save;
3768 } 3775 }
3769 warn "emergency_perl_save: end map save\n"; 3776 info "emergency_perl_save: end map save\n";
3770 3777
3771 warn "emergency_perl_save: begin database checkpoint\n"; 3778 info "emergency_perl_save: begin database checkpoint\n";
3772 BDB::db_env_txn_checkpoint $DB_ENV; 3779 BDB::db_env_txn_checkpoint $DB_ENV;
3773 warn "emergency_perl_save: end database checkpoint\n"; 3780 info "emergency_perl_save: end database checkpoint\n";
3774 3781
3775 warn "emergency_perl_save: begin write uuid\n"; 3782 info "emergency_perl_save: begin write uuid\n";
3776 write_uuid_sync 1; 3783 write_uuid_sync 1;
3777 warn "emergency_perl_save: end write uuid\n"; 3784 info "emergency_perl_save: end write uuid\n";
3778 }; 3785 };
3779 3786
3780 warn "emergency_perl_save: starting sync()\n"; 3787 info "emergency_perl_save: starting sync()\n";
3781 IO::AIO::aio_sync sub { 3788 IO::AIO::aio_sync sub {
3782 warn "emergency_perl_save: finished sync()\n"; 3789 info "emergency_perl_save: finished sync()\n";
3783 }; 3790 };
3784 3791
3785 warn "emergency_perl_save: leave\n"; 3792 info "emergency_perl_save: leave\n";
3786} 3793}
3787 3794
3788sub post_cleanup { 3795sub post_cleanup {
3789 my ($make_core) = @_; 3796 my ($make_core) = @_;
3790 3797
3791 warn Carp::longmess "post_cleanup backtrace" 3798 error Carp::longmess "post_cleanup backtrace"
3792 if $make_core; 3799 if $make_core;
3793 3800
3794 my $fh = pidfile; 3801 my $fh = pidfile;
3795 unlink $PIDFILE if <$fh> == $$; 3802 unlink $PIDFILE if <$fh> == $$;
3796} 3803}
3821} 3828}
3822 3829
3823sub do_reload_perl() { 3830sub do_reload_perl() {
3824 # can/must only be called in main 3831 # can/must only be called in main
3825 if (in_main) { 3832 if (in_main) {
3826 warn "can only reload from main coroutine"; 3833 error "can only reload from main coroutine";
3827 return; 3834 return;
3828 } 3835 }
3829 3836
3830 return if $RELOAD++; 3837 return if $RELOAD++;
3831 3838
3832 my $t1 = AE::time; 3839 my $t1 = AE::time;
3833 3840
3834 while ($RELOAD) { 3841 while ($RELOAD) {
3835 warn "reloading..."; 3842 info "reloading...";
3836 3843
3837 warn "entering sync_job"; 3844 trace "entering sync_job";
3838 3845
3839 cf::sync_job { 3846 cf::sync_job {
3840 cf::write_runtime_sync; # external watchdog should not bark 3847 cf::write_runtime_sync; # external watchdog should not bark
3841 cf::emergency_save; 3848 cf::emergency_save;
3842 cf::write_runtime_sync; # external watchdog should not bark 3849 cf::write_runtime_sync; # external watchdog should not bark
3843 3850
3844 warn "syncing database to disk"; 3851 trace "syncing database to disk";
3845 BDB::db_env_txn_checkpoint $DB_ENV; 3852 BDB::db_env_txn_checkpoint $DB_ENV;
3846 3853
3847 # if anything goes wrong in here, we should simply crash as we already saved 3854 # if anything goes wrong in here, we should simply crash as we already saved
3848 3855
3849 warn "flushing outstanding aio requests"; 3856 trace "flushing outstanding aio requests";
3850 while (IO::AIO::nreqs || BDB::nreqs) { 3857 while (IO::AIO::nreqs || BDB::nreqs) {
3851 Coro::EV::timer_once 0.01; # let the sync_job do it's thing 3858 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3852 } 3859 }
3853 3860
3854 warn "cancelling all extension coros"; 3861 trace "cancelling all extension coros";
3855 $_->cancel for values %EXT_CORO; 3862 $_->cancel for values %EXT_CORO;
3856 %EXT_CORO = (); 3863 %EXT_CORO = ();
3857 3864
3858 warn "removing commands"; 3865 trace "removing commands";
3859 %COMMAND = (); 3866 %COMMAND = ();
3860 3867
3861 warn "removing ext/exti commands"; 3868 trace "removing ext/exti commands";
3862 %EXTCMD = (); 3869 %EXTCMD = ();
3863 %EXTICMD = (); 3870 %EXTICMD = ();
3864 3871
3865 warn "unloading/nuking all extensions"; 3872 trace "unloading/nuking all extensions";
3866 for my $pkg (@EXTS) { 3873 for my $pkg (@EXTS) {
3867 warn "... unloading $pkg"; 3874 trace "... unloading $pkg";
3868 3875
3869 if (my $cb = $pkg->can ("unload")) { 3876 if (my $cb = $pkg->can ("unload")) {
3870 eval { 3877 eval {
3871 $cb->($pkg); 3878 $cb->($pkg);
3872 1 3879 1
3873 } or warn "$pkg unloaded, but with errors: $@"; 3880 } or error "$pkg unloaded, but with errors: $@";
3874 } 3881 }
3875 3882
3876 warn "... clearing $pkg"; 3883 trace "... clearing $pkg";
3877 clear_package $pkg; 3884 clear_package $pkg;
3878 } 3885 }
3879 3886
3880 warn "unloading all perl modules loaded from $LIBDIR"; 3887 trace "unloading all perl modules loaded from $LIBDIR";
3881 while (my ($k, $v) = each %INC) { 3888 while (my ($k, $v) = each %INC) {
3882 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 3889 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
3883 3890
3884 warn "... unloading $k"; 3891 trace "... unloading $k";
3885 delete $INC{$k}; 3892 delete $INC{$k};
3886 3893
3887 $k =~ s/\.pm$//; 3894 $k =~ s/\.pm$//;
3888 $k =~ s/\//::/g; 3895 $k =~ s/\//::/g;
3889 3896
3892 } 3899 }
3893 3900
3894 clear_package $k; 3901 clear_package $k;
3895 } 3902 }
3896 3903
3897 warn "getting rid of safe::, as good as possible"; 3904 trace "getting rid of safe::, as good as possible";
3898 clear_package "safe::$_" 3905 clear_package "safe::$_"
3899 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3906 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3900 3907
3901 warn "unloading cf.pm \"a bit\""; 3908 trace "unloading cf.pm \"a bit\"";
3902 delete $INC{"cf.pm"}; 3909 delete $INC{"cf.pm"};
3903 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; 3910 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3904 3911
3905 # don't, removes xs symbols, too, 3912 # don't, removes xs symbols, too,
3906 # and global variables created in xs 3913 # and global variables created in xs
3907 #clear_package __PACKAGE__; 3914 #clear_package __PACKAGE__;
3908 3915
3909 warn "unload completed, starting to reload now"; 3916 info "unload completed, starting to reload now";
3910 3917
3911 warn "reloading cf.pm"; 3918 trace "reloading cf.pm";
3912 require cf; 3919 require cf;
3913 cf::_connect_to_perl_1; 3920 cf::_connect_to_perl_1;
3914 3921
3915 warn "loading config and database again"; 3922 trace "loading config and database again";
3916 cf::reload_config; 3923 cf::reload_config;
3917 3924
3918 warn "loading extensions"; 3925 trace "loading extensions";
3919 cf::load_extensions; 3926 cf::load_extensions;
3920 3927
3921 if ($REATTACH_ON_RELOAD) { 3928 if ($REATTACH_ON_RELOAD) {
3922 warn "reattaching attachments to objects/players"; 3929 trace "reattaching attachments to objects/players";
3923 _global_reattach; # objects, sockets 3930 _global_reattach; # objects, sockets
3924 warn "reattaching attachments to maps"; 3931 trace "reattaching attachments to maps";
3925 reattach $_ for values %MAP; 3932 reattach $_ for values %MAP;
3926 warn "reattaching attachments to players"; 3933 trace "reattaching attachments to players";
3927 reattach $_ for values %PLAYER; 3934 reattach $_ for values %PLAYER;
3928 } 3935 }
3929 3936
3930 warn "running post_init jobs"; 3937 trace "running post_init jobs";
3931 (pop @POST_INIT)->(1) while @POST_INIT; 3938 (pop @POST_INIT)->(1) while @POST_INIT;
3932 3939
3933 warn "leaving sync_job"; 3940 trace "leaving sync_job";
3934 3941
3935 1 3942 1
3936 } or do { 3943 } or do {
3937 warn $@; 3944 error $@;
3938 cf::cleanup "error while reloading, exiting."; 3945 cf::cleanup "error while reloading, exiting.";
3939 }; 3946 };
3940 3947
3941 warn "reloaded"; 3948 info "reloaded";
3942 --$RELOAD; 3949 --$RELOAD;
3943 } 3950 }
3944 3951
3945 $t1 = AE::time - $t1; 3952 $t1 = AE::time - $t1;
3946 warn "reload completed in ${t1}s\n"; 3953 info "reload completed in ${t1}s\n";
3947}; 3954};
3948 3955
3949our $RELOAD_WATCHER; # used only during reload 3956our $RELOAD_WATCHER; # used only during reload
3950 3957
3951sub reload_perl() { 3958sub reload_perl() {
4011 if ($NOW >= $NEXT_RUNTIME_WRITE) { 4018 if ($NOW >= $NEXT_RUNTIME_WRITE) {
4012 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 4019 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
4013 Coro::async_pool { 4020 Coro::async_pool {
4014 $Coro::current->{desc} = "runtime saver"; 4021 $Coro::current->{desc} = "runtime saver";
4015 write_runtime_sync 4022 write_runtime_sync
4016 or warn "ERROR: unable to write runtime file: $!"; 4023 or error "ERROR: unable to write runtime file: $!";
4017 }; 4024 };
4018 } 4025 }
4019 4026
4020 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 4027 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
4021 $sig->send; 4028 $sig->send;
4029 4036
4030 if (0) { 4037 if (0) {
4031 if ($NEXT_TICK) { 4038 if ($NEXT_TICK) {
4032 my $jitter = $TICK_START - $NEXT_TICK; 4039 my $jitter = $TICK_START - $NEXT_TICK;
4033 $JITTER = $JITTER * 0.75 + $jitter * 0.25; 4040 $JITTER = $JITTER * 0.75 + $jitter * 0.25;
4034 warn "jitter $JITTER\n";#d# 4041 debug "jitter $JITTER\n";#d#
4035 } 4042 }
4036 } 4043 }
4037} 4044}
4038 4045
4039{ 4046{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines