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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use DerefMut;
use crateSettings;
use crateShutdown;
use crateError;
/* ---------- */
/// A worker represents a thread that runs for the lifetime of the program (or at least for a very long time).
///
/// Types that implement the [`Worker`] trait are called `workers`. Workers are independent of anything to do their job,
/// even of the runtime where they were launched, the data to work on being provided by the type itself.
///
/// Workers are defined by one main method, [`Worker::run`], which run the actual infinite loop. This method
/// has a default implementation that first calls the [`Worker::on_start`] method once at the beginning, then calls
/// [`Worker::on_update`] in the loop until it returns [`ControlFlow::Break`] or the runtime is stopped.
///
/// The last two methods exist as a convenience for simple workers that do not need specific instructions or
/// setup to be done before running their infinite loop. Default implementations are provided:
/// * [`Worker::on_start`] does nothing and returns immediately.
/// * [`Worker::on_update`] does nothing and returns [`ControlFlow::Break`] immediately.
///
/// # Examples
///
/// A dead simple worker that prints "Hello, World!" each 100ms for one second:
///
/// ```
/// # use employees::{Runtime, Worker, ControlFlow};
/// # use std::time::Duration;
/// struct WorkerThatPrints;
/// impl Worker for WorkerThatPrints {
/// fn on_update(&mut self) -> ControlFlow {
/// println!("Hello, World!");
/// std::thread::sleep(Duration::from_millis(100));
/// ControlFlow::Continue
/// }
/// }
///
/// let mut runtime = Runtime::new();
///
/// runtime.launch(WorkerThatPrints);
/// std::thread::sleep(Duration::from_secs(1));
/// ```
///
/// A worker that counts the number of time its update method has beed called and print that amount once dropped:
///
/// ```
/// # use employees::*;
/// # use std::time::Duration;
/// #[derive(Debug, Default)]
/// struct Counter {
/// count: usize
/// }
///
/// impl Worker for Counter {
/// fn on_update(&mut self) -> ControlFlow {
/// self.count += 1;
///
/// // Some heavy work...
/// # std::thread::sleep(Duration::from_millis(50));
///
/// ControlFlow::Continue
/// }
/// }
///
/// impl Drop for Counter {
/// fn drop(&mut self) {
/// println!("num updates: {}", self.count);
/// }
/// }
///
/// let mut runtime = Runtime::new();
/// runtime.launch(Counter::default());
/// # std::thread::sleep(Duration::from_secs(1));
/// ```
/* ---------- */
/// Allow building a worker before actually launching it with the [`Runtime::launch_from_context`] function.
///
/// This trait exists as way to build and launch workers in a builder pattern way. For example, users can
/// can launch workers configured via a configuration file or command-line arguments.
///
/// [`Runtime::launch_from_context`]: crate::Runtime::launch_from_context
///
/// # Examples
///
/// ```
/// # use employees::*;
/// // Let's define a counter...
/// struct Counter {
/// counter: usize,
/// max: usize,
/// }
///
/// impl Counter {
/// fn new(max: usize) -> Self {
/// Self {
/// counter: 0,
/// max,
/// }
/// }
/// }
///
/// impl Worker for Counter {
/// fn on_update(&mut self) -> ControlFlow {
/// self.counter += 1;
/// if self.counter > self.max {
/// return ControlFlow::Break;
/// }
///
/// ControlFlow::Continue
/// }
/// }
///
/// // ... and the context to build it from.
/// struct CounterContext {
/// max: usize
/// }
///
/// impl CounterContext {
/// fn new(max: usize) -> Self {
/// Self { max }
/// }
/// }
///
/// impl Context for CounterContext {
/// type Target = Counter;
///
/// fn into_worker(self) -> Result<Self::Target, Error> {
/// Ok(Counter::new(self.max))
/// }
/// }
///
/// let mut runtime = Runtime::new();
/// let context = CounterContext::new(10);
///
/// runtime.launch_from_context(context);
/// ```
/* ---------- */
/// This trait is very similar to the [`Context`] but is needed for workers to be respawned.
///
/// Respawnable workers are launch with the [`Runtime::launch_respawnable`] function and
/// are respawned if they panicked with the [`Runtime::health_check`] one. See their
/// respective documentation for more details.
///
/// [`Runtime::launch_respawnable`]: crate::Runtime::launch_respawnable
/// [`Runtime::health_check`]: crate::Runtime::health_check
///
/// # Examples
///
/// ```
/// # use employees::*;
/// # use std::time::Duration;
/// // A worker that panic some time after being spawned...
/// struct PanickingWorker;
/// impl Worker for PanickingWorker {
/// fn on_update(&mut self) -> ControlFlow {
/// std::thread::sleep(Duration::from_secs(1));
/// panic!("panicking!")
/// }
/// }
///
/// // ... and its context.
/// struct WorkerContext;
/// impl RespawnableContext<'_> for WorkerContext {
/// fn boxed_worker(&self) -> Result<Box<dyn Worker>, Error> {
/// Ok(Box::new(PanickingWorker))
/// }
/// }
///
/// let mut runtime = Runtime::new();
/// runtime.launch_respawnable(WorkerContext);
///
/// std::thread::sleep(Duration::from_secs(1));
/// runtime.health_check();
/// # std::thread::sleep(Duration::from_millis(500));;
/// ```
/* ---------- */
/// Defines the control flow of [`Workers`].
///
/// [`Workers`]: crate::Worker
///
/// # Examples
///
/// A worker that counts to 10 and stops.
///
/// ```
/// # use employees::*;
/// #[derive(Debug, Default)]
/// struct Counter {
/// count: usize
/// }
///
/// impl Worker for Counter {
/// fn on_update(&mut self) -> ControlFlow {
/// self.count += 1;
/// println!("counter: {}", self.count);
///
///
/// // We're done counting, let's leave the loop.
/// if self.count > 10 {
/// return ControlFlow::Break;
/// }
///
/// // The loop continues.
/// ControlFlow::Continue
/// }
/// }
///
/// let mut runtime = Runtime::new();
/// runtime.launch(Counter::default());
/// ```
/* ---------- */