1 // This file is written in D programming language
2 /**
3 *   The example demonstrates basic daemonize features. Described
4 *   daemon responds to SIGTERM and SIGHUP signals.
5 *
6 *   If SIGTERM is received, daemon terminates. If SIGHUP is received,
7 *   daemon prints "Hello World!" message to logg.
8 *
9 *   Daemon will auto-terminate after 5 minutes of running.
10 *
11 *   Copyright: © 2014 Anton Gushcha
12 *   License: Subject to the terms of the MIT license, as written in the included LICENSE file.
13 *   Authors: NCrashed <ncrashed@gmail.com>
14 */
15 module example01;
16 
17 static if( __VERSION__ < 2075) import std.datetime;
18 else import core.time;
19 
20 import daemonize.d;
21 
22 // First you need to describe your daemon via template
23 alias daemon = Daemon!(
24     "DaemonizeExample1", // unique name
25 
26     // Setting associative map signal -> callbacks
27     KeyValueList!(
28         // You can bind same delegate for several signals by Composition template
29         // delegate can take additional argument to know which signal is caught
30         Composition!(Signal.Terminate, Signal.Quit, Signal.Shutdown, Signal.Stop), (logger, signal)
31         {
32             logger.logInfo("Exiting...");
33             return false; // returning false will terminate daemon
34         },
35         Composition!(Signal.HangUp,Signal.Pause,Signal.Continue), (logger)
36         {
37             logger.logInfo("Hello World!");
38             return true; // continue execution
39         }
40     ),
41 
42     // Main function where your code is
43     (logger, shouldExit) {
44         // will stop the daemon in 5 minutes
45         static if( __VERSION__ < 2075)
46         {
47             auto time = Clock.currSystemTick + cast(TickDuration)5.dur!"minutes";
48             alias currTime = Clock.currSystemTick;
49         }
50         else
51         {
52             auto time = MonoTime.currTime + 5.dur!"minutes";
53             alias currTime = MonoTime.currTime;
54         }
55         while(!shouldExit() && time > currTime) {  }
56 
57         logger.logInfo("Exiting main function!");
58 
59         return 0;
60     }
61 );
62 
63 int main()
64 {
65     // For windows is important to use absolute path for logging
66     version(Windows) string logFilePath = "C:\\logfile.log";
67     else string logFilePath = "logfile.log";
68 
69     return buildDaemon!daemon.run(new shared DloggLogger(logFilePath));
70 }