public void PrepareForExecution() { FailedExperimentalUnits = new List <ExperimentalUnit>(); // tasks, inputs and outputs foreach (ExperimentalUnit experiment in ExperimentalUnits) { HerdTask task = new HerdTask(); // We are assuming the same exe file is used in all the experiments!!! // IMPORTANT //Because the experiment file might well be outside the RLSimion folder structure //we need make all paths to experiment files relative and let the herd agent //know that they must be renamed task.Name = experiment.Name; task.Exe = experiment.SelectedVersion.ExeFile; task.Arguments = experiment.RelativeExperimentFileName + " -pipe=" + experiment.Name; task.Pipe = experiment.Name; task.AuthenticationToken = "";// HerdAgent.AuthenticationCode; //TODO: Add Settings? Tasks.Add(task); //////App Version requirements //Exe file AddInputFile(task.Exe); //Rename rules AddRenameRules(experiment.SelectedVersion.Requirements.RenameRules); //Input files AddInputFiles(experiment.SelectedVersion.Requirements.InputFiles); AddInputFile(experiment.ExperimentFileName); //No output files come from the app version requirements in principle //////Run-time requirements //Input files AddInputFiles(experiment.RunTimeReqs.InputFiles); AddOutputFiles(experiment.RunTimeReqs.OutputFiles); /////Fix relative paths outside RLSimion's folder structure //we add rename rules: // -for the experiment file itself if (experiment.ExperimentFileName != experiment.RelativeExperimentFileName && !RenameRules.Keys.Contains(experiment.ExperimentFileName)) { RenameRules.Add(experiment.ExperimentFileName, experiment.RelativeExperimentFileName); } // -for the output files foreach (string outputFile in OutputFiles) { string renamedFile = Folders.experimentRelativeDir + "/" + Herd.Utils.RemoveDirectories(outputFile, 2); if (outputFile != renamedFile && !RenameRules.Keys.Contains(outputFile)) { RenameRules.Add(outputFile, renamedFile); } } } }
protected void SendTask(HerdTask task, CancellationToken cancelToken) { string taskXML = "<Task Name=\"" + task.Name + "\""; taskXML += " Exe=\"" + task.Exe + "\""; taskXML += " Arguments=\"" + task.Arguments + "\""; taskXML += " Pipe=\"" + task.Pipe + "\""; //taskXML += " AuthenticationToken=\"" + task.authenticationToken + "\""; taskXML += "/>"; byte[] bytes = Encoding.ASCII.GetBytes(taskXML); WriteAsync(bytes, 0, bytes.Length, cancelToken); }
public async Task <bool> ReceiveTask(CancellationToken cancelToken) { HerdTask task = new HerdTask(); Match match; //This expression was tested and worked fine with and without the authentication token match = await ReadUntilMatchAsync(TaskHeaderRegEx, 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; if (match.Groups.Count > 5) { task.AuthenticationToken = match.Groups[6].Value; } m_job.Tasks.Add(task); return(true); }
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); }