-
Notifications
You must be signed in to change notification settings - Fork 1
/
proc_container_state.H
258 lines (187 loc) · 5.88 KB
/
proc_container_state.H
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
** Copyright 2022 Double Precision, Inc.
** See COPYING for distribution information.
*/
#ifndef proc_container_state_h
#define proc_container_state_h
#include "proc_container_statefwd.H"
#include <string>
#include <variant>
#include <type_traits>
#include <functional>
#include <string>
#include "proc_container_runnerfwd.H"
#include "proc_container_timerfwd.H"
#include "external_filedesc.H"
#include "log.H"
#include <list>
//! A process container that's not running
struct state_stopped {
//! Human readable label.
const STATE_LABEL &get_label() const;
//! There are no timers here.
const proc_container_timer *timer() const { return nullptr; }
};
//! A process container state that's CCing the output from the container
struct state_requester_stdout {
//! The requester's stdout pipe
external_filedesc requester_stdout;
};
/*! A visitor that retrieves the container's requester_stdout
Returns a file descriptor to a pipe to the standard output of the command
that started/stopped/restarted/reloaded a container.
If the container's state is derived from state_requester_stdout, extract it.
*/
struct get_requester_stdout {
external_filedesc requester_stdout;
template<typename T> void operator()(T &t)
{
if constexpr (std::is_base_of_v<state_requester_stdout, T>) {
this->requester_stdout=t.requester_stdout;
}
}
};
//! A process container that's starting
struct state_starting : state_requester_stdout {
//! Starting this container because it is a dependency.
bool dependency;
//! The connections that requested this container to get started.
std::list<external_filedesc> requesters;
state_starting(
bool dependency,
external_filedesc requester,
external_filedesc requester_stdout
) : state_requester_stdout{
std::move(requester_stdout)
}, dependency{dependency}
{
if (requester)
{
requesters.push_back(std::move(requester));
}
}
//! Running start process.
proc_container_runner starting_runner;
//! Running start process's timeout
proc_container_timer starting_runner_timeout;
//! Received is_populated=false before the runner stopped
bool delayed_depopulation{false};
//! Human readable label.
const STATE_LABEL &get_label() const;
//! Return the starting_runner_timeout
const proc_container_timer *timer() const
{
return &starting_runner_timeout;
}
};
//! A process container that's started
struct state_started : state_requester_stdout {
bool dependency;
//! When this container started.
time_t start_time=log_current_timespec().tv_sec;
state_started(bool dependency) : dependency{dependency} {}
//! Human readable label.
const STATE_LABEL &get_label() const;
//! Currently running reload or restart command
proc_container_runner reload_or_restart_runner;
//! Respawnable runner
proc_container_runner respawn_runner;
/*!
When the respawnable runner was first started
This information is not preserved when the daemon gets re-execed.
This is not critical.
*/
time_t respawn_starting_time=start_time;
/*!
How many times the respawnable runner was restarted
This information is not preserved when the daemon gets re-execed.
This is not critical.
*/
size_t respawn_counter=0;
/*! Respawn cleanup timer
Before we respawn we send a SIGTERM to anything that's left in the
container, and wait to send a SIGKILL to kill off anything that's
stubborn.
*/
proc_container_timer respawn_prepare_timer;
//! Whether the respawnable runner succeeded
bool respawn_succeeded=false;
//! Return the respawn_prepare_timer
const proc_container_timer *timer() const
{
return &respawn_prepare_timer;
}
};
//! A \ref state_stopping "state_stopping container" that's waiting to be able to begin stopping
struct stop_pending {
//! Human readable label.
const STATE_LABEL &get_label() const;
const proc_container_timer *timer() const { return nullptr; }
};
//! A \ref state_stopping "state_stopping container" that's running its stopping process
struct stop_running {
//! Running stop process
proc_container_runner stopping_runner;
//! Running stop process timeout
proc_container_timer stopping_runner_timeout;
//! Human readable label.
const STATE_LABEL &get_label() const;
//! Return the stopping_runner_timeout
const proc_container_timer *timer() const
{
return &stopping_runner_timeout;
}
};
//! A \ref state_stopping "state_stopping container" that's removing its processes
struct stop_removing {
//! Timer after SIGTERM gets sent.
//! SIGKILL gets sent when it expires.
proc_container_timer sigkill_timer;
//! Whether SIGKILL has been sent.
bool sigkill_sent;
//! Human readable label.
const STATE_LABEL &get_label() const;
//! Return the sigkill_timer.
const proc_container_timer *timer() const { return &sigkill_timer; }
};
//! The phase of a \ref state_stopping "state_stopping container".
//! stop_pending - waiting for other containers that depend on this container
//! to stop.
//!
//! proc_container_runner - the running stopping process.
//!
//! stop_removing - removing processes in the container.
typedef std::variant<stop_pending, stop_running,
stop_removing> stopping_phase;
//! A process container that's stopping
struct state_stopping : state_requester_stdout {
//! Its current phase
stopping_phase phase;
//! The connections that requested this container to get stopped.
std::list<external_filedesc> requesters;
//! Non default, non-copy constructor forwards to phase's constructor.
template<typename Arg, typename ...Args,
typename=std::enable_if_t<!std::is_same_v<std::remove_cvref_t<
Arg>,
state_stopping>>>
state_stopping(Arg &&arg, Args && ...args)
: phase{
std::forward<Arg>(arg),
std::forward<Args>(args)...
}
{
}
//! Human readable label.
const STATE_LABEL &get_label() const;
//! No timers here
const proc_container_timer *timer() const
{
return std::visit(
[]
(auto &phase)
{
return phase.timer();
}, phase);
}
};
#endif