-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsul-lock
More file actions
executable file
·205 lines (158 loc) · 5.01 KB
/
consul-lock
File metadata and controls
executable file
·205 lines (158 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env perl
# consul-lock - run a program under lock
# Copyright (c) 2015 Robert Norris. MIT license.
use 5.014;
use warnings;
use strict;
use AnyEvent;
use AnyEvent::Consul 0.005;
use AnyEvent::Subprocess;
use AnyEvent::Handle;
my ($lockpath, @exec) = @ARGV;
unless ($lockpath && @exec) {
say "usage: $0 <lock-path> <program> [args...]";
exit 1;
}
sub debug (@) {
state $debug = !!$ENV{CONSUL_LOCK_DEBUG};
return unless $debug;
say STDERR "CONSUL_LOCK: @_";
}
my $cv = AnyEvent->condvar;
my $c = AnyEvent::Consul->new;
my ($refresh_guard, $sleep_guard, $kill_guard, %signal_guard);
my $run;
sub error_handler {
my ($msg) = @_;
say STDERR "consul-lock: error during API call, restarting: $msg";
start_session();
}
sub begin_acquire {
my ($sid) = @_;
debug "acquiring lock";
$c->kv->put($lockpath, 1, wait => "10s", acquire => $sid, error_cb => \&error_handler, cb => sub {
my ($acquired) = @_;
unless ($acquired) {
$sleep_guard = AnyEvent->timer(after => "5s", cb => sub {
debug "couldn't acquire lock, sleeping";
undef $sleep_guard;
return begin_acquire($sid);
});
return;
}
debug "lock acquired, confirming we really have it";
$c->kv->get($lockpath, error_cb => \&error_handler, cb => sub {
my ($kv, $meta) = @_;
if (!$kv || $sid ne ($kv->session // '')) {
debug "lost lock before exec, retrying";
return begin_acquire($sid);
}
debug "we have the lock, setting up subprocess";
my $job = AnyEvent::Subprocess->new(
delegates => ['StandardHandles'],
on_completion => sub {
my ($done) = @_;
debug "subprocess done, releasing lock";
undef $refresh_guard;
%signal_guard = ();
$c->kv->put($lockpath, 1, release => $sid, error_cb => \&error_handler, cb => sub {
debug "destroying session";
$c->session->destroy($sid, error_cb => \&error_handler, cb => sub {
debug "all done, raising condvar";
$cv->send($done->exit_value);
});
});
},
code => sub {
debug "subprocess $$ started, executing: @exec";
exec @exec or debug "exec fail, aborting";
require POSIX;
POSIX::_exit(1);
},
);
$run = $job->run;
$run->delegate('stdout')->handle->on_read(sub {
my ($handle) = @_;
print substr $handle->{rbuf}, 0, length($handle->{rbuf}), '';
});
$run->delegate('stderr')->handle->on_read(sub {
my ($handle) = @_;
print STDERR substr $handle->{rbuf}, 0, length($handle->{rbuf}), '';
});
my $stdin_handle; $stdin_handle = AnyEvent::Handle->new(
fh => \*STDIN,
on_read => sub {
$run->delegate('stdin')->handle->push_write(substr $stdin_handle->{rbuf}, 0, length($stdin_handle->{rbuf}), '');
},
on_eof => sub {
$run->delegate('stdin')->handle->close_fh;
},
);
for my $signal (qw(INT TERM HUP)) {
$signal_guard{$signal} = AnyEvent->signal(signal => $signal, error_cb => \&error_handler, cb => sub {
if ($kill_guard) {
debug "caught SIG$signal during subprocess shutdown, ignoring";
return;
}
debug "caught SIG$signal";
send_signal($signal);
});
}
begin_lockwatch($sid, $meta->index);
});
});
}
sub begin_lockwatch {
my ($sid, $index) = @_;
debug "setting up lock watcher, sid $sid, index $index";
$c->kv->get($lockpath, index => $index, wait => "30s",
error_cb => \&error_handler,
cb => sub {
my ($kv, $meta) = @_;
if ($index == $meta->index) {
debug "lock key is unchanged";
return begin_lockwatch($sid, $index);
}
if ($kv && $sid eq ($kv->session // '')) {
debug "lock key changed, but our session still holds it";
return begin_lockwatch($sid, $meta->index);
}
debug "lost lock, terminating subprocess";
if ($kill_guard) {
debug "not terminating during signal shutdown";
return;
}
send_signal('TERM');
},
error_cb => sub {
my ($msg) = @_;
debug "lock watcher error: $msg";
return begin_lockwatch($sid, $index);
},
);
}
sub send_signal {
my ($signal) = @_;
debug "sending SIG$signal to subprocess";
$run->kill($signal);
$kill_guard = AnyEvent->timer(after => "5s", cb => sub {
debug "subprocess didn't die, sending SIGKILL";
$run->kill;
});
}
sub start_session {
undef $refresh_guard;
$c->session->create(Consul::Session->new(name => $lockpath, ttl => "10s"), error_cb => \&error_handler, cb => sub {
my ($sid) = @_;
debug "created session, sid $sid";
$refresh_guard = AnyEvent->timer(after => "5s", interval => "5s", cb => sub {
debug "renewing session";
$c->session->renew($sid, error_cb => \&error_handler);
});
begin_acquire($sid);
});
}
start_session();
my $rc = $cv->recv;
debug "exiting with rc $rc";
exit $rc;