private CJob getJob() { CJob job = new CJob(); job.name = m_name; //tasks, inputs and outputs foreach (MonitoredExperimentViewModel experiment in m_monitoredExperiments) { CTask task = new CTask(); //we are assuming the same exe file is used in all the experiments!!! //IMPORTANT task.name = experiment.name; task.exe = experiment.exeFile; task.arguments = experiment.filePath + " " + experiment.pipeName; task.pipe = experiment.pipeName; job.tasks.Add(task); //add EXE files if (!job.inputFiles.Contains(task.exe)) job.inputFiles.Add(task.exe); //add prerrequisites foreach (string pre in experiment.prerrequisites) if (!job.inputFiles.Contains(pre)) job.inputFiles.Add(pre); //add experiment file to inputs if (!job.inputFiles.Contains(experiment.filePath)) job.inputFiles.Add(experiment.filePath); Utility.getInputsAndOutputs(experiment.filePath, ref job); } return job; }
protected void SendTask(CTask task, CancellationToken cancelToken) { string taskXML = "<Task Name=\"" + task.name + "\""; taskXML += " Exe=\"" + task.exe + "\""; taskXML += " Arguments=\"" + task.arguments + "\""; taskXML += " Pipe=\"" + task.pipe + "\""; taskXML += "/>"; byte[] bytes = Encoding.ASCII.GetBytes(taskXML); writeAsync(bytes, 0, bytes.Length, cancelToken); }
public async Task <bool> ReceiveTask(CancellationToken cancelToken) { CTask task = new CTask(); Match match; match = await ReadUntilMatchAsync("<Task Name=\"([^\"]*)\" Exe=\"([^\"]*)\" Arguments=\"([^\"]*)\" Pipe=\"([^\"]*)\"/>", cancelToken); task.name = match.Groups[1].Value; task.exe = match.Groups[2].Value; task.arguments = match.Groups[3].Value; task.pipe = match.Groups[4].Value; m_job.tasks.Add(task); return(true); }
public async Task <int> runTaskAsync(CTask task, CancellationToken cancelToken) { int returnCode = m_noErrorCode; NamedPipeServerStream pipeServer = null; Process myProcess = new Process(); if (task.pipe != "") { pipeServer = new NamedPipeServerStream(task.pipe); } try { myProcess.StartInfo.FileName = getCachedFilename(task.exe); myProcess.StartInfo.Arguments = task.arguments; myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(myProcess.StartInfo.FileName); logMessage("Running command: " + myProcess.StartInfo.FileName + " " + myProcess.StartInfo.Arguments); //not to read 23.232 as 23232 Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; myProcess.Start(); //addSpawnedProcessToList(myProcess); XMLStream xmlStream = new XMLStream(); string xmlItem; if (pipeServer != null) { pipeServer.WaitForConnection(); while (pipeServer.IsConnected) { //check if the herd agent sent us a quit message //in case it did, the function will cancel the cancellation token checkCancellationRequests(); //check if we have been asked to cancel cancelToken.ThrowIfCancellationRequested(); int numBytes = await xmlStream.readFromNamedPipeStreamAsync(pipeServer, cancelToken); xmlItem = xmlStream.processNextXMLItem(); while (xmlItem != "") { await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + ">" + xmlItem + "</" + task.pipe + ">" , cancelToken, false); xmlItem = xmlStream.processNextXMLItem(); } } } logMessage("Named pipe has been closed for task " + task.name); await waitForExitAsync(myProcess, cancelToken); int exitCode = myProcess.ExitCode; logMessage("Process exited in task " + task.name + ". Return code=" + exitCode); //myProcess.WaitForExit(); if (exitCode < 0) { await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + "><End>Error</End></" + task.pipe + ">" , cancelToken, false); returnCode = m_jobInternalErrorCode; } else { await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + "><End>Ok</End></" + task.pipe + ">" , cancelToken, false); } logMessage("Exit code: " + myProcess.ExitCode); } catch (OperationCanceledException) { logMessage("Thread finished gracefully"); if (myProcess != null) { myProcess.Kill(); } returnCode = m_remotelyCancelledErrorCode; } catch (Exception ex) { logMessage("unhandled exception in runTaskAsync()"); logMessage(ex.ToString()); if (myProcess != null) { myProcess.Kill(); } returnCode = m_jobInternalErrorCode; } finally { logMessage("Task " + task.name + " finished"); //removeSpawnedProcessFromList(myProcess); if (pipeServer != null) { pipeServer.Close(); } } return(returnCode); }
public async Task<bool> ReceiveTask(CancellationToken cancelToken) { CTask task = new CTask(); Match match; match= await ReadUntilMatchAsync("<Task Name=\"([^\"]*)\" Exe=\"([^\"]*)\" Arguments=\"([^\"]*)\" Pipe=\"([^\"]*)\"/>",cancelToken); task.name = match.Groups[1].Value; task.exe = match.Groups[2].Value; task.arguments = match.Groups[3].Value; task.pipe = match.Groups[4].Value; m_job.tasks.Add(task); return true; }
protected void SendTask(CTask task,CancellationToken cancelToken) { string taskXML = "<Task Name=\"" + task.name + "\""; taskXML += " Exe=\"" + task.exe + "\""; taskXML += " Arguments=\"" + task.arguments + "\""; taskXML += " Pipe=\"" + task.pipe + "\""; taskXML += "/>"; byte[] bytes= Encoding.ASCII.GetBytes(taskXML); writeAsync(bytes, 0, bytes.Length,cancelToken); }
public async Task<int> runTaskAsync(CTask task, CancellationToken cancelToken) { int returnCode= m_noErrorCode; NamedPipeServerStream pipeServer = null; Process myProcess = new Process(); if (task.pipe != "") pipeServer = new NamedPipeServerStream(task.pipe); try { myProcess.StartInfo.FileName = getCachedFilename(task.exe); myProcess.StartInfo.Arguments = task.arguments; myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(myProcess.StartInfo.FileName); logMessage("Running command: " + myProcess.StartInfo.FileName + " " + myProcess.StartInfo.Arguments); //not to read 23.232 as 23232 Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; myProcess.Start(); //addSpawnedProcessToList(myProcess); XMLStream xmlStream = new XMLStream(); string xmlItem; if (pipeServer != null) { pipeServer.WaitForConnection(); while (pipeServer.IsConnected) { //check if the herd agent sent us a quit message //in case it did, the function will cancel the cancellation token checkCancellationRequests(); //check if we have been asked to cancel cancelToken.ThrowIfCancellationRequested(); int numBytes= await xmlStream.readFromNamedPipeStreamAsync(pipeServer,cancelToken); xmlItem = xmlStream.processNextXMLItem(); while (xmlItem != "") { await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + ">" + xmlItem + "</" + task.pipe + ">" , cancelToken, false); xmlItem = xmlStream.processNextXMLItem(); } } } logMessage("Named pipe has been closed for task " + task.name); await waitForExitAsync(myProcess,cancelToken); int exitCode = myProcess.ExitCode; logMessage("Process exited in task " + task.name + ". Return code=" + exitCode); //myProcess.WaitForExit(); if (exitCode < 0) { await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + "><End>Error</End></" + task.pipe + ">" , cancelToken,false); returnCode = m_jobInternalErrorCode; } else await xmlStream.writeMessageAsync(m_tcpClient.GetStream(), "<" + task.pipe + "><End>Ok</End></" + task.pipe + ">" , cancelToken,false); logMessage("Exit code: " + myProcess.ExitCode); } catch (OperationCanceledException) { logMessage("Thread finished gracefully"); if (myProcess!=null) myProcess.Kill(); returnCode = m_remotelyCancelledErrorCode; } catch(Exception ex) { logMessage("unhandled exception in runTaskAsync()"); logMessage(ex.ToString()); if (myProcess != null) myProcess.Kill(); returnCode = m_jobInternalErrorCode; } finally { logMessage("Task " + task.name + " finished"); //removeSpawnedProcessFromList(myProcess); if (pipeServer!=null) pipeServer.Close(); } return returnCode; }