feat: cleanup and daemonization

This commit is contained in:
2024-08-04 12:45:32 +02:00
parent dcc25f88a7
commit 1bcdca9e21
11 changed files with 64 additions and 49 deletions

View File

@ -1,8 +1,7 @@
#include "log.h"
#include <stdbool.h>
#include <assert.h>
#include <time.h>
#include <pthread.h>
#include <stdarg.h>
pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
log_settings g_log_settings;

View File

@ -4,14 +4,8 @@
#include "pipeline.h"
#include "threadpool.h"
#include "util.h"
#include <bits/pthreadtypes.h>
#include <pthread.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
void executor(void* data) {
const char* command = data;
@ -21,7 +15,8 @@ void executor(void* data) {
threadpool* pool = NULL;
void on_event(pipeline_event* const e) {
threadpool_add_work(pool, executor, (void*)e->command);
if(!threadpool_add_work(pool, executor, (void*)e->command))
log_error("could not add work to the threadpool");
}
void* listen_for_changes_thread(void* data) {
@ -74,7 +69,6 @@ int main(int argc, char** argv) {
settings.use_colors = args.use_colors;
settings.out_file = args.log_file.has_value ? fopen(args.log_file.value, "w+") : stdout;
log_init(settings);
pool = threadpool_create(args.executors);
if(args.help) {
print_help(stdout, argv[0]);
@ -100,8 +94,14 @@ int main(int argc, char** argv) {
exit(EXIT_FAILURE);
}
pool = threadpool_create(args.executors);
per_line(args.config_file.value, &config_interpret_line);
// BOOKMARK: You were reading :Man system.unit and :Man systemd.service as preperation on making a systemd unit file
// This will be needed for the .deb package, as well as the arch linux package.
// alpine linux is using OpenRC (cool), which complicates things a little bit, but shouldn't be too bad. The wiki is
// generally really well written. Otherwise, I am sure that both wiki.gentoo and wiki.archlinux have great pages too
// docker is super easy, just make a dockerfile - only concern is the trigger files.
pipeline_loop();
threadpool_destroy(pool);
}

View File

@ -2,7 +2,6 @@
#include "pipeline.h"
#include "threadlist.h"
#include "util.h"
#include <assert.h>
#include <regex.h>
#include <stdlib.h>

View File

@ -1,15 +1,20 @@
#include "threadpool.h"
#include "log.h"
#include "threadpool.h"
#include <stdlib.h>
static threadpool_work* threadpool_work_create(thread_func func, void *arg) {
threadpool_work* result;
if (func == NULL)
return NULL;
result = malloc(sizeof(threadpool_work));
result->func = func;
result->arg = arg;
result->next = NULL;
static optional_threadpool_work threadpool_work_create(thread_func func, void *arg) {
optional_threadpool_work result;
result.value = NULL;
result.has_value = false;
if(func == NULL) {
log_error("cant create threadpool work, function is null");
return result;
}
result.value = malloc(sizeof(threadpool_work));
result.value->func = func;
result.value->arg = arg;
result.value->next = NULL;
result.has_value = true;
return result;
}
@ -91,7 +96,6 @@ threadpool* threadpool_create(size_t num) {
}
return result;
}
// TODO: add log statements
void threadpool_destroy(threadpool* pool) {
log_trace("destroying threadpool");
@ -122,21 +126,24 @@ void threadpool_destroy(threadpool* pool) {
bool threadpool_add_work(threadpool* pool, thread_func func, void *arg) {
log_trace("adding work task to pool");
threadpool_work* work;
if (pool == NULL)
if(pool == NULL) {
log_error("could not add work to threadpool, pool is null");
return false;
}
work = threadpool_work_create(func, arg);
if (work == NULL)
optional_threadpool_work work = threadpool_work_create(func, arg);
if(!work.has_value) {
log_error("could not add work to threadpool");
return false;
}
pthread_mutex_lock(&(pool->work_mutex));
if (pool->work_first == NULL) {
pool->work_first = work;
pool->work_first = work.value;
pool->work_last = pool->work_first;
} else {
pool->work_last->next = work;
pool->work_last = work;
pool->work_last->next = work.value;
pool->work_last = work.value;
}
pthread_cond_broadcast(&(pool->work_cond));
pthread_mutex_unlock(&(pool->work_mutex));
@ -145,8 +152,10 @@ bool threadpool_add_work(threadpool* pool, thread_func func, void *arg) {
}
void threadpool_wait(threadpool* pool) {
if (pool == NULL)
if(pool == NULL) {
log_error("pool object is null");
return;
}
pthread_mutex_lock(&(pool->work_mutex));
while (1) {
if (pool->work_first != NULL || (!pool->stop && pool->working_count != 0) || (pool->stop && pool->thread_count != 0))