1
#pragma once
2

            
3
#include <functional>
4

            
5
#include "envoy/common/pure.h"
6
#include "envoy/event/dispatcher.h"
7

            
8
namespace Envoy {
9
namespace Server {
10

            
11
class ServerLifecycleNotifier {
12
public:
13
51049
  virtual ~ServerLifecycleNotifier() = default;
14

            
15
  /**
16
   * Stages of the envoy server instance lifecycle.
17
   */
18
  enum class Stage {
19
    /**
20
     * The server instance main thread is about to enter the dispatcher loop.
21
     */
22
    Startup,
23

            
24
    /**
25
     * The server instance init manager has finished initialization.
26
     */
27
    PostInit,
28

            
29
    /**
30
     * The server instance is being shutdown and the dispatcher is about to exit.
31
     * This provides listeners a last chance to run a callback on the main dispatcher.
32
     * Note: the server will wait for callbacks that registered to take a completion
33
     * before exiting the dispatcher loop.
34
     * Note: callbacks that registered with a completion will only be notified for this
35
     * stage if the server did not prematurely shutdown before fully starting up (specifically
36
     * if the server shutdown before worker threads were started).
37
     */
38
    ShutdownExit
39
  };
40

            
41
  // A handle to a callback registration. Deleting this handle will unregister the callback.
42
  class Handle {
43
  public:
44
51
    virtual ~Handle() = default;
45
  };
46
  using HandlePtr = std::unique_ptr<Handle>;
47

            
48
  /**
49
   * Callback invoked when the server reaches a certain lifecycle stage.
50
   *
51
   * Instances of the second type which take an Event::PostCb parameter must post
52
   * that callback to the main dispatcher when they have finished processing of
53
   * the new lifecycle state. This is useful when the main dispatcher needs to
54
   * wait for registered callbacks to finish their work before continuing, e.g.,
55
   * during server shutdown.
56
   */
57
  using StageCallback = std::function<void()>;
58
  using StageCallbackWithCompletion = std::function<void(Event::PostCb)>;
59

            
60
  /**
61
   * Register a callback function that will be invoked on the main thread when
62
   * the specified stage is reached.
63
   *
64
   * The second version which takes a completion back is currently only supported
65
   * for the ShutdownExit stage.
66
   */
67
  virtual HandlePtr registerCallback(Stage stage, StageCallback callback) PURE;
68
  virtual HandlePtr registerCallback(Stage stage, StageCallbackWithCompletion callback) PURE;
69
};
70

            
71
} // namespace Server
72
} // namespace Envoy