From 4ad218b5710659899ba842da5a597db5052f2d4d Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sun, 12 Feb 2017 23:44:52 +0100 Subject: blogc-make: implemented automatic reloader for runserver rule this is not the best implementation possible, but it works for most use cases --- src/blogc-make/rules.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'src/blogc-make/rules.c') diff --git a/src/blogc-make/rules.c b/src/blogc-make/rules.c index 711809a..1f18d5e 100644 --- a/src/blogc-make/rules.c +++ b/src/blogc-make/rules.c @@ -12,6 +12,9 @@ #include #include #include +#include +#include +#include #include "../common/utils.h" #include "ctx.h" #include "exec.h" @@ -552,14 +555,77 @@ static int all_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bool verbose); // RUNSERVER RULE + +typedef struct { + bm_ctx_t *ctx; + bool verbose; +} runserver_args_t; + +static runserver_args_t *args = NULL; + +static void +runserver_sig_handler(int sig) +{ + free(args); + args = NULL; +} + +static void* +runserver_thread(void *arg) +{ + signal(SIGINT, runserver_sig_handler); + signal(SIGTERM, runserver_sig_handler); + args = arg; + while (true) { + bm_ctx_reload(args->ctx); + if (args->ctx == NULL) { + fprintf(stderr, "blogc-make: error: failed to reload context. " + "reloader disabled!\n"); + goto runserver_cleanup; + } + if (0 != all_exec(args->ctx, NULL, args->verbose)) { + fprintf(stderr, "blogc-make: error: failed to rebuild website. " + "reloader disabled!\n"); + goto runserver_cleanup; + } + sleep(1); + } + +runserver_cleanup: + free(args); + args = NULL; + + return NULL; +} + static int runserver_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bool verbose) { + // first 'all' call is syncronous, to do a 'sanity check' int rv = all_exec(ctx, NULL, verbose); if (rv != 0) return rv; - return bm_exec_blogc_runserver(ctx->settings, verbose); + runserver_args_t *args = bc_malloc(sizeof(runserver_args_t)); + args->ctx = ctx; + args->verbose = verbose; + + pthread_t thread; + + if (0 != pthread_create(&thread, NULL, runserver_thread, args)) { + fprintf(stderr, "blogc-make: error: failed to create blogc-runserver " + "thread!\n"); + return 3; + } + + if (0 != pthread_detach(thread)) { + fprintf(stderr, "blogc-make: error: failed to detach blogc-runserver " + "thread!\n"); + return 3; + } + + bm_exec_blogc_runserver(ctx->settings, verbose); + return 0; } -- cgit v1.2.3-18-g5258