Exemple #1
0
        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);
        }
Exemple #2
0
        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;
        }