revbank/lib/RevBank/FileIO.pm
Juerd Waalboer 52749df5f3 Ignore all hook exceptions except in hook_checkout_prepare
A space had a custom plugin that died during hook_checkout, which caused
the CHECKOUT lines to be logged without the corresponding BALANCE, and
indeed no account balances were updated. While the plugin had a bug, it
should not cause a half transaction in RevBank.

After some hesitation, I went with ON ERROR RESUME NEXT because if a
hook throws an exception, that should not interfere with other plugins
(the hook can return ABORT if this it was intentional), including the
calling plugin. An error message is printed (but not logged... TODO: add
hook_plugin_fail to plugins/log) but the show must go on.

During hook_checkout_prepare, however, nothing is set in stone yet, so
this could be used for something that might die, and this instance of
call_hooks() is now the one place where a failing hook should result in
the transaction getting aborted. For this, call_hooks() now returns a
success status boolean. Maybe it would make sense in more places, but I
didn't identify any such calls yet.

RevBank::Cart->checkout used to return a success status boolean, but it
could just as well just die (indirectly, to abort the transaction) since
it can't be called a second time within the same transaction anyway
(because ->set_user must be called exactly once), so continuing with the
same transaction can't result in anything useful anyway.

In some places, error messages were slightly improved to contain a bit
more information.
2023-11-24 05:15:22 +01:00

121 lines
2.1 KiB
Perl

package RevBank::FileIO;
use v5.28;
use warnings;
use experimental 'signatures'; # stable since v5.36
use autodie;
use Fcntl qw(:flock);
use Carp qw(croak);
use Time::HiRes qw(sleep);
my $tempfn = ".revbank.$$";
my $lockfn = ".revbank.global-lock";
my $lockfh;
my $lockcount = 0;
sub get_lock() {
if (defined $lockfh) {
die "Fatal inconsistency" if $lockcount < 1;
return ++$lockcount;
}
die "Fatal inconsistency" if $lockcount;
open $lockfh, ">", $lockfn;
my $attempt = 1;
my $debug = !!$ENV{REVBANK_DEBUG};
FLOCK: {
if (flock $lockfh, LOCK_EX | LOCK_NB) {
syswrite $lockfh, $$;
return ++$lockcount;
}
if (($attempt % 50) == 0 or $debug) {
warn "Another revbank instance has the global lock. Waiting for it to finish...\n"
}
sleep .1;
$attempt++;
redo FLOCK;
}
croak "Could not acquire lock on $lockfn; file access failed";
}
sub release_lock() {
if (not defined $lockfh) {
die "Fatal inconsistency" if $lockcount;
return;
}
die "Fatal inconsistency" if $lockcount < 1;
if (--$lockcount == 0) {
flock $lockfh, LOCK_UN;
close $lockfh;
undef $lockfh;
}
}
sub release_all_locks() {
release_lock while $lockcount;
}
sub with_lock :prototype(&) ($code) {
get_lock;
my @rv;
my $rv;
my $list_context = wantarray;
eval {
@rv = $code->() if $list_context;
$rv = $code->() if not $list_context;
};
release_lock;
croak $@ =~ s/\.?\n$/, rethrown/r if $@;
return @rv if $list_context;
return $rv if not $list_context;
}
sub slurp($fn) {
return with_lock {
local $/ = wantarray ? "\n" : undef;
open my $fh, "<", $fn;
return readline $fh;
}
}
sub spurt($fn, @data) {
return with_lock {
open my $out, ">", $tempfn;
print $out @data;
close $out;
rename $tempfn, $fn;
};
}
sub append($fn, @data) {
return with_lock {
open my $out, ">>", $fn;
print $out @data;
close $out;
};
}
sub rewrite($fn, $sub) {
return with_lock {
open my $in, "<", $fn;
open my $out, ">", $tempfn;
while (defined(my $line = readline $in)) {
local $_ = $line;
my $new = $sub->($line);
print $out $new if defined $new;
}
close $out;
close $in;
rename $tempfn, $fn;
};
}
1;