예제 #1
0
        public CommandResult Execute(string fileName, string args)
        {
            lock (_executingLock)
            {
                if (isDisposed)
                {
                    throw new ObjectDisposedException(nameof(HestExecutor));
                }

                //记录开始时间
                DateTime start = DateTime.Now;
                //开始进程
                commandProcedure = BasicBooter.CommandProcedureManager.OpenCommand(fileName, args);
                commandProcedure.OutputReceived += OnOutputReceived;

                //触发事件
                CommandExecuting?.Invoke(this, new CommandExecutingEventArgs(fileName, args));
                var cmdResult = commandProcedure.Execute();
                commandProcedure.Dispose();
                //记录结束时间
                DateTime end = DateTime.Now;

                //构造结果对象
                var result = new CommandResult(cmdResult.ExitCode, cmdResult.Output);

                //触发结束事件
                CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(fileName, args, result, end - start));

                //返回结果
                return(result);
            };
        }
예제 #2
0
 /// <summary>
 /// 无锁的执行..不建议覆写
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 protected virtual Result ExecuteWithoutLock(string fileName, string args)
 {
     if (disposedValue)
     {
         throw new ObjectDisposedException(nameof(CommandExecutor));
     }
     int exitCode;
     ProcessStartInfo pStartInfo;
     outputBuilder.Clear();
     pStartInfo = GetStartInfo(fileName, args);
     DateTime start = DateTime.Now;
     currentProcess = Process.Start(pStartInfo);
     OnProcessStarted(currentProcess);
     try
     {
         CommandExecuting?.Invoke(this, new CommandExecutingEventArgs(fileName, args));
         ExecutingSource?.Invoke(this, new CommandExecutingEventArgs(fileName, args));
     }
     catch { }
     currentProcess.WaitForExit();
     DateTime end = DateTime.Now;
     exitCode = currentProcess.ExitCode;
     OnProcessExited(currentProcess);
     currentProcess = null;
     var result = new Result(outputBuilder.Result, exitCode);
     try
     {
         CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(fileName, args, result, end - start));
         ExecutedSource?.Invoke(this, new CommandExecutedEventArgs(fileName, args, result, end - start));
     }
     catch { }
     return result;
 }
예제 #3
0
        private void FireExecutingEvent(Command command)
        {
            var eventArgs = new CommandExecutingEventArgs(command);

            CommandExecuting.Invoke(this, eventArgs);
            if (eventArgs.Cancel)
            {
                throw new CommandAbortedException("Command canceled by event handler");
            }
        }
예제 #4
0
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public ICommandResult Execute(string fileName, string args)
        {
            lock (_executingLock)
            {
                if (isDisposed)
                {
                    throw new ObjectDisposedException(nameof(HestExecutor));
                }
                //输出构造器
                outputBuilder.Clear();
                //记录开始时间
                DateTime start = DateTime.Now;
                //开始进程
                currentProcess = Process.Start(GetStartInfo(fileName, args));

                //监听
                currentProcess.OutputDataReceived += (s, e) =>
                {
                    outputBuilder.AppendOut(e.Data);
                    OutputReceived?.Invoke(this, new OutputReceivedEventArgs(e, false));
                };
                currentProcess.ErrorDataReceived += (s, e) =>
                {
                    outputBuilder.AppendError(e.Data);
                    OutputReceived?.Invoke(this, new OutputReceivedEventArgs(e, true));
                };
                currentProcess.BeginOutputReadLine();
                currentProcess.BeginErrorReadLine();

                //触发事件
                CommandExecuting?.Invoke(this, new CommandExecutingEventArgs(fileName, args));

                //等待进程结束
                currentProcess.WaitForExit();
                currentProcess.CancelErrorRead();
                currentProcess.CancelOutputRead();

                //记录结束时间
                DateTime end = DateTime.Now;
                //构造结果对象
                var result = new HestExecutorResult()
                {
                    ExitCode = currentProcess.ExitCode, Output = outputBuilder.Result
                };
                //触发结束事件
                CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(fileName, args, result, end - start));
                //返回结果
                return(result);
            };
        }
예제 #5
0
        /// <summary>Executes the supplied command</summary>
        /// <param name="command">The command to execute.</param>
        /// <param name="context">The context passed to the command</param>
        public virtual void Execute(CommandBase <CommandContext> command, CommandContext context)
        {
            var args = new CommandProcessEventArgs {
                Command = command, Context = context
            };

            if (CommandExecuting != null)
            {
                CommandExecuting.Invoke(this, args);
            }

            logger.Info(args.Command.Name + " processing " + args.Context);
            using (var tx = persister.Repository.BeginTransaction())
            {
                try
                {
                    args.Command.Process(args.Context);
                    Utility.FindEmpty(args.Context.Content);
                    tx.Commit();

                    if (CommandExecuted != null)
                    {
                        CommandExecuted.Invoke(this, args);
                    }
                }
                catch (StopExecutionException)
                {
                    tx.Rollback();
                }
                catch (Exception ex)
                {
                    tx.Rollback();
                    logger.Error(ex);
                    throw;
                }
                finally
                {
                    logger.Info(" -> " + args.Context);
                }
            }
        }
예제 #6
0
 /// <summary>
 /// Raise the SMTP command as executing.
 /// </summary>
 /// <param name="command">The command that is executing.</param>
 internal void RaiseSmtpCommandExecuting(SmtpCommand command)
 {
     CommandExecuting?.Invoke(this, new SmtpCommandExecutingEventArgs(command));
 }
 /// <summary>
 /// The trigger function for <see cref="CommandExecuting"/>	event.
 /// </summary>
 /// <param name="sqlCommand">The SQL command that is going for execution.</param>
 private void OnCommandExecuting(FbCommand sqlCommand, SqlStatementType statementType)
 {
     CommandExecuting?.Invoke(this, new CommandExecutingEventArgs(sqlCommand, statementType));
 }
예제 #8
0
 /// <summary>
 /// Raise the command executing event.
 /// </summary>
 /// <param name="command">The command that is executing.</param>
 public void RaiseCommandExecuting(SmtpCommand command)
 {
     CommandExecuting?.Invoke(this, new SmtpCommandExecutingEventArgs(command));
 }
예제 #9
0
 /// <summary>
 /// Fires an  event.
 /// </summary>
 /// <param name="command">The command.</param>
 /// <param name="connection">The connection.</param>
 protected void FireExecutingEvent(IDbCommand command, IDataConnection connection, IDbConnection conn)
 {
     CommandExecuting?.Invoke(this, new CommandExecutingEventArgs(command, connection, conn));
 }
예제 #10
0
 protected virtual void OnCommandExecuting(string msg)
 {
     CommandExecuting?.Invoke(this, new CommandEventArgs {
         Message = msg
     });
 }
예제 #11
0
 protected virtual void OnCommandExecuting()
 {
     CommandExecuting?.Invoke(this, EventArgs.Empty);
 }