| 1 | # ABSTRACT: Show and break task pick locks | ||
| 2 | |||
| 3 | package App::karr::Cmd::Unlock; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use MooX::Cmd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use MooX::Options ( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | usage_string => 'USAGE: karr unlock [ID[,ID,...]] [--all] [--json]', | |
| 9 | 2 | ); | |
| 10 | 0 | ▶ | use App::karr::Role::BoardAccess; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use App::karr::Role::Output; | ||
| 12 | use App::karr::Lock; | ||
| 13 | |||
| 14 | with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output', 'App::karr::Role::ClaimTimeout'; | ||
| 15 | |||
| 16 | |||
| 17 | option all => ( | ||
| 18 | is => 'ro', | ||
| 19 | doc => 'Break every lock on the board', | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub execute { | ||
| 23 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 24 | |||
| 25 | $self->check_positional_args($args_ref, 1); | ||
| 26 | |||
| 27 | # Pull first: a lock pushed by a command that died before it could release | ||
| 28 | # one is on the remote, and this is the command for exactly that mess. The | ||
| 29 | # guard is disarmed on the reporting path below, which writes nothing. | ||
| 30 | my $guard = $self->sync_before; | ||
| 31 | |||
| 32 | my $ec = $self->store->effective_config; | ||
| 33 | my $lock = App::karr::Lock->new( | ||
| 34 | git => $self->git, | ||
| 35 | ttl => $self->_parse_timeout($ec->{lock_timeout}, | ||
| 36 | App::karr::Lock->DEFAULT_TTL), | ||
| 37 | ); | ||
| 38 | |||
| 39 | my @pos = $self->positional_args($args_ref); | ||
| 40 | my @held = $lock->locks; | ||
| 41 | |||
| 42 | # No target: report only. Clearing a lock is destructive to whoever holds it, | ||
| 43 | # so it takes an explicit id or --all. | ||
| 44 | unless ($self->all || defined $pos[0]) { | ||
| 45 | $guard->done; | ||
| 46 | $self->_report(@held); | ||
| 47 | return; | ||
| 48 | } | ||
| 49 | |||
| 50 | # break_lock clears both addresses a lock can have -- the current one and the | ||
| 51 | # pre-#93 one inside refs/karr/* -- so a task holding one of each appears | ||
| 52 | # twice in @held but must only be broken, and reported, once. | ||
| 53 | my %seen; | ||
| 54 | my @ids = $self->all | ||
| 55 | ? grep { !$seen{$_}++ } map { $_->{task_id} } @held | ||
| 56 | : $self->parse_ids($pos[0]); | ||
| 57 | |||
| 58 | my @results; | ||
| 59 | for my $id (@ids) { | ||
| 60 | my ($ok, $owner) = $lock->break_lock($id); | ||
| 61 | push @results, { | ||
| 62 | id => 0 + $id, | ||
| 63 | broken => $ok ? \1 : \0, | ||
| 64 | ( $ok ? ( owner => $owner ) : () ), | ||
| 65 | }; | ||
| 66 | next if $self->json; | ||
| 67 | if ($ok) { printf "Broke lock on task %d (was held by %s)\n", $id, $owner } | ||
| 68 | else { printf "Task %d is not locked\n", $id } | ||
| 69 | } | ||
| 70 | |||
| 71 | $self->sync_after; | ||
| 72 | |||
| 73 | $self->print_json_results(@results); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _report { | ||
| 77 | my ($self, @held) = @_; | ||
| 78 | |||
| 79 | if ($self->json) { | ||
| 80 | $self->print_json([ map { { %$_, | ||
| 81 | expired => $_->{expired} ? \1 : \0, | ||
| 82 | legacy => $_->{legacy} ? \1 : \0, | ||
| 83 | } } @held ]); | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | unless (@held) { | ||
| 88 | print "No locks held.\n"; | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | for my $l (@held) { | ||
| 93 | printf "Task %-4d held by %s%s%s%s\n", | ||
| 94 | $l->{task_id}, | ||
| 95 | $l->{owner}, | ||
| 96 | ( defined $l->{age} ? sprintf( ' for %s', _duration( $l->{age} ) ) : '' ), | ||
| 97 | ( $l->{expired} ? ' [expired]' : '' ), | ||
| 98 | # A lock still sitting in the board namespace was written by a karr older | ||
| 99 | # than #93, or pulled from a remote that was given one. Nothing takes it | ||
| 100 | # into account any more, and breaking it is how it finally goes away. | ||
| 101 | ( $l->{legacy} ? ' [stray: pushed by an older karr, safe to break]' : '' ); | ||
| 102 | } | ||
| 103 | print "\nBreak one with 'karr unlock ID', or all of them with 'karr unlock --all'.\n"; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _duration { | ||
| 107 | my ($secs) = @_; | ||
| 108 | return "${secs}s" if $secs < 60; | ||
| 109 | return int( $secs / 60 ) . 'm' if $secs < 3600; | ||
| 110 | return int( $secs / 3600 ) . 'h'; | ||
| 111 | } | ||
| 112 | |||
| 113 | 1; | ||
| 114 | |||
| 115 | __END__ | ||