20
|
1 /* LICENSE BLOCK
|
|
2 Part of mde: a Modular D game-oriented Engine
|
|
3 Copyright © 2007-2008 Diggory Hardy
|
|
4
|
|
5 This program is free software; you can redistribute it and/or modify it under the terms of
|
|
6 the GNU General Public License, version 2, as published by the Free Software Foundation.
|
|
7
|
|
8 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
10 See the GNU General Public License for more details.
|
|
11
|
|
12 You should have received a copy of the GNU General Public License along
|
|
13 with this program; if not, write to the Free Software Foundation, Inc.,
|
|
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
|
|
15
|
|
16 /**************************************************************************************************
|
|
17 * Initialisation setup and exit cleanup module.
|
|
18 *
|
|
19 * This module provides an infrastructure for handling much of the initialisation and
|
|
20 * deinitialisation of the program. It does not, however, provide much of the (de)initialisation
|
|
21 * code; with the exception of that for the logger.
|
|
22 *************************************************************************************************/
|
|
23 module mde.scheduler.Init;
|
|
24
|
|
25 import mde.scheduler.InitStage;
|
|
26 import mde.scheduler.exception;
|
|
27
|
|
28 import mde.options;
|
|
29 import paths = mde.resource.paths;
|
|
30 import mde.exception;
|
|
31
|
|
32 // tango imports
|
|
33 import tango.core.Thread;
|
|
34 import tango.core.Exception;
|
|
35 import tango.stdc.stringz : fromStringz;
|
|
36 import tango.util.log.Log : Log, Logger;
|
|
37 import tango.util.log.ConsoleAppender : ConsoleAppender;
|
|
38
|
|
39 version = SwitchAppender;
|
|
40 version (SwitchAppender) { // My own variation, currently just a test
|
|
41 import tango.util.log.SwitchingFileAppender : SwitchingFileAppender;
|
|
42 } else {
|
|
43 import tango.util.log.RollingFileAppender : RollingFileAppender;
|
|
44 }
|
|
45
|
|
46 /**
|
|
47 * Static CTOR
|
|
48 *
|
|
49 * This should handle a minimal amount of functionality where useful. For instance, configuring the
|
|
50 * logger here and not in Init allows unittests to use the logger.
|
|
51 */
|
|
52 static this()
|
|
53 {
|
|
54 // Find/create paths:
|
|
55 try {
|
|
56 paths.resolvePaths();
|
|
57 } catch (Exception e) {
|
|
58 throw new InitException ("Resolving paths failed: " ~ e.msg);
|
|
59 }
|
|
60
|
|
61 // Set up the logger:
|
|
62 {
|
|
63 // Where logging is done to is determined at compile-time, currently just via static ifs.
|
|
64 Logger root = Log.getRootLogger();
|
|
65
|
|
66 static if (true ) { // Log to the console
|
|
67 root.addAppender(new ConsoleAppender);
|
|
68 }
|
|
69 static if (true ) { // Log to files
|
|
70 version (SwitchAppender) {
|
|
71 root.addAppender (new SwitchingFileAppender (paths.logDir~"/log-.txt", 5));
|
|
72 } else {
|
|
73 // Use 2 log files with a maximum size of 1 MB:
|
|
74 root.addAppender (new RollingFileAppender (paths.logDir~"/log-.txt", 2, 1024*1024));
|
|
75 }
|
|
76 }
|
|
77
|
|
78 // Set the level here, but set it again once options have been loaded:
|
|
79 debug root.setLevel(root.Level.Trace);
|
|
80 else root.setLevel(root.Level.Info);
|
|
81 }
|
|
82 }
|
|
83 static ~this()
|
|
84 {
|
|
85 }
|
|
86
|
|
87 // Global instance, since called init functions need to interact:
|
|
88 scope Init init;
|
|
89
|
|
90 /**
|
|
91 * Init class
|
|
92 *
|
|
93 * A scope class created at beginning of the program and destroyed at the end; thus the CTOR
|
|
94 * handles program initialisation and the DTOR handles program cleanup.
|
|
95 */
|
|
96 scope class Init
|
|
97 {
|
|
98 private static Logger logger;
|
|
99 static this() {
|
|
100 logger = Log.getLogger ("mde.scheduler.Init.Init");
|
|
101 }
|
|
102
|
|
103 /** CTOR − initialisation
|
|
104 *
|
|
105 * Runs general initialisation code, in a threaded manner where this isn't difficult.
|
|
106 *
|
|
107 * If any init fails, it must run necessary cleanup first since the DTOR cannot(?) be run. */
|
|
108 /* In a single-threaded function this could be done with:
|
|
109 * scope(failure) cleanup;
|
|
110 * This won't work with a threaded init function since any threads completing succesfully will
|
|
111 * not clean-up, and a fixed list of clean-up functions cannot be used since clean-up functions
|
|
112 * must not run where the initialisation functions have failed.
|
|
113 * Hence a list of clean-up functions is built similarly to scope(failure) --- see addCleanupFct.
|
|
114 */
|
|
115 this()
|
|
116 {
|
|
117 logger.trace ("Init: starting");
|
|
118
|
|
119 //BEGIN Pre-init (stage init0)
|
|
120 /* Load options now. Don't load in a thread since:
|
|
121 * Loading should be fast
|
|
122 * Most work is probably disk access
|
|
123 * It's a really good idea to let the options apply to all other loading. */
|
|
124 try {
|
|
125 Options.load();
|
|
126 } catch (optionsLoadException e) {
|
|
127 throw new InitException ("Loading options failed; message: " ~ e.msg);
|
|
128 }
|
|
129
|
|
130 // Now re-set the logging level:
|
|
131 Log.getRootLogger.setLevel (cast(Log.Level) Options.misc.logLevel, true); // set the stored log level
|
|
132 //END Pre-init
|
|
133
|
|
134
|
|
135 //BEGIN Init (stages init2, init4)
|
|
136 /* Call init functions.
|
|
137 *
|
|
138 * Current method is to try using threads, and on failure assume no threads were actually
|
|
139 * created and run functions in a non-threaded manner. */
|
|
140
|
|
141 try { // init2
|
|
142 cleanupStages ~= &cleanup2; // add appropriate cleanup stage
|
|
143
|
|
144 if (runStageThreaded (init2)) runStageForward (init2);
|
|
145 }
|
|
146 catch (InitStageException) { // This init stage failed.
|
|
147 runCleanupStages();
|
|
148 throw new InitException ("Initialisation failed during stage init2");
|
|
149 }
|
|
150
|
|
151 try { // init4
|
|
152 cleanupStages ~= &cleanup4; // add appropriate cleanup stage
|
|
153
|
|
154 if (runStageThreaded (init4)) runStageForward (init4);
|
|
155 }
|
|
156 catch (InitStageException) { // This init stage failed.
|
|
157 runCleanupStages();
|
|
158 throw new InitException ("Initialisation failed during stage init4");
|
|
159 }
|
|
160 //END Init
|
|
161
|
|
162 logger.trace ("Init: done");
|
|
163 }
|
|
164
|
|
165 /** DTOR - runs cleanup functions. */
|
|
166 ~this()
|
|
167 {
|
|
168 logger.trace ("Cleanup: starting");
|
|
169
|
|
170 // cleanup1:
|
|
171 Options.save(); // save options... do so here for now
|
|
172
|
|
173 // cleanup2, 4:
|
|
174 runCleanupStages();
|
|
175
|
|
176 logger.trace ("Cleanup: done");
|
|
177 }
|
|
178
|
|
179
|
|
180 //BEGIN runStage...
|
|
181 private static {
|
|
182 /* The following three functions, runStage*, each run all functions in a stage in some order,
|
|
183 * catching any exceptions thrown by the functions (although this isn't guaranteed for threads),
|
|
184 * and throw an InitStageException on failure. */
|
|
185
|
|
186 const UFE = "Unhandled exception from Init function:";
|
|
187 /* Runs all functions consecutively, first-to-last.
|
|
188 * If any function fails, halts immediately. */
|
|
189 void runStageForward (InitStage s) {
|
|
190 foreach (func; s.funcs) {
|
|
191 if (s.failure) break;
|
|
192 try {
|
|
193 func();
|
|
194 } catch (Exception e) {
|
|
195 logger.fatal (UFE);
|
|
196 logger.fatal (e.msg);
|
|
197
|
|
198 s.setFailure();
|
|
199 }
|
|
200 }
|
|
201 if (s.failure) throw new InitStageException; // Problem running; abort and cleanup from here.
|
|
202 }
|
|
203 /* Runs all functions consecutively, last-to-first.
|
|
204 * If any function fails, continue until all have been run. */
|
|
205 void runStageReverse (InitStage s) {
|
|
206 foreach_reverse (func; s.funcs) {
|
|
207 try {
|
|
208 func();
|
|
209 } catch (Exception e) {
|
|
210 logger.fatal (UFE);
|
|
211 logger.fatal (e.msg);
|
|
212
|
|
213 s.setFailure();
|
|
214 }
|
|
215 }
|
|
216 if (s.failure) throw new InitStageException; // Problem running; abort and cleanup from here.
|
|
217 }
|
|
218 /* Tries running functions in a threaded way. Returns false if successful, true if not but
|
|
219 * functions should be run without threads. */
|
|
220 bool runStageThreaded (InitStage s) {
|
|
221 if (!Options.misc.useThreads) return true; // Use unthreaded route instead
|
|
222
|
|
223 ThreadGroup tg;
|
|
224 try { // creating/starting threads could fail
|
|
225 tg = new ThreadGroup;
|
|
226 foreach (func; s.funcs) tg.create(func); // Start all threads
|
|
227 } catch (ThreadException e) { // Problem with threading; try without threads
|
|
228 logger.warn ("Caught ThreadException while trying to create threads:");
|
|
229 logger.warn (e.msg);
|
|
230 logger.warn ("Will continue in a non-threaded manner.");
|
|
231
|
|
232 Options.misc.useThreads = false; // Disable threads entirely
|
|
233 return true; // Try again without threads
|
|
234 }
|
|
235
|
|
236 /* Wait for all threads to complete.
|
|
237 *
|
|
238 * If something went wrong, we still need to do this before cleaning up.
|
|
239 */
|
|
240 foreach (t; tg) {
|
|
241 try {
|
|
242 t.join (true);
|
|
243 } catch (Exception e) {
|
|
244 // Relying on catching exceptions thrown by other threads is a bad idea.
|
|
245 // Hence all threads should catch their own exceptions and return safely.
|
|
246
|
|
247 logger.fatal ("Unhandled exception from Init function:");
|
|
248 logger.fatal (e.msg);
|
|
249
|
|
250 s.setFailure (); // abort (but join other threads first)
|
|
251 }
|
|
252 }
|
|
253
|
|
254 if (s.failure) throw new InitStageException; // Problem running; abort and cleanup from here.
|
|
255 return false; // Done successfully
|
|
256 }
|
|
257 }
|
|
258 //END runStage...
|
|
259
|
|
260 private {
|
|
261 InitStage*[] cleanupStages; // All cleanup stages needing to be run later.
|
|
262 void runCleanupStages () {
|
|
263 foreach_reverse (s; cleanupStages) {
|
|
264 try {
|
|
265 runStageReverse (*s);
|
|
266 }
|
|
267 catch (InitStageException) {
|
|
268 // We're cleaning up anyway, just report and continue
|
|
269 logger.error ("Cleanup function failed! Continuing...");
|
|
270 }
|
|
271 }
|
|
272 cleanupStages = []; // All have been run, don't want them getting run again
|
|
273 }
|
|
274 }
|
|
275
|
|
276 debug (mdeUnitTest) unittest {
|
|
277 /* Fake init and cleanup. Use unittest-specific init and cleanup InitStages to avoid
|
|
278 * messing other init/cleanup up. */
|
|
279 static InitStage initUT, cleanupUT;
|
|
280
|
|
281 static bool initialised = false;
|
|
282 static void cleanupFunc1 () {
|
|
283 initialised = false;
|
|
284 }
|
|
285 static void cleanupFunc2 () {
|
|
286 assert (initialised == true);
|
|
287 }
|
|
288
|
|
289 static void initFunc () {
|
|
290 initialised = true;
|
|
291 cleanupUT.addFunc (&cleanupFunc1);
|
|
292 cleanupUT.addFunc (&cleanupFunc2);
|
|
293 }
|
|
294
|
|
295 initUT.addFunc (&initFunc);
|
|
296
|
|
297 runStageForward (initUT);
|
|
298 assert (initialised);
|
|
299
|
|
300 runStageReverse (cleanupUT);
|
|
301 assert (!initialised);
|
|
302
|
|
303 logger.info ("Unittest complete.");
|
|
304 }
|
|
305 }
|