diff --git a/source/serverino/daemon.d b/source/serverino/daemon.d index 985bcc9..34207a0 100644 --- a/source/serverino/daemon.d +++ b/source/serverino/daemon.d @@ -141,6 +141,9 @@ package class WorkerInfo ~this() { + if (status != State.STOPPED) + setStatus(State.STOPPED); + clear(); } @@ -379,9 +382,6 @@ static: /// Resume the daemon. void resume() @nogc nothrow { suspended = false; } - /// Check if the daemon is suspended. - bool suspended() @nogc nothrow { return suspended; } - /// Check if the daemon is running. bool running() @nogc nothrow { return !suspended && !exitRequested; } @@ -843,6 +843,7 @@ package: } // Exit requested, shutdown everything. + // ---------------------------------- // Close all the listeners. foreach(ref listener; config.listeners) @@ -871,7 +872,6 @@ package: // Delete the canary file. removeCanary(); - // Force exit. import core.stdc.stdlib : exit; exit(0); } @@ -907,7 +907,8 @@ package: int epoll; } -__gshared: + +private __gshared: string[string] workerEnvironment; diff --git a/source/serverino/main.d b/source/serverino/main.d index 7c7cad3..d81461c 100644 --- a/source/serverino/main.d +++ b/source/serverino/main.d @@ -208,7 +208,7 @@ template ServerinoLoop(Modules...) if (ServerinoProcess.isDynamicComponent) return wakeServerino!allModules(config); else { - new Thread({ Thread.getThis.isDaemon = true; wakeServerino!allModules(config); }).start(); + new Thread({ wakeServerino!allModules(config); }).start(); return 0; } } diff --git a/tests/test-03/source/app.d b/tests/test-03/source/app.d new file mode 100644 index 0000000..322e5ef --- /dev/null +++ b/tests/test-03/source/app.d @@ -0,0 +1,72 @@ +/* +Copyright (c) 2023-2024 Andrea Fontana + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. +*/ + +module app; + +import serverino; + +import std; +import core.thread; + +mixin ServerinoBackground; + +@endpoint +void test(Request request, Output output) +{ + output.status = 200; + output ~= "OK"; + + log("Request received: ", request.path); +} + +void main() +{ + import serverino.daemon; + + assert(Daemon.running, "Wrong daemon state."); + HTTP client = HTTP(); + + // Set the timeout to 500ms. + client.connectTimeout = 500.msecs; + client.operationTimeout = 500.msecs; + client.dataTimeout = 500.msecs; + + while(!Daemon.bootCompleted) Thread.sleep(100.msecs); + + assertNotThrown(get("http://localhost:8080/1", client) == "OK"); + + Daemon.suspend(); + Thread.sleep(1.seconds); + assert(!Daemon.running, "Wrong daemon state."); + assertThrown(get("http://localhost:8080/2", client)); + + Daemon.resume(); + Thread.sleep(1.seconds); + assert(Daemon.running, "Wrong daemon state."); + assertNotThrown(get("http://localhost:8080/3", client) == "OK"); + + Daemon.shutdown(); + +} \ No newline at end of file