blib/lib/App/karr/Role/SyncLifecycle.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 169 CC 21 SCAR 37.1
CC
21
Cov
0%
_sync_attempts
110.0
sync_after
20.0
sync_before
12.0
CRAP 40.9
1 # ABSTRACT: Role providing sync lifecycle with retry and guard insurance
2  
3 package App::karr::Role::SyncLifecycle;
4 our $VERSION = '0.602';
5 use Moo::Role;
6 use MooX::Options;
7 # Loaded without importing, and every call below is qualified: a Moo::Role
8 # composes every sub in its package into its consumers, imported ones included,
9 # so an imported user_error would become a method on every syncing command
10 # (#38). App::karr::Role::Output and App::karr::Role::TaskMutation say the same.
11 use App::karr::Error ();
12 use App::karr::SyncGuard;
13  
14 option quiet => (
15 is => 'ro',
16 doc => 'Suppress sync progress and retry messages (errors are still shown)',
17 # Every syncing command carries --quiet. Per-command help suppresses it for
18 # the same reason --dir is hidden (ticket k276): the page should list what
19 # is interesting about THIS command, and --quiet is the same on all of them.
20 hidden => 1,
21 );
22  
23 option local_only => (
24 is => 'ro',
25 default => 0,
26 doc => 'Write to the local board refs only; skip the fetch and the push',
27 # Left visible where --quiet is hidden (k291). --quiet and --dir are hidden
28 # because they are the same uninteresting plumbing on every command; this is
29 # a per-invocation behaviour change -- it is exactly how you keep a `karr
30 # move`/`edit`/`handoff`/`create` from blocking on an unreachable remote --
31 # so the command's own help page is where an agent hitting a transport
32 # timeout should find it.
33 );
34  
35 # Holds the SyncGuard for the duration of a command so its DESTROY-insurance
36 # actually spans the command body. sync_before stashes it here; sync_after
37 # neutralises it after a successful push. Without this the guard returned by
38 # sync_before was discarded in void context and pushed prematurely (#28).
39 has _sync_guard => (
40 is => 'rw',
41 default => sub { undef },
42 );
43  
44  
45  
46 sub sync_pull {
47 my ( $self, %opt ) = @_;
48 my $git = $self->_sync_git;
49  
50 my ($ok) = $self->_sync_attempts( 'Pull', $git,
51 sub { $git->pull( undef, %opt ) } );
52  
53 # The git error is not repeated here. It was printed above the moment it
54 # happened, and embedding a copy in the terminal message meant one failed
55 # sync showed the same multi-line git output twice -- under --quiet too,
56 # which silences the retry banners and nothing else (#27, #77). This half
57 # now matches sync_after, which has always ended on the verdict alone.
58 App::karr::Error::user_error(
59 "Pull failed after 3 attempts. Nothing was changed.\n"
60 . "Run 'karr sync' to retry." ) unless $ok;
61  
62 return 1;
63 }
64  
65 # The git handle every method here works through. The duck-typed doubles the
66 # sync tests drive this role with expose it directly; the commands get it from
67 # App::karr::Role::BoardDiscovery.
68 sub _sync_git {
69 my ($self) = @_;
70 return $self->can('git') ? $self->git : $self->store->git;
71 }
72  
73 # The retry loop -- the only one. Three attempts, the first silent, the retries
74 # announced from the second, errors always on STDERR and --quiet silencing the
75 # announcements but never an error.
76 #
77 # $label is 'Pull' or 'Push' and appears in both the banner and the git error
78 # line, so the wording every existing test pins is produced here and nowhere
79 # else. Returns ( $ok, $rejected ); $rejected is only ever true when
80 # check_rejection was asked for.
81 sub _sync_attempts {
82 my ( $self, $label, $git, $attempt, %opt ) = @_;
83  
84 my $ok = 0;
85 my $err = '';
86 my $shown = '';
87 my $rejected = 0;
88 for my $try ( 1 .. 3 ) {
89 # Retry-only: attempt 1 is silent; only announce the actual retries.
90 print STDERR "$label retry $try of 3...\n"
91 if $try > 1 && !$self->quiet;
92 $ok = $attempt->();
93 if ($ok) {
94 print STDERR "$label succeeded.\n" if $try > 1 && !$self->quiet;
95 last;
96 }
97 # Errors always reach STDERR, even under --quiet (#27). But the same
98 # error once per attempt is not three pieces of information, and
99 # last_error is multi-line now that a rejection lists a reason per ref
100 # (#84) -- three copies of that buries the one thing worth reading. A
101 # repeat of what was just printed is dropped; a *different* error still
102 # gets its own line.
103 $err = 'git ' . lc($label) . ' failed: '
104 . ( $git->last_error // 'unknown error' );
105 print STDERR " $err\n" if $err ne $shown;
106 $shown = $err;
107  
108 if ( $opt{check_rejection} ) {
109 # A per-ref rejection is final (#84): the remote was reached and
110 # said no, so two more attempts would only collect the same
111 # refusal twice more, at a second each, on every writing command.
112 # The `can` is for the duck-typed git objects the sync tests drive
113 # this role with.
114 #
115 # Except when the answer was "another push got to this ref first",
116 # which is contention and not a refusal: the same refspec lands on
117 # the next attempt. That is what the retry loop is for, and
118 # skipping it turned a `karr create` that had already written its
119 # card into a failed command on every parallel run (#181). Only a
120 # push whose rejections are *all* contention is retried, so one
121 # protected ref still ends it the way #84 requires.
122 $rejected = $git->can('push_rejections')
123 && @{ $git->push_rejections } ? 1 : 0;
124 $rejected = 0
125 if $rejected && $git->can('push_contention') && $git->push_contention;
126 last if $rejected;
127 }
128  
129 sleep 1 if $try < 3;
130 }
131 return ( $ok, $rejected );
132 }
133  
134  
135 sub sync_before {
136 my ( $self, %opt ) = @_;
137  
138 # --local-only writes to the board's local refs and never reaches the
139 # remote, so the fetch is skipped. The command still writes, so a guard is
140 # created for the callers that capture one (Backup/Repair/Unlock call ->done
141 # on it), but it is spent immediately: with the push skipped in sync_after,
142 # neither the guard's DESTROY nor bin/karr's END-block flush may turn that
143 # skip into a push at process teardown.
144 $self->sync_pull(%opt) unless $self->local_only;
145  
146 # Stash the guard on the object so it outlives sync_before's return and
147 # covers the whole command body; sync_after neutralises it on success.
148 my $git = $self->_sync_git;
149 my $guard = App::karr::SyncGuard->new( git => $git, quiet => $self->quiet );
150 $guard->done if $self->local_only;
151 $self->_sync_guard($guard);
152 return $guard;
153 }
154  
155  
156 sub sync_after {
157 my ($self) = @_;
158  
159 # --local-only skipped the fetch and spent the guard in sync_before; skip
160 # the push to match, so nothing here reaches the remote. The refs are on the
161 # local board, and `karr sync` publishes them once the remote is reachable
162 # again.
163 return if $self->local_only;
164  
165 my $git = $self->_sync_git;
166  
167 my ( $ok, $rejected ) = $self->_sync_attempts( 'Push', $git,
168 sub { $git->push }, check_rejection => 1 );
169  
170 # Neutralise the insurance guard on both outcomes.
171 #
172 # On success: so its DESTROY does not fire a second, redundant push once
173 # the command body returns (#28).
174 #
175 # On failure: the three attempts the insurance would make have just been
176 # made and the error below carries the same "run karr sync" guidance, so
177 # leaving the guard armed would only make the END flush in bin/karr (#37)
178 # repeat the identical failing push, doubling both the delay and the noise
179 # on an already-failing command. On a per-ref rejection the attempts were
180 # not spent, but the answer was given (#84), so it holds there too.
181 $self->_release_guard;
182  
183 return if $ok;
184  
185 App::karr::Error::user_error(
186 "Push rejected by the remote. Local refs are intact.\n"
187 . "The refs above were refused, not lost in transit, so pushing again "
188 . "would only be refused again." ) if $rejected;
189  
190 App::karr::Error::user_error(
191 "Push failed after 3 attempts. Local refs are intact.\n"
192 . "Run 'karr sync' to retry." );
193 }
194  
195  
196 sub sync_pull_foundation {
197 my ($self) = @_;
198 my $git = $self->_sync_git;
199  
200 my ($ok) = $self->_sync_attempts( 'Pull', $git,
201 sub { $git->pull_foundation } );
202  
203 App::karr::Error::user_error(
204 "Pull of refs/karr-foundation/ failed after 3 attempts.\n"
205 . "The board is synced; the fleet namespace is not.\n"
206 . "Run 'karr sync' to retry." ) unless $ok;
207  
208 return 1;
209 }
210  
211  
212 sub sync_push_foundation {
213 my ($self) = @_;
214 my $git = $self->_sync_git;
215  
216 my ( $ok, $rejected ) = $self->_sync_attempts( 'Push', $git,
217 sub { $git->push_foundation }, check_rejection => 1 );
218 return if $ok;
219  
220 App::karr::Error::user_error(
221 "Push of refs/karr-foundation/ was rejected by the remote. Local refs "
222 . "are intact.\n"
223 . "The refs above were refused, not lost in transit, so pushing again "
224 . "would only be refused again." ) if $rejected;
225  
226 App::karr::Error::user_error(
227 "Push of refs/karr-foundation/ failed after 3 attempts. Local refs "
228 . "are intact.\n"
229 . "Run 'karr sync' to retry." );
230 }
231  
232 sub _release_guard {
233 my ($self) = @_;
234 if ( my $guard = $self->_sync_guard ) {
235 $guard->done;
236 $self->_sync_guard(undef);
237 }
238 return;
239 }
240  
241 1;
242  
243 __END__