|
4 | 4 | #include "fsm-health.h"
|
5 | 5 | #include "fsmonitor--daemon.h"
|
6 | 6 |
|
| 7 | +/* |
| 8 | + * Every minute wake up and test our health. |
| 9 | + */ |
| 10 | +#define WAIT_FREQ_MS (60 * 1000) |
| 11 | + |
| 12 | +/* |
| 13 | + * State machine states for each of the interval functions |
| 14 | + * used for polling our health. |
| 15 | + */ |
| 16 | +enum interval_fn_ctx { |
| 17 | + CTX_INIT = 0, |
| 18 | + CTX_TERM, |
| 19 | + CTX_TIMER |
| 20 | +}; |
| 21 | + |
| 22 | +typedef int (interval_fn)(struct fsmonitor_daemon_state *state, |
| 23 | + enum interval_fn_ctx ctx); |
| 24 | + |
7 | 25 | struct fsm_health_data
|
8 | 26 | {
|
9 | 27 | HANDLE hEventShutdown;
|
@@ -42,27 +60,72 @@ void fsm_health__dtor(struct fsmonitor_daemon_state *state)
|
42 | 60 | FREE_AND_NULL(state->health_data);
|
43 | 61 | }
|
44 | 62 |
|
| 63 | +/* |
| 64 | + * A table of the polling functions. |
| 65 | + */ |
| 66 | +static interval_fn *table[] = { |
| 67 | + NULL, /* must be last */ |
| 68 | +}; |
| 69 | + |
| 70 | +/* |
| 71 | + * Call all of the polling functions in the table. |
| 72 | + * Shortcut and return first error. |
| 73 | + * |
| 74 | + * Return 0 if all succeeded. |
| 75 | + */ |
| 76 | +static int call_all(struct fsmonitor_daemon_state *state, |
| 77 | + enum interval_fn_ctx ctx) |
| 78 | +{ |
| 79 | + int k; |
| 80 | + |
| 81 | + for (k = 0; table[k]; k++) { |
| 82 | + int r = table[k](state, ctx); |
| 83 | + if (r) |
| 84 | + return r; |
| 85 | + } |
| 86 | + |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
45 | 90 | void fsm_health__loop(struct fsmonitor_daemon_state *state)
|
46 | 91 | {
|
47 | 92 | struct fsm_health_data *data = state->health_data;
|
| 93 | + int r; |
| 94 | + |
| 95 | + r = call_all(state, CTX_INIT); |
| 96 | + if (r < 0) |
| 97 | + goto force_error_stop; |
| 98 | + if (r > 0) |
| 99 | + goto force_shutdown; |
48 | 100 |
|
49 | 101 | for (;;) {
|
50 | 102 | DWORD dwWait = WaitForMultipleObjects(data->nr_handles,
|
51 | 103 | data->hHandles,
|
52 |
| - FALSE, INFINITE); |
| 104 | + FALSE, WAIT_FREQ_MS); |
53 | 105 |
|
54 | 106 | if (dwWait == WAIT_OBJECT_0 + HEALTH_SHUTDOWN)
|
55 | 107 | goto clean_shutdown;
|
56 | 108 |
|
| 109 | + if (dwWait == WAIT_TIMEOUT) { |
| 110 | + r = call_all(state, CTX_TIMER); |
| 111 | + if (r < 0) |
| 112 | + goto force_error_stop; |
| 113 | + if (r > 0) |
| 114 | + goto force_shutdown; |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
57 | 118 | error(_("health thread wait failed [GLE %ld]"),
|
58 | 119 | GetLastError());
|
59 | 120 | goto force_error_stop;
|
60 | 121 | }
|
61 | 122 |
|
62 | 123 | force_error_stop:
|
63 | 124 | state->health_error_code = -1;
|
| 125 | +force_shutdown: |
64 | 126 | ipc_server_stop_async(state->ipc_server_data);
|
65 | 127 | clean_shutdown:
|
| 128 | + call_all(state, CTX_TERM); |
66 | 129 | return;
|
67 | 130 | }
|
68 | 131 |
|
|
0 commit comments