#include "daemon.h"
#include "module.h"
+// Interval after which the heartbeat function is being called again
+#define HEARTBEAT 60000000 // 60s
+
struct collecty_module {
collecty_ctx* ctx;
int nrefs;
// Events
struct {
+ sd_event_source* heartbeat;
sd_event_source* shutdown;
} events;
};
-static int collecty_daemon_module_shutdown(sd_event_source* source, void* data) {
+static int collecty_module_heartbeat(sd_event_source* source, uint64_t usec, void* data) {
+ collecty_module* self = data;
+ int r;
+
+ DEBUG(self->ctx, "Heartbeat called for %s\n", collecty_module_name(self));
+
+ // XXX TODO
+
+ // Arm the timer again
+ r = sd_event_source_set_time(self->events.heartbeat, usec + HEARTBEAT);
+ if (r < 0)
+ return r;
+
+ // Call this continuously
+ r = sd_event_source_set_enabled(self->events.heartbeat, SD_EVENT_ON);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+static int collecty_module_register_heartbeat(collecty_module* self) {
+ int r;
+
+ // Call the heartbeat function immediately
+ r = sd_event_add_time_relative(self->loop, &self->events.heartbeat,
+ CLOCK_MONOTONIC, 0, 0, collecty_module_heartbeat, self);
+ if (r < 0) {
+ ERROR(self->ctx, "Failed to register the heartbeat timer: %s\n", strerror(-r));
+ return r;
+ }
+
+ return 0;
+}
+
+static int collecty_module_shutdown(sd_event_source* source, void* data) {
collecty_module* self = data;
DEBUG(self->ctx, "Shutting down module '%s'\n", collecty_module_name(self));
// Call the shutdown function when the loop exits
r = sd_event_add_exit(self->loop, &self->events.shutdown,
- collecty_daemon_module_shutdown, collecty_module_ref(self));
+ collecty_module_shutdown, collecty_module_ref(self));
if (r < 0) {
ERROR(self->ctx, "Failed to register module shutdown: %s\n", strerror(-r));
}
static void collecty_module_free(collecty_module* self) {
if (self->methods->free)
self->methods->free(self->ctx);
+ if (self->events.heartbeat)
+ sd_event_source_unref(self->events.heartbeat);
if (self->events.shutdown)
sd_event_source_unref(self->events.shutdown);
if (self->loop)
// Store the methods
self->methods = methods;
+ // Register heartbeat
+ r = collecty_module_register_heartbeat(self);
+ if (r < 0)
+ goto ERROR;
+
// Register shutdown
r = collecty_module_register_shutdown(self);
if (r < 0)