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