srvzio/
service.rs

1//! The key module of this library: a `Service`
2
3/// A `Service` is a _black box_ that does work: it can be started and it can be stopped.
4///
5/// This trait abstracts away the actions that can be done from the outside to a `Service`.
6/// It's up to the specific implementor to make sense of what starting/stopping means.
7///
8/// Note that every method in this trait is by default implemented as a no-op: this leaves to the
9/// actual implementor to decide what is fitting to implement, and what is not.
10pub trait Service {
11
12  /// Service name
13  fn name(&self) -> &'static str;
14
15  /// Starts the service
16  fn start(&mut self);
17
18  /// Awaits that the service is done starting.
19  ///
20  /// Implement to provide sensible logic to wait for a service to be fully started.
21  ///
22  /// This is usually used _after_ a call to `start()`.
23  fn await_started(&mut self) {
24    // By default, nothing to do
25  }
26
27  /// Starts the service and waits for it to be done starting.
28  ///
29  /// A _graceful_ start.
30  fn start_and_await(&mut self) {
31    self.start();
32    self.await_started();
33  }
34
35  /// Stops the service
36  fn stop(&mut self);
37
38  /// Awaits that the service is done stopping.
39  ///
40  /// Implement to provide sensible logic to wait for a service to be fully stopped.
41  ///
42  /// This is usually used _after_ a call to `stop()`.
43  fn await_stopped(&mut self) {
44    // By default, nothing to do
45  }
46
47  /// Stops the service and waits for it to be done stopping.
48  ///
49  /// A _graceful_ stop.
50  fn stop_and_await(&mut self) {
51    self.stop();
52    self.await_stopped();
53  }
54
55}