chore: some cleanup

yes. I made the cardinal sin. I am using a goto.
This commit is contained in:
Asger Gitz-Johansen 2024-08-06 20:18:22 +02:00
parent 6c1102db43
commit 5469fdcf92

View File

@ -14,52 +14,71 @@
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* create_pipeline_id() {
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);
return pipeline_id;
}
optional_int open_logfile(const char* const pipeline_id) {
optional_int result;
result.has_value = false;
result.value = 0;
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) {
if (fd != -1) {
result.has_value = true;
result.value = fd;
} else
perror("open");
free(log_file);
free(log_filepath);
return result;
}
void executor(void* data) {
// Create pipeline id
char* pipeline_id = create_pipeline_id();
// Create logfile path
optional_int fd = open_logfile(pipeline_id);
if(!fd.has_value)
return;
}
posix_spawn_file_actions_adddup2(&actions, fd, STDOUT_FILENO);
posix_spawn_file_actions_adddup2(&actions, fd, STDERR_FILENO);
// spawn the process
pid_t pid;
posix_spawn_file_actions_t actions;
posix_spawn_file_actions_init(&actions);
posix_spawn_file_actions_adddup2(&actions, fd.value, STDOUT_FILENO);
posix_spawn_file_actions_adddup2(&actions, fd.value, STDERR_FILENO);
const pipeline_event* const e = data;
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* 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;
goto end; // I know. The raptors have picked up the scent. I'll just have to mask it with more poopy code.
}
log_trace("{%s} spawned (%s)", pipeline_id, e->name);
log_trace("{%s} (%s) spawned", pipeline_id, e->name);
// Wait for process to complete
int status;
waitpid(pid, &status, 0);
if(WIFEXITED(status))
log_trace("{%s} exited with status %d", pipeline_id, WEXITSTATUS(status));
// TODO: clean this function up!
log_trace("{%s} (%s) exited with status %d", pipeline_id, e->name, WEXITSTATUS(status));
end:
close(fd.value);
free(pipeline_id);
free(log_file);
free(log_filepath);
free(name);
free(url);
free(trigger);
free(id);
}
void on_event(pipeline_event* const e) {