RejectedSoftware Forums

Sign up

Shutting down or restarting a vibe.d app

Is there a clean way to shut down a vibe.d app?

vibe.core.core.exitEventLoop() seems to simply exit the event loop,
which means that unfinished HTTP requests will be aborted, right?

I also see vibe.core.core.setIdleHandler() which could be useful, but
again, how do I tell vibe.d not to accept new requests?

I would like to use this for a SIGHUP handler for reopening log files
after logrotate, or reloading the configuration.

Re: Shutting down or restarting a vibe.d app

On Tue, 30 Jun 2015 18:31:06 +0200, Marc Schütz wrote:

Is there a clean way to shut down a vibe.d app?

vibe.core.core.exitEventLoop() seems to simply exit the event loop,
which means that unfinished HTTP requests will be aborted, right?

I also see vibe.core.core.setIdleHandler() which could be useful, but
again, how do I tell vibe.d not to accept new requests?

I would like to use this for a SIGHUP handler for reopening log files
after logrotate, or reloading the configuration.

listenHTTP now returns a HTTPListener that has a method stopListening(), which could be used for this purpose. Then, if everything would work correctly, as soon as all tasks have finished, the event loop should automatically exit. Unfortunately this is still not the case.

To do this manually as a workaround, something like this should work:

Router s_router;
int s_activeRequestCount = 0;
HTTPServerListener s_listener;
bool s_exit;

void countRequestsWrapper(HTTPServerRequest req, HTTPServerResponse res)
{
	s_activeRequestCount++;
	scope (exit) {
		if (!--s_activeRequestCount && s_exit)
			exitEventLoop();
	}

	s_router.handleRequest(req, res);
}

void exitCleanly()
{
	if (!s_activeRequestCount) exitEventLoop();
	else {
		s_listener.stopListening();
		s_exit = true;
	}
}

static this()
{
	s_router = new URLRouter;
	// ...

	s_listener = listenHTTP(settings, &countRequestsWrapper);
}

Re: Shutting down or restarting a vibe.d app

Thanks, I'll try that. Another related question:

I have a worker thread running in parallel with the main thread. This
worker thread exits when it receives OwnerTerminated, and it used to
shut down cleanly when the program was terminated, e.g. by SIGINT.
However, something has changed lately, and I can't figure out what it
is. When I run the program with dub and press Ctrl-C, it prints
"Received signal 2. Shutting down.", but actually keeps running (in the
background, dub itself exits). The worker thread doesn't received an
ownerTerminated signal anymore, though I can't tell for sure whether
this part actually worked that way before, I only know that it exited.

Do you have an idea what could have caused this? It only happend
yesterday, and I usually follow vibe.d's (and druntime's/Phobos') Git
closely, so there are not that many candidates. But I tried with older
vibe.d versions, and it still doesn't quit cleanly.

Failing that, is there a way to detect when a shutdown has been
requested, so that I can notify my worker thread manually?

(openSUSE 13.2 x86_64, libevent 2.0.21)

Re: Shutting down or restarting a vibe.d app

Am Wed, 1 Jul 2015 12:59:30 +0200
schrieb Marc Schütz schuetzm@gmx.net:

Thanks, I'll try that. Another related question:

I have a worker thread running in parallel with the main thread. This
worker thread exits when it receives OwnerTerminated, and it used to
shut down cleanly when the program was terminated, e.g. by SIGINT.
However, something has changed lately, and I can't figure out what it
is. When I run the program with dub and press Ctrl-C, it prints
"Received signal 2. Shutting down.", but actually keeps running (in
the background, dub itself exits). The worker thread doesn't
received an ownerTerminated signal anymore, though I can't tell for
sure whether this part actually worked that way before, I only know
that it exited.

Do you have an idea what could have caused this? It only happend
yesterday, and I usually follow vibe.d's (and druntime's/Phobos') Git
closely, so there are not that many candidates. But I tried with older
vibe.d versions, and it still doesn't quit cleanly.

Failing that, is there a way to detect when a shutdown has been
requested, so that I can notify my worker thread manually?

(openSUSE 13.2 x86_64, libevent 2.0.21)

Nevermind, I found this:
http://dlang.org/phobos/core_thread.html#.Thread.isDaemon

Setting this in my worker thread makes it work correctly, including the
OwnerTerminated message.