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 std.exception; 25 26 private import core.thread; 27 28 private import dxx.util; 29 private import dxx.app; 30 31 interface Job : NotificationSource { 32 enum Status { 33 NOT_STARTED, 34 STARTED, 35 SUSPENDED, 36 TERMINATED, 37 THROWN_EXCEPTION 38 } 39 struct JobStatusEvent { 40 Job job; 41 Status status; 42 } 43 44 @property pure @safe nothrow @nogc 45 const(Status) status() const; 46 47 @property pure @safe nothrow @nogc 48 bool terminated() const; 49 50 @property pure @safe nothrow @nogc 51 ref inout(Exception) thrownException() inout; 52 53 nothrow 54 void execute(InjectionContainer injector=InjectionContainer.getInstance); 55 56 //void join(); 57 //void setProperty(string k,string v); 58 //string getProperty(string k); 59 } 60 61 abstract class JobBase : SyncNotificationSource, Job { 62 Status _status = Status.NOT_STARTED; 63 Exception _thrownException; 64 InjectionContainer _injector; 65 66 @property pure @safe nothrow @nogc 67 const(Status) status() const { 68 return _status; 69 } 70 @property nothrow 71 void status(Status s) { 72 _status = s; 73 auto e = JobStatusEvent(this,s); 74 (cast(shared)this).send!JobStatusEvent(&e); 75 } 76 @property pure @safe nothrow @nogc 77 bool terminated() const { 78 return (status == Status.TERMINATED) || (status == Status.THROWN_EXCEPTION); 79 } 80 @property @safe nothrow 81 ref inout(Exception) thrownException() inout { 82 return _thrownException; 83 } 84 @property @safe nothrow 85 ref inout(InjectionContainer) injector() inout { 86 return _injector; 87 } 88 89 nothrow 90 void execute(InjectionContainer injector=InjectionContainer.getInstance) { 91 try { 92 enforce(_status == Status.NOT_STARTED); 93 _injector = injector; 94 setup; 95 status(Status.STARTED); 96 process; 97 status = Status.TERMINATED; 98 } catch(Exception e) { 99 MsgLog.warning("Exception: " ~ e.message); 100 _thrownException = e; 101 status = Status.THROWN_EXCEPTION; 102 } finally { 103 terminate; 104 } 105 } 106 107 static void join(const(Job) j) { 108 while(!j.terminated) { 109 Thread.sleep( dur!("msecs")( 10 ) ); 110 } 111 } 112 113 void setup() {} 114 115 abstract void process(); 116 117 nothrow void terminate() {} 118 119 void setProperty(T)(string k,T v) { 120 injector.setParam(v,k); 121 } 122 123 T getProperty(T)(string k) { 124 return injector.getParm(k); 125 } 126 127 this() { 128 } 129 } 130 131 class JobDelegate(alias F) : JobBase { 132 override void process() { 133 F(); 134 } 135 }