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.workflow; 23 24 private import std.algorithm; 25 26 private import dxx.app.job; 27 28 interface WorkflowElement { 29 void processElement(WorkflowJob job); 30 } 31 32 //abstract class WorkflowElementBase : WorkflowElement { 33 // override void process(WorkflowJob job) { 34 // processElement(job); 35 // } 36 // abstract void processElement(WorkflowJob job); 37 //} 38 39 interface Workflow { 40 @property 41 ref inout (WorkflowElement[]) workflowElements() inout; 42 43 @property 44 ref inout (string[]) args() inout; 45 } 46 47 abstract class WorkflowBase : Workflow { 48 WorkflowElement[] _workflowElements; 49 string[] _args; 50 51 @property 52 ref inout (WorkflowElement[]) workflowElements() inout { 53 return _workflowElements; 54 } 55 @property 56 ref inout (string[]) args() inout { 57 return _args; 58 } 59 this(WorkflowElement[] elements,string[] args) { 60 _workflowElements = elements; 61 _args = args; 62 } 63 } 64 65 final class DefaultWorkflow : WorkflowBase { 66 this(WorkflowElement[] elements,string[] args) { 67 super(elements,args); 68 } 69 } 70 71 final class WorkflowJob : JobBase { 72 Workflow _workflow; 73 this(Workflow wf) { 74 this._workflow = wf; 75 } 76 @property 77 inout(Workflow) workflow() inout { 78 return _workflow; 79 } 80 override void executeJob() { 81 workflow.workflowElements.each!(e=>e.processElement(this)); 82 } 83 } 84 85 final class WorkflowRunner { 86 Job createJob(Workflow wf) { 87 auto job = new WorkflowJob(wf); 88 return job; 89 } 90 } 91 92 unittest { 93 import std.stdio; 94 95 class TestWorkflowElement : WorkflowElement { 96 bool _done = false; 97 override void processElement(WorkflowJob job) { 98 writeln("TestWorkflowElement.processElement"); 99 _done = true; 100 } 101 } 102 string[] arg = [ "arg0","arg1","arg2" ]; 103 104 auto elem = new TestWorkflowElement; 105 WorkflowElement[] e = [ elem ]; 106 auto wf = new DefaultWorkflow(e,arg); 107 auto r = new WorkflowRunner; 108 auto j = r.createJob(wf); 109 j.execute(); 110 assert(j.terminated); 111 assert(j.status == Job.Status.TERMINATED); 112 assert(elem._done); 113 114 } 115 116 unittest { 117 import std.stdio; 118 class TestWorkflowElementException : WorkflowElement { 119 override void processElement(WorkflowJob job) { 120 writeln("TestWorkflowElementException.processElement"); 121 throw new Exception("workflow unittest"); 122 } 123 } 124 string[] arg = [ "arg0","arg1","arg2" ]; 125 126 auto elem = new TestWorkflowElementException; 127 WorkflowElement[] e = [ elem ]; 128 auto wf = new DefaultWorkflow(e,arg); 129 auto r = new WorkflowRunner; 130 auto j = r.createJob(wf); 131 j.execute(); 132 assert(j.terminated); 133 assert(j.status == Job.Status.THROWN_EXCEPTION); 134 assert(j.thrownException !is null); 135 } 136