1 // This file is written in D programming language
2 /**
3 *   The example shows how to send signals to daemons created by
4 *   daemonize.
5 *
6 *   Copyright: © 2014 Anton Gushcha
7 *   License: Subject to the terms of the MIT license, as written in the included LICENSE file.
8 *   Authors: NCrashed <ncrashed@gmail.com>
9 */
10 module example02;
11 
12 import std.datetime;
13 
14 import dlogg.strict;
15 import daemonize.d;
16 
17 // Describing custom signals
18 enum RotateLogSignal = "RotateLog".customSignal;
19 enum DoSomethingSignal = "DoSomething".customSignal;
20 
21 version(DaemonServer)
22 {
23     // Full description for daemon side
24     alias daemon = Daemon!(
25         "DaemonizeExample2",
26         
27         KeyValueList!(
28             Composition!(Signal.Terminate, Signal.Quit, Signal.Shutdown, Signal.Stop), (logger)
29             {
30                 logger.logInfo("Exiting...");
31                 return false;
32             },
33             Signal.HangUp, (logger)
34             {
35                 logger.logInfo("Hello World!");
36                 return true;
37             },
38             RotateLogSignal, (logger)
39             {
40                 logger.logInfo("Rotating log!");
41                 logger.reload;
42                 return true;
43             },
44             DoSomethingSignal, (logger)
45             {
46                 logger.logInfo("Doing something...");
47                 return true;
48             }
49         ),
50         
51         (logger, shouldExit) 
52         {
53             // will stop the daemon in 5 minutes
54             auto time = Clock.currSystemTick + cast(TickDuration)5.dur!"minutes";
55             while(!shouldExit() && time > Clock.currSystemTick) {  }
56             logger.logInfo("Exiting main function!");
57             
58             return 0;
59         }
60     );
61     
62     int main()
63     {
64         // For windows is important to use absolute path for logging
65         version(Windows) string logFilePath = "C:\\logfile.log";
66         else string logFilePath = "logfile.log";
67 		
68         auto logger = new shared StrictLogger(logFilePath);
69         return buildDaemon!daemon.run(logger); 
70     }
71 }
72 version(DaemonClient)
73 {
74     import core.thread;
75     import core.time;
76     
77     // For client you don't need full description of the daemon
78     // the truncated description consists only of name and a list of
79     // supported signals
80     alias client = DaemonClient!(
81         "DaemonizeExample2",
82         Composition!(Signal.Terminate, Signal.Quit, Signal.Shutdown, Signal.Stop),
83         Signal.HangUp,
84         RotateLogSignal,
85         DoSomethingSignal
86     );
87     
88     void main()
89     {
90     	auto logger = new shared StrictLogger("client.log");
91     	
92     	alias daemon = buildDaemon!client;
93     	alias send = daemon.sendSignal;
94     	alias uninstall = daemon.uninstall;
95     	
96         send(logger, Signal.HangUp); 
97         Thread.sleep(50.dur!"msecs");
98         send(logger, RotateLogSignal); 
99         Thread.sleep(50.dur!"msecs");
100         send(logger, DoSomethingSignal);         
101         Thread.sleep(50.dur!"msecs");
102         send(logger, Signal.Terminate);
103         
104         // For windows services are remain in SC manager until uninstalled manually
105         version(Windows)
106         {
107         	Thread.sleep(500.dur!"msecs");
108         	uninstall(logger);
109     	}
110     }
111 }