1 /**
2 Copyright 2018 Mark Fisher
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 **/
22 module dxx.app.job;
23 
24 private import core.thread;
25 
26 private import dxx.util;
27 private import dxx.app;
28 
29 interface Job : NotificationSource {
30     enum Status {
31         NOT_STARTED,
32         STARTED,
33         PAUSED,
34         TERMINATED,
35         THROWN_EXCEPTION
36     }
37     struct JobStatusEvent {
38       Job job;
39       Status status;
40     }
41 
42     @property pure @safe nothrow @nogc
43     const(Status) status() const;
44 
45     @property pure @safe nothrow @nogc
46     bool terminated() const;
47 
48     @property pure @safe nothrow @nogc
49     ref inout(Exception) thrownException() inout;
50 
51     void execute();
52     //void join();
53 	//void setProperty(string k,string v);
54 	//string getProperty(string k);
55 }
56 
57 abstract class JobBase : SyncNotificationSource, Job {
58     Status _status = Status.NOT_STARTED;
59     Exception _thrownException;
60     bool _terminated = false;
61     DefaultInjector _injector;
62     
63     @property pure @safe nothrow @nogc
64     const(Status) status() const {
65         return _status;
66     }
67     @property
68     void status(Status s) {
69       _status = s;
70       auto e = JobStatusEvent(this,s);
71       (cast(shared)this).send!JobStatusEvent(&e);
72     }
73     @property pure @safe nothrow @nogc
74     bool terminated() const {
75         return _terminated;
76     }
77     @property @safe nothrow
78     ref inout(Exception) thrownException() inout {
79         return _thrownException;
80     }
81     @property @safe nothrow
82     ref inout(DefaultInjector) injector() inout {
83     	return _injector;
84     }
85 
86     nothrow
87     void execute() {
88         try {
89         	_injector = RuntimeModule.injector; 
90             status(Status.STARTED);
91             executeJob();
92             status = Status.TERMINATED;
93         } catch(Exception e) {
94             _thrownException = e;
95             try {
96               MsgLog.warning(e.message);
97               status = Status.THROWN_EXCEPTION;
98             } catch(Exception _e) {
99               MsgLog.error(_e.message);
100             } finally {}
101         } finally {
102             _terminated = true;
103         }
104     }
105 
106     static void join(const(Job) j) {
107         while(!j.terminated) {
108             Thread.sleep( dur!("msecs")( 10 ) );
109         }
110     }
111     abstract void executeJob();
112 //	override void setProperty(T)(string k,T v) {
113 //		injector.setProperty(v,k);
114 //	}    
115 //	override T getProperty(T)(string k) {
116 //		return injector.getProperty!T(k);
117 //	}   
118 	//override 
119 	void setProperty(string k,string v) {
120 		injector.register!string(v,k);
121 	}    
122 	//override 
123 	string getProperty(string k) {
124 		return injector.resolve!string(k);
125 	}   
126 }
127 
128 class JobDelegate(alias F) : JobBase {
129     override void executeJob() {
130         F();
131     }
132 }