Browse Source

server exits properly

master
alistair 3 years ago
parent
commit
736e9028f4
  1. 6
      darkhttpd.c
  2. 25
      main.cpp
  3. 1
      templater.hpp
  4. 12
      util.cpp
  5. 1
      util.h

6
darkhttpd.c

@ -2727,12 +2727,12 @@ static void pidfile_create(void) { @@ -2727,12 +2727,12 @@ static void pidfile_create(void) {
/* [<-] end of pidfile helpers. */
/* Close all sockets and FILEs and exit. */
static void stop_running(int sig unused) {
void stop_running(int sig unused) {
running = 0;
}
/* Execution starts here. */
int darkhttpd_main(int argc, const char **argv) {
int darkhttpd_main(int argc, char **argv) {
printf("%s, %s.\n", pkgname, copyright);
parse_default_extension_map();
parse_commandline(argc, argv);
@ -2762,8 +2762,6 @@ int darkhttpd_main(int argc, const char **argv) { @@ -2762,8 +2762,6 @@ int darkhttpd_main(int argc, const char **argv) {
/* signals */
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
err(1, "signal(ignore SIGPIPE)");
if (signal(SIGINT, stop_running) == SIG_ERR)
err(1, "signal(SIGINT)");
if (signal(SIGTERM, stop_running) == SIG_ERR)
err(1, "signal(SIGTERM)");

25
main.cpp

@ -23,6 +23,7 @@ @@ -23,6 +23,7 @@
#include <tinyxml2.h>
#include <unistd.h>
#include <csignal>
#include "default-templates.h"
#include "templater.hpp"
@ -36,6 +37,7 @@ const std::string TEMPLATE_FILEEXT = "template"; @@ -36,6 +37,7 @@ const std::string TEMPLATE_FILEEXT = "template";
const std::string DEFAULT_WATCH_FILETYPES[] = {"md", "markdown", TEMPLATE_FILEEXT};
const std::string DEFAULT_IGNORE_FILES[] = {SITE_CONFIG_FNAME};
const std::string TEST_URL = "http://localhost:8000";
static volatile bool continue_running = true;
using json = nlohmann::json;
namespace fs = std::filesystem;
@ -708,6 +710,12 @@ std::map<std::string, std::string> parse_options (int argc, char **argv) { @@ -708,6 +710,12 @@ std::map<std::string, std::string> parse_options (int argc, char **argv) {
return parsed_opts;
}
void kill_childs(int sig)
{
continue_running = false;
}
int main(int argc, char **argv) {
// TODO:
//
@ -749,22 +757,27 @@ int main(int argc, char **argv) { @@ -749,22 +757,27 @@ int main(int argc, char **argv) {
auto cmd_options = parse_options(argc, argv);
stgen::builder b (cmd_options);
b.build();
if (settings.test) {
signal(SIGINT, kill_childs);
server s {};
s.serve_now(cmd_options.at("publish_root"), "8000", "127.0.0.1");
spdlog::info("Rebuilding every 5 seconds.");
while (true) {
/* darkhttpd registers a signal handler so this just uses that as it is
* compiled in rather than fork and exec'd
*/
while (continue_running) {
b.build();
unsigned int microsecond = 1000000;
usleep(5 * microsecond);//sleeps for 3 second
spdlog::info("Rebuilt.");
spdlog::info("Built site.");
sleep(5);
}
s.stop_serving();
}
putchar('\n');
return 0;
}

1
templater.hpp

@ -32,7 +32,6 @@ struct blog_item { @@ -32,7 +32,6 @@ struct blog_item {
std::map<std::string, std::string> properties;
time_t post_date;
};
#include "templater.h"
class substitution_plugin {

12
util.cpp

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
#include "util.h"
#include <signal.h>
#include <csignal>
#include <sys/wait.h>
std::string
@ -115,13 +115,13 @@ void server::serve_now(std::string wroot, std::string port, std::string addr) { @@ -115,13 +115,13 @@ void server::serve_now(std::string wroot, std::string port, std::string addr) {
}
void server::stop_serving() {
kill(spid, SIGTERM);
waitpid(spid, NULL, 0);
if (spid) {
kill(spid, SIGTERM);
waitpid(-1, NULL, 0);
}
spid = 0;
}
server::~server() {
if (spid) {
stop_serving();
}
stop_serving();
}

1
util.h

@ -55,6 +55,7 @@ namespace darkhttpd { @@ -55,6 +55,7 @@ namespace darkhttpd {
extern "C" {
int darkhttpd_main(int argc, const char **argv);
static void stop_running(int sig);
}

Loading…
Cancel
Save