feat: handle sigint properly

This commit is contained in:
Asger Gitz-Johansen 2024-09-09 07:21:09 +02:00
parent bdf3a54ba0
commit 99174939f5
2 changed files with 15 additions and 0 deletions

View File

@ -22,6 +22,7 @@
#include "pipeline.h" #include "pipeline.h"
#include "threadpool.h" #include "threadpool.h"
#include "util.h" #include "util.h"
#include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -86,6 +87,12 @@ void* listen_for_config_changes_thread(void* data) {
return NULL; return NULL;
} }
void signal_handler(int signal) {
log_info("signal retrieved");
if(signal == SIGINT)
pipeline_cancel();
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
cli_options args = parse(argc, argv); cli_options args = parse(argc, argv);
log_settings settings; log_settings settings;
@ -94,6 +101,8 @@ int main(int argc, char** argv) {
settings.out_file = args.log_file.has_value ? fopen(args.log_file.value, "w+") : stdout; settings.out_file = args.log_file.has_value ? fopen(args.log_file.value, "w+") : stdout;
log_init(settings); log_init(settings);
signal(SIGINT, signal_handler);
if(args.help) { if(args.help) {
print_help(stdout, argv[0]); print_help(stdout, argv[0]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);

View File

@ -26,6 +26,7 @@
pthread_list_node* root = NULL; pthread_list_node* root = NULL;
optional_pipeline_conf pipeline_create(const char* config_line) { optional_pipeline_conf pipeline_create(const char* config_line) {
log_trace("pipeline create");
optional_pipeline_conf result; optional_pipeline_conf result;
result.has_value = false; result.has_value = false;
const char* pattern = "[^[:blank:]]+|\"[^\"]*\""; const char* pattern = "[^[:blank:]]+|\"[^\"]*\"";
@ -72,6 +73,7 @@ optional_pipeline_conf pipeline_create(const char* config_line) {
} }
void pipeline_destroy(pipeline_conf* conf) { void pipeline_destroy(pipeline_conf* conf) {
log_trace("pipeline destroy");
free(conf->name); free(conf->name);
free(conf->url); free(conf->url);
free(conf->trigger); free(conf->trigger);
@ -80,6 +82,7 @@ void pipeline_destroy(pipeline_conf* conf) {
} }
void pipeline_register(pthread_t thread) { void pipeline_register(pthread_t thread) {
log_trace("pipeline register thread");
if(root == NULL) { if(root == NULL) {
root = create_thread_node(thread); root = create_thread_node(thread);
return; return;
@ -88,11 +91,13 @@ void pipeline_register(pthread_t thread) {
} }
void pipeline_loop() { void pipeline_loop() {
log_trace("pipeline loop");
clear_thread_list(root); clear_thread_list(root);
root = NULL; root = NULL;
} }
void pipeline_cancel() { void pipeline_cancel() {
log_trace("cancelling pipeline");
pthread_list_node* cursor = root; pthread_list_node* cursor = root;
while(cursor != NULL) { while(cursor != NULL) {
pthread_cancel(cursor->thread); pthread_cancel(cursor->thread);
@ -101,6 +106,7 @@ void pipeline_cancel() {
} }
void pipeline_event_destroy(pipeline_event* ev) { void pipeline_event_destroy(pipeline_event* ev) {
log_trace("pipeline event destroy");
free(ev->name); free(ev->name);
free(ev->trigger); free(ev->trigger);
free(ev->url); free(ev->url);