public async Task <int> RunTaskAsync(HerdTask task, CancellationToken cancelToken) { int returnCode = m_noErrorCode; NamedPipeServerStream pipeServer = null; Process myProcess = new Process(); string actualPipeName = task.Pipe; if (task.Pipe != "") { if (CPUArchitecture == PropValues.Linux32 || CPUArchitecture == PropValues.Linux64) { //we need to prepend the name of the pipe with "/tmp/" to be accesible to the client process actualPipeName = "/tmp/" + task.Pipe; } pipeServer = new NamedPipeServerStream(actualPipeName); } XMLStream xmlStream = new XMLStream(); try { //not to read 23.232 as 23232 Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; myProcess.StartInfo.FileName = getCachedFilename(task.Exe); myProcess.StartInfo.Arguments = task.Arguments; myProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(myProcess.StartInfo.FileName); myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.UseShellExecute = false; if (myProcess.Start()) { LogToFile("Running command: " + myProcess.StartInfo.FileName + " " + myProcess.StartInfo.Arguments); } else { LogToFile("Error running command: " + myProcess.StartInfo.FileName + " " + myProcess.StartInfo.Arguments); return(m_jobInternalErrorCode); } string xmlItem; if (pipeServer != null) { bool clientEnded = false; pipeServer.WaitForConnection(); while (pipeServer.IsConnected && !clientEnded) { //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 != "" && !clientEnded) { if (xmlItem != "<End></End>") { await xmlStream.WriteMessageAsync(m_tcpClient.GetStream(), "<" + task.Pipe + ">" + xmlItem + "</" + task.Pipe + ">", cancelToken); xmlItem = xmlStream.processNextXMLItem(); } else { clientEnded = true; } } } } LogMessage("Task ended: " + task.Name + ". Waiting for process to end"); if (!myProcess.HasExited) { await WaitForExitAsync(myProcess, cancelToken); } int exitCode = myProcess.ExitCode; LogMessage("Process exited in task " + task.Name + ". Return code=" + exitCode); if (exitCode < 0) { await xmlStream.WriteMessageAsync(m_tcpClient.GetStream(), "<" + task.Pipe + "><End>Error</End></" + task.Pipe + ">", cancelToken); returnCode = m_jobInternalErrorCode; } else { await xmlStream.WriteMessageAsync(m_tcpClient.GetStream(), "<" + task.Pipe + "><End>Ok</End></" + task.Pipe + ">", cancelToken); } LogMessage("Exit code: " + myProcess.ExitCode); } catch (OperationCanceledException) { LogMessage("Thread finished gracefully"); if (myProcess != null) { myProcess.Kill(); } returnCode = m_remotelyCancelledErrorCode; } catch (AuthenticationException ex) { LogMessage(ex.ToString()); } 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"); if (pipeServer != null) { pipeServer.Dispose(); } } return(returnCode); }
public async Task WriteMessageAsync(string message, CancellationToken cancelToken, bool addDefaultMessageType = false) { await m_xmlStream.WriteMessageAsync(m_netStream, message, cancelToken, addDefaultMessageType); }