aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/worker.c
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2022-08-26 05:51:53 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2022-08-26 05:54:22 +0200
commit1de2aaa4001eb2d0ddfc4f86ab2c2a3eebdc9841 (patch)
tree85b0290fd51c220a9a621e98c0b88d92ce776e33 /src/worker.c
parentworker: capture process output (diff)
downloadcimple-1de2aaa4001eb2d0ddfc4f86ab2c2a3eebdc9841.tar.gz
cimple-1de2aaa4001eb2d0ddfc4f86ab2c2a3eebdc9841.zip
worker: allow graceful shutdowns
Well, maybe "graceful" is a strong word, but now you _can_ do ./server & ./worker & ./client ci_run URL REV && kill "$( pidof worker )" and the worker will wait for the CI run to complete.
Diffstat (limited to 'src/worker.c')
-rw-r--r--src/worker.c29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/worker.c b/src/worker.c
index 0dc64d3..d26408f 100644
--- a/src/worker.c
+++ b/src/worker.c
@@ -5,6 +5,7 @@
#include "msg.h"
#include "net.h"
#include "process.h"
+#include "signal.h"
#include <errno.h>
#include <pthread.h>
@@ -45,6 +46,13 @@ git_shutdown:
void worker_destroy(struct worker *worker)
{
+ print_log("Shutting down\n");
+
+ if (worker->task_active) {
+ if (pthread_join(worker->task, NULL))
+ print_errno("pthread_join");
+ worker->task_active = 0;
+ }
if (pthread_mutex_destroy(&worker->task_mtx))
print_errno("pthread_mutex_destroy");
close(worker->fd);
@@ -141,6 +149,7 @@ static void *worker_task_wrapper(void *_ctx)
static int worker_schedule_task(struct worker *worker, const struct msg *msg, worker_task_body body)
{
struct worker_task_context *ctx;
+ pthread_attr_t attr;
int ret = 0;
ctx = malloc(sizeof(*ctx));
@@ -156,15 +165,29 @@ static int worker_schedule_task(struct worker *worker, const struct msg *msg, wo
goto free_ctx;
}
+ ret = pthread_attr_init(&attr);
+ if (ret < 0) {
+ print_errno("pthread_attr_init");
+ goto free_msg;
+ }
+
+ ret = signal_set_thread_attr(&attr);
+ if (ret < 0)
+ goto free_attr;
+
ret = pthread_create(&worker->task, NULL, worker_task_wrapper, ctx);
if (ret < 0) {
print_errno("pthread_create");
- goto free_msg;
+ goto free_attr;
}
worker->task_active = 1;
+ pthread_attr_destroy(&attr);
return ret;
+free_attr:
+ pthread_attr_destroy(&attr);
+
free_msg:
msg_free(ctx->msg);
free(ctx->msg);
@@ -245,11 +268,13 @@ int worker_main(struct worker *worker, int, char *[])
if (ret < 0)
return ret;
- while (1) {
+ while (!global_stop_flag) {
print_log("Waiting for a new command\n");
ret = msg_recv_and_handle(worker->fd, worker_msg_handler_lock, worker);
if (ret < 0)
return ret;
}
+
+ return ret;
}