diff --git a/include/notify.h b/include/notify.h index 205d6f3..2dc0fee 100644 --- a/include/notify.h +++ b/include/notify.h @@ -21,9 +21,12 @@ #include typedef void(*notify_callback)(pipeline_event* const); +typedef void(*config_change_callback)(); // Start listening for changes to the provided file. // Note that the `struct inotify_event*` provided is a managed pointer. void listen_for_changes(const pipeline_conf* config, notify_callback callback); +void listen_for_config_changes(const char* config_filepath, config_change_callback callback); + #endif diff --git a/src/main.c b/src/main.c index 3f71a01..51129dc 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,7 @@ threadpool* pool = NULL; char* trigger_dir = "/tmp/sci"; +bool config_file_changed = false; void on_event(pipeline_event* const e) { if(!threadpool_add_work(pool, executor, (void*)e)) @@ -68,6 +69,18 @@ void config_interpret_line(const char* line) { pipeline_register(t); } +void on_config_file_changed() { + config_file_changed = true; + pipeline_cancel(); + log_info("config file changed, reloading..."); +} + +void* listen_for_config_changes_thread(void* data) { + while(1) + listen_for_config_changes((const char*)data, &on_config_file_changed); + return NULL; +} + int main(int argc, char** argv) { cli_options args = parse(argc, argv); log_settings settings; @@ -108,9 +121,18 @@ int main(int argc, char** argv) { if(args.environment_vars.has_value) set_shared_environment(args.environment_vars.value); - pool = threadpool_create(args.executors); - per_line(args.config_file.value, &config_interpret_line); - - pipeline_loop(); - threadpool_destroy(pool); + log_info("spawning trigger thread for config file"); + pthread_t conf_listener; + ASSERT_SYSCALL_SUCCESS(pthread_create(&conf_listener, NULL, &listen_for_config_changes_thread, (void*)args.config_file.value)); + pthread_setname_np(conf_listener, "sci-conf-listener"); + do { + config_file_changed = false; + worker_pool = threadpool_create(args.executors); + per_line(args.config_file.value, &config_interpret_line); + log_info("listening for pipeline invocations"); + pipeline_loop(); + } while(config_file_changed); + pthread_cancel(conf_listener); + threadpool_destroy(worker_pool); + destroy_options(args); } diff --git a/src/pipeline.c b/src/pipeline.c index 6cfda3b..6d5f923 100644 --- a/src/pipeline.c +++ b/src/pipeline.c @@ -48,7 +48,8 @@ optional_pipeline_conf pipeline_create(const char* config_line) { cursor += pmatch[0].rm_eo; } if(i != 4) { - log_error("invalid configuration!\nline is invalid: \"%s\""); + log_error("invalid configuration!"); + log_error("line is invalid: \"%s\"", config_line); for(int j = i-1; j >= 0; j--) free(opts[j]); return result;