|
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 | +enum interval_fn_ctx { CTX_INIT = 0, CTX_TERM, CTX_TIMER }; |
| 13 | + |
| 14 | +typedef int (interval_fn)(struct fsmonitor_daemon_state *state, |
| 15 | + enum interval_fn_ctx ctx); |
| 16 | + |
| 17 | +static interval_fn *table[] = { |
| 18 | + NULL, /* must be last */ |
| 19 | +}; |
| 20 | + |
| 21 | +/* |
| 22 | + * Call all of the functions in the table. |
| 23 | + * Shortcut and return first error. |
| 24 | + * |
| 25 | + * Return 0 if all succeeded. |
| 26 | + */ |
| 27 | +static int call_all(struct fsmonitor_daemon_state *state, |
| 28 | + enum interval_fn_ctx ctx) |
| 29 | +{ |
| 30 | + int k; |
| 31 | + |
| 32 | + for (k = 0; table[k]; k++) { |
| 33 | + int r = table[k](state, ctx); |
| 34 | + if (r) |
| 35 | + return r; |
| 36 | + } |
| 37 | + |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
7 | 41 | struct fsm_health_data
|
8 | 42 | {
|
9 | 43 | HANDLE hEventShutdown;
|
@@ -45,24 +79,42 @@ void fsm_health__dtor(struct fsmonitor_daemon_state *state)
|
45 | 79 | void fsm_health__loop(struct fsmonitor_daemon_state *state)
|
46 | 80 | {
|
47 | 81 | struct fsm_health_data *data = state->health_data;
|
| 82 | + int r; |
| 83 | + |
| 84 | + r = call_all(state, CTX_INIT); |
| 85 | + if (r < 0) |
| 86 | + goto force_error_stop; |
| 87 | + if (r > 0) |
| 88 | + goto force_shutdown; |
48 | 89 |
|
49 | 90 | for (;;) {
|
50 | 91 | DWORD dwWait = WaitForMultipleObjects(data->nr_handles,
|
51 | 92 | data->hHandles,
|
52 |
| - FALSE, INFINITE); |
| 93 | + FALSE, WAIT_FREQ_MS); |
53 | 94 |
|
54 | 95 | if (dwWait == WAIT_OBJECT_0 + HEALTH_SHUTDOWN)
|
55 | 96 | goto clean_shutdown;
|
56 | 97 |
|
| 98 | + if (dwWait == WAIT_TIMEOUT) { |
| 99 | + r = call_all(state, CTX_TIMER); |
| 100 | + if (r < 0) |
| 101 | + goto force_error_stop; |
| 102 | + if (r > 0) |
| 103 | + goto force_shutdown; |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
57 | 107 | error(_("health thread wait failed [GLE %ld]"),
|
58 | 108 | GetLastError());
|
59 | 109 | goto force_error_stop;
|
60 | 110 | }
|
61 | 111 |
|
62 | 112 | force_error_stop:
|
63 | 113 | state->health_error_code = -1;
|
| 114 | +force_shutdown: |
64 | 115 | ipc_server_stop_async(state->ipc_server_data);
|
65 | 116 | clean_shutdown:
|
| 117 | + call_all(state, CTX_TERM); |
66 | 118 | return;
|
67 | 119 | }
|
68 | 120 |
|
|
0 commit comments