-
Notifications
You must be signed in to change notification settings - Fork 49
/
heartbeat.c
173 lines (150 loc) · 4.39 KB
/
heartbeat.c
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
/************************************************************\
* Copyright 2021 Lawrence Livermore National Security, LLC
* (c.f. AUTHORS, NOTICE.LLNS, COPYING)
*
* This file is part of the Flux resource manager framework.
* For details, see https://github.com/flux-framework.
*
* SPDX-License-Identifier: LGPL-3.0
\************************************************************/
/* heartbeat.c - publish regular heartbeat messages
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <flux/core.h>
#include <src/common/libutil/fsd.h>
#include "ccan/str/str.h"
static const double default_period = 2.0;
struct heartbeat {
flux_t *h;
double period;
flux_watcher_t *timer;
flux_msg_handler_t **handlers;
flux_future_t *f;
};
static void heartbeat_get_cb (flux_t *h,
flux_msg_handler_t *mh,
const flux_msg_t *msg,
void *arg)
{
struct heartbeat *hb = arg;
if (flux_request_decode (msg, NULL, NULL) < 0)
goto error;
if (flux_respond_pack (h,
msg,
"{s:f}",
"period",
hb->period) < 0)
flux_log_error (h, "error responding to heartbeat.get request");
return;
error:
if (flux_respond_error (h, msg, errno, NULL) < 0)
flux_log_error (h, "error responding to heartbeat.get request");
}
static void publish_continuation (flux_future_t *f, void *arg)
{
struct heartbeat *hb = arg;
if (flux_future_get (f, NULL) < 0)
flux_log_error (hb->h, "error publishing heartbeat");
flux_future_destroy (f);
hb->f = NULL;
}
static void timer_cb (flux_reactor_t *r,
flux_watcher_t *w,
int revents,
void *arg)
{
struct heartbeat *hb = arg;
flux_future_destroy (hb->f);
if (!(hb->f = flux_event_publish (hb->h, "heartbeat.pulse", 0, NULL))) {
flux_log_error (hb->h, "error sending publish request");
return;
}
if (flux_future_then (hb->f, -1, publish_continuation, hb) < 0) {
flux_log_error (hb->h, "error setting up continuation");
flux_future_destroy (hb->f);
hb->f = NULL;
}
}
static int parse_args (int argc, char **argv, struct heartbeat *hb)
{
int i;
for (i = 0; i < argc; i++) {
if (strstarts (argv[i], "period=")) {
if (fsd_parse_duration (argv[i] + 7, &hb->period) < 0) {
flux_log_error (hb->h, "error parsing period value");
return -1;
}
}
else {
flux_log (hb->h, LOG_ERR, "unknown option: %s", argv[i]);
goto inval;
}
}
return 0;
inval:
errno = EINVAL;
return -1;
}
static const struct flux_msg_handler_spec htab[] = {
{ FLUX_MSGTYPE_REQUEST,
"heartbeat.get",
heartbeat_get_cb,
FLUX_ROLE_USER
},
FLUX_MSGHANDLER_TABLE_END,
};
static void heartbeat_destroy (struct heartbeat *hb)
{
if (hb) {
int saved_errno = errno;
flux_future_destroy (hb->f);
flux_msg_handler_delvec (hb->handlers);
flux_watcher_destroy (hb->timer);
free (hb);
errno = saved_errno;
}
}
static struct heartbeat *heartbeat_create (flux_t *h)
{
struct heartbeat *hb;
if (!(hb = calloc (1, sizeof (*hb))))
return NULL;
hb->h = h;
hb->period = default_period;
if (flux_msg_handler_addvec (hb->h, htab, hb, &hb->handlers) < 0)
goto error;
return hb;
error:
heartbeat_destroy (hb);
return NULL;
}
int mod_main (flux_t *h, int argc, char **argv)
{
struct heartbeat *hb;
flux_reactor_t *r = flux_get_reactor (h);
if (!(hb = heartbeat_create (h)))
return -1;
if (parse_args (argc, argv, hb) < 0)
goto error;
if (!(hb->timer = flux_timer_watcher_create (r,
0.,
hb->period,
timer_cb,
hb)))
goto error;
flux_watcher_start (hb->timer);
if (flux_reactor_run (r, 0) < 0) {
flux_log_error (h, "flux_reactor_run");
goto error;
}
heartbeat_destroy (hb);
return 0;
error:
heartbeat_destroy (hb);
return -1;
}
/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/