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.procjob; 24 25 private import dxx.sys.spawn; 26 private import dxx.app; 27 private import dxx.util; 28 private import dxx; 29 30 /** 31 * This class defines a notifying job that starts a native process. 32 */ 33 class ProcessJob : PlatformJobBase { 34 35 static class StdIOWriter : TextWriter { 36 string prefix; 37 shared 38 this(string p) { 39 prefix = p; 40 } 41 void writeText(dstring text) { 42 MsgLog.info(MsgParam!("[%s]: %s")(prefix,text)); 43 } 44 } 45 string cmd; 46 string[] param; 47 string result; 48 49 protected ExternalProcess proc; 50 protected TextWriter stdErrWriter; 51 protected TextWriter stdOutWriter; 52 53 override shared 54 void setup() { 55 super.setup; 56 } 57 58 override shared 59 void processPlatformJob() { 60 (cast(ProcessJob)this).shell; 61 } 62 63 nothrow 64 override shared 65 void terminate() { 66 if(proc) { 67 try { 68 (cast(ExternalProcess)proc).wait; 69 //enforce(proc.state == ExternalProcessState.Stopped); 70 } catch(Throwable e) { 71 MsgLog.error(e.message); 72 } 73 } 74 super.terminate; 75 } 76 shared 77 this(string cmd,string[] param) { 78 super(); 79 this.cmd = cmd; 80 this.param = (cast(shared(string[]))param).dup; 81 this.stdOutWriter = new shared(StdIOWriter)(cmd); 82 this.stdErrWriter = new shared(StdIOWriter)(cmd ~ ":err"); 83 this.proc = cast(shared(ExternalProcess))new ExternalProcess(); 84 } 85 auto shell() { 86 auto exec = cmd.findExecutablePath; 87 //auto stdOutWriter = new ProtectedTextStorage(); 88 //auto stdOutWriter = new Logg7ingWriter(cmd); 89 return proc.run(exec,param,runtimeConstants.curDir,stdOutWriter,stdErrWriter); 90 //proc.wait(); 91 //enforce(proc.state == ExternalProcessState.Stopped); 92 //enforce(proc.result == 0); 93 //return proc.result; 94 //return stdOutWriter.readText(); 95 } 96 } 97 98 unittest { 99 import std.stdio; 100 auto j = new shared(ProcessJob)("ls",["-lh"]); 101 j.execute(); 102 assert(j.terminated); 103 writeln("Job status: ",j.status); 104 assert(j.status == Job.Status.THROWN_EXCEPTION); 105 assert(j.thrownException !is null); 106 }