From 6c1102db43fa0a1e87f26afbd24d7617c9f741fe Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Tue, 6 Aug 2024 20:05:20 +0200 Subject: [PATCH] feat: add option to set where pipeline logfiles go --- include/cli.h | 1 + src/cli.c | 20 ++++++++++++++++---- src/main.c | 38 +++++++++++++++++++++++++------------- src/pipeline.c | 13 +++++-------- src/threadpool.c | 2 +- 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/include/cli.h b/include/cli.h index 09ac137..85dd468 100644 --- a/include/cli.h +++ b/include/cli.h @@ -11,6 +11,7 @@ typedef struct { bool version; bool use_colors; optional_str log_file; + optional_str pipeline_log_dir; } cli_options; // Construct a new cli_options struct instance. diff --git a/src/cli.c b/src/cli.c index ea5ec39..aff19cc 100644 --- a/src/cli.c +++ b/src/cli.c @@ -21,6 +21,9 @@ cli_options new_options() { result.log_file.has_value = false; result.log_file.value = NULL; + + result.pipeline_log_dir.has_value = false; + result.pipeline_log_dir.value = NULL; return result; } @@ -29,28 +32,33 @@ void destroy_options(cli_options v) { free(v.config_file.value); if(v.log_file.has_value) free(v.log_file.value); + if(v.pipeline_log_dir.has_value) + free(v.pipeline_log_dir.value); } // threadpool* pool = NULL; +char* log_dir = "./"; // NOTE: must end with a / void executor(void* data) { const pipeline_event* const e = data; pid_t pid; - char* name = join("SCI_PIPELINE_NAME=", e->name); - char* url = join("SCI_PIPELINE_URL=", e->url); - char* trigger = join("SCI_PIPELINE_TRIGGER=", e->trigger); - char *envp[] = { - name, - url, - trigger, - NULL - }; - char* argv[] = { "/bin/sh", "-c", e->command, NULL }; uuid_t uuid; uuid_generate(uuid); char* pipeline_id = malloc(32); uuid_unparse_lower(uuid, pipeline_id); posix_spawn_file_actions_t actions; posix_spawn_file_actions_init(&actions); - char* output_file = join(pipeline_id, ".log"); - int fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); + char* log_file = join(pipeline_id, ".log"); + char* log_filepath = join(log_dir, log_file); + int fd = open(log_filepath, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd == -1) { perror("open"); return; } posix_spawn_file_actions_adddup2(&actions, fd, STDOUT_FILENO); posix_spawn_file_actions_adddup2(&actions, fd, STDERR_FILENO); + char* name = join("SCI_PIPELINE_NAME=", e->name); + char* url = join("SCI_PIPELINE_URL=", e->url); + char* trigger = join("SCI_PIPELINE_TRIGGER=", e->trigger); + char* id = join("SCI_PIPELINE_ID=", pipeline_id); + char *envp[] = { + name, + url, + trigger, + id, + NULL + }; + char* argv[] = { "/bin/sh", "-c", e->command, NULL }; if(posix_spawn(&pid, "/bin/sh", &actions, NULL, argv, envp) != 0) { perror("posix_spawn"); return; @@ -49,8 +53,10 @@ void executor(void* data) { waitpid(pid, &status, 0); if(WIFEXITED(status)) log_trace("{%s} exited with status %d", pipeline_id, WEXITSTATUS(status)); + // TODO: clean this function up! free(pipeline_id); - free(output_file); + free(log_file); + free(log_filepath); free(name); free(url); free(trigger); @@ -127,7 +133,13 @@ int main(int argc, char** argv) { exit(EXIT_FAILURE); } + if(args.pipeline_log_dir.has_value) + log_dir = args.pipeline_log_dir.value; + struct stat st = {0}; + if(stat(log_dir, &st) == -1) + mkdir(log_dir, 0700); + if(stat("/tmp/sci", &st) == -1) mkdir("/tmp/sci", 0700); diff --git a/src/pipeline.c b/src/pipeline.c index 5338ba1..a695842 100644 --- a/src/pipeline.c +++ b/src/pipeline.c @@ -38,14 +38,11 @@ optional_pipeline_conf pipeline_create(const char* config_line) { result.value->url = opts[1]; result.value->trigger = opts[2]; result.value->command = opts[3]; - const char* msg = - "config:\n" - "name: %s\n" - "url: %s\n" - "trigger: %s\n" - "command: %s" - ; - log_trace(msg, result.value->name, result.value->url, result.value->trigger, result.value->command); + log_trace("read config:"); + log_trace(" name=%s", result.value->name); + log_trace(" url=%s", result.value->url); + log_trace(" trigger=%s", result.value->trigger); + log_trace(" command=%s", result.value->command); result.has_value = true; return result; } diff --git a/src/threadpool.c b/src/threadpool.c index f7cbb49..366fc9a 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -42,7 +42,7 @@ static threadpool_work* threadpool_work_pop(threadpool* pool) { return work; } -static void* threadpool_worker(void *arg) { +static void* threadpool_worker(void* arg) { log_trace("threadpool worker spawned"); threadpool* pool = arg; threadpool_work* work;