lib/App/karr/SyncGuard.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/App/karr/CrossBoard.pm ↑ Summary blib/lib/App/karr.pm »
1 # ABSTRACT: Push guard with automatic retry on scope exit
2  
3 package App::karr::SyncGuard;
4 our $VERSION = '0.602';
5 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Scalar::Util qw( refaddr weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use App::karr::Git;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11  
12 # Process-wide registry of armed guards, keyed by refaddr with weakened values
13 # so a guard that is released normally is not kept alive here. done() and
14 # DESTROY deregister, flush_armed() drains. In practice a karr process holds at
15 # most one guard; this is a registry rather than a single slot only because
16 # nothing guarantees that, and a stale single slot would push the wrong guard.
17 our %ARMED;
18  
19 sub BUILD {
20 my ($self) = @_;
21 my $key = refaddr $self;
22 $ARMED{$key} = $self;
23 weaken $ARMED{$key};
24 return;
25 }
26  
27 has git => (
28 is => 'ro',
29 required => 1,
30 );
31  
32  
33 has _done => (
34 is => 'rw',
35 default => 0,
36 );
37  
38 has quiet => (
39 is => 'ro',
40 default => 0,
41 );
42  
43  
44 has _errors => (
45 is => 'ro',
46 default => sub { [] },
47 );
48  
49  
50 sub done {
51 my ($self) = @_;
52 $self->{_done} = 1;
53 delete $ARMED{ refaddr $self };
54 return;
55 }
56  
57  
58 sub flush_armed {
59 my ($class) = @_;
60  
61 # Drain first: whatever happens below, these guards have been dealt with,
62 # and a guard freed mid-loop must not leave a dangling registry key.
63 my @guards = grep { defined } values %ARMED;
64 %ARMED = ();
65  
66 return 0 unless $App::karr::Git::WRITES;
67  
68 my $flushed = 0;
69 for my $guard (@guards) {
70 next if $guard->{_done};
71 $flushed++;
72 eval { $guard->_insurance_push; 1 }
73 or warn 'Push insurance failed: ' . ( $@ || 'unknown error' );
74 # Spent either way: on failure _insurance_push has already warned with
75 # the "run karr sync" guidance, so DESTROY must not report it again.
76 $guard->{_done} = 1;
77 }
78 return $flushed;
79 }
80  
81  
82 sub errs {
83 my ($self) = @_;
84 return @{$self->{_errors}};
85 }
86  
87 sub DESTROY {
88 my ($self) = @_;
89  
90 # A guard only reaped during global destruction cannot push. By then Perl
91 # is destroying blessed objects in no defined order, and App::karr::Git's
92 # libgit2/FFI layer is explicitly not re-entrant in that phase -- attempting
93 # it recursed until the machine was out of memory (#34). Report instead of
94 # pushing; the refs are still on disk.
95 #
96 # Reaching this at all means nobody called flush_armed (#37) -- the CLI
97 # does, from an END block, so this is the embedder's last resort.
98 #
99 # Nothing here may touch $self->{git}: it is a blessed object and may
100 # already have been reaped, which is exactly what made this branch
101 # nondeterministic before. $App::karr::Git::WRITES is a plain package
102 # scalar, still readable throughout this phase, so "the body died before
103 # writing anything" is decided on real state rather than on teardown order.
104 # %ARMED is likewise left alone here: deregistering from a hash the same
105 # teardown is freeing buys nothing when the process is already ending.
106 if ( ${^GLOBAL_PHASE} eq 'DESTRUCT' ) {
107 return if $self->{_done};
108 return unless $App::karr::Git::WRITES;
109 warn "Push skipped: karr exited before the board was pushed.\n"
110 . "Local refs are intact. Run 'karr sync' to push them.\n";
111 return;
112 }
113  
114 delete $ARMED{ refaddr $self };
115 return if $self->{_done};
116 $self->_insurance_push;
117 return;
118 }
119  
120 # The insurance push itself: up to 3 attempts, retry-only output (#27), never
121 # fatal. Shared by DESTROY and flush_armed so the die path behaves identically
122 # whether the guard is released by scope exit or drained at process teardown.
123 sub _insurance_push {
124 my ($self) = @_;
125  
126 my $git = $self->git;
127 my $ok = 0;
128 my $err = '';
129  
130 for my $attempt ( 1 .. 3 ) {
131 # Retry-only (#27): the first attempt is silent; only announce retries,
132 # and honour --quiet. Errors and the final guidance are always shown.
133 print STDERR "Push retry $attempt of 3...\n"
134 if $attempt > 1 && !$self->{quiet};
135 $ok = $git->push;
136 if ($ok) {
137 $self->{_done} = 1;
138 return 1;
139 }
140 # Native Git::Native/libgit2 ops set no shell exit code; the failure
141 # detail lives in $git->last_error (see App::karr::Git).
142 $err = "git push failed: " . ( $git->last_error // 'unknown error' );
143 push @{$self->{_errors}}, $err;
144 print STDERR " $err\n";
145  
146 # A push the far side refused ref by ref is not a lost connection: the
147 # remote was reached and gave its answer, so the other two attempts
148 # would collect the same refusal twice more, a second apart, on a
149 # command that has already died. App::karr::Role::SyncLifecycle stops
150 # on the same signal (#84); this path is the one that runs *after* a
151 # command failed, so reporting the least here was backwards (#96).
152 #
153 # Contention is not that answer: two pushes raced for the same ref and
154 # the next attempt of the same refspec lands (#181). The insurance push
155 # is the last chance the board has to reach the remote, so it is the
156 # one place that must not mistake a race for a refusal.
157 #
158 # The `can` is for the duck-typed git objects the sync tests drive this
159 # class with, and matches how SyncLifecycle asks the same question.
160 last
161 if $git->can('push_rejections')
162 && @{ $git->push_rejections }
163 && !( $git->can('push_contention') && $git->push_contention );
164  
165 sleep 1 if $attempt < 3;
166 }
167  
168 # Never die() here: DESTROY typically runs while another exception unwinds
169 # the stack, where a die is turned into a swallowed "(in cleanup)" warning
170 # (or lost entirely during global destruction), masking this message. Warn
171 # so the guidance always reaches STDERR.
172 #
173 # Which guidance depends on what failed. "Run 'karr sync' to retry" is only
174 # true when retrying can work; after a refusal it sends the user to a
175 # command that will be refused identically, and says nothing about the one
176 # thing they need to know -- that the remote, not the network, said no. The
177 # per-ref reasons are in the error printed just above.
178 warn $self->_rejected
179 ? "Push rejected by the remote. Local refs are intact.\n"
180 . "The refs above were refused, not lost in transit, so pushing again "
181 . "would only be refused again.\n"
182 : "Push failed after 3 attempts. Local refs are intact.\n"
183 . "Run 'karr sync' to retry.\n"
184 . "Errors: " . join( ', ', $self->errs ) . "\n";
185 return 0;
186 }
187  
188 # Whether the last push failed because the remote refused refs, rather than
189 # because it could not be reached. Contention does not count: a race the three
190 # attempts could not win is an ordinary failure to push, and the guidance for
191 # it is "run karr sync", not "the remote will refuse this again" (#181).
192 sub _rejected {
193 my ($self) = @_;
194 my $git = $self->git;
195 return 0 unless $git && $git->can('push_rejections');
196 my $rejected = $git->push_rejections;
197 return 0 unless $rejected && @$rejected;
198 return 0 if $git->can('push_contention') && $git->push_contention;
199 return 1;
200 }
201  
202 1;
203  
204 __END__
« blib/lib/App/karr/CrossBoard.pm ↑ Summary blib/lib/App/karr.pm »