public bool RunCommand(XTMFCommand command, ref string error)
        {
            lock (_SessionLock)
            {
                if (command.Do(ref error))
                {
                    HasChanged = true;
                    if (_InCombinedContext)
                    {
                        var list = _CombinedCommands;
                        list.Add(command);
                    }
                    else
                    {
                        if (command.CanUndo())
                        {
                            _UndoStack.Add(command);
                        }

                        CommandExecuted?.Invoke(this, new EventArgs());
                    }

                    // if we do something new, redo no long is available
                    _RedoStack.Clear();
                    return(true);
                }

                return(false);
            }
        }
Exemplo n.º 2
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);
            };
        }
Exemplo n.º 3
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;
 }
        private void ExecuteCommand(IInfiniteCanvasCommand command)
        {
            _undoCommands.Push(command);
            _redoCommands.Clear();
            command.Execute();

            CommandExecuted?.Invoke(this, EventArgs.Empty);
        }
Exemplo n.º 5
0
 public override void Execute(object parameter)
 {
     base.Execute(parameter);
     CommandExecuted?.Invoke(new MediaCommandExecutedEventArgs()
     {
         Parameter = parameter
     });
 }
Exemplo n.º 6
0
        private async Task ExecuteAndDispatchNext(CommandBase command)
        {
            await command.ExecuteAsync().ConfigureAwait(false);

            CommandExecuted?.Invoke(this, new CommandRunnerEvent(command));

            DispatchNext();
        }
Exemplo n.º 7
0
 public void InvokeCommand(CmdlineCommand command, string args)
 {
     // We don't want to block this call because it causes issues if some sync operation that shuts down server is also called.
     // For example, mode switch or instance shutdown calls are stopping server,
     // which results in serviceHost.Close() timeout, since server would be still waiting for InvokeCommand to finish.
     Task.Run(async() =>
     {
         await Task.Delay(100);
         syncContext.Post(_ => CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(command, args)), null);
     });
 }
Exemplo n.º 8
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="journalRecord"></param>
 /// <param name="isLocal"></param>
 /// <param name="events"></param>
 private void NotifyCommandExecuted(JournalRecord journalRecord, bool isLocal, IEnumerable <Event> events)
 {
     try
     {
         CommandExecuted.Invoke(journalRecord, isLocal, events);
     }
     catch
     {
         // Don't let external code crash the engine
     }
 }
Exemplo n.º 9
0
 private void NotifyCommandExecuted(JournalRecord journalRecord, bool isLocal, IEnumerable <Event> events)
 {
     try
     {
         CommandExecuted.Invoke(journalRecord, isLocal, events);
     }
     catch (Exception exception)
     {
         _logger.LogError(exception, "Exception thrown in CommandExecuted handler.");
     }
 }
        public void OnCommandExecuted(ICommandContext context, Task <IResult> task)
        {
            IResult result = task.Result;

            if (result is null)
            {
                return;
            }

            CommandExecuted?.Invoke(new Optional <CommandInfo>(), context, result);
        }
Exemplo n.º 11
0
        private void CommandExecutionLoop()
        {
            int commandNumber = 0;

            foreach (ICommand command in commands)
            {
                ExecuteCommand(command);
                commandNumber++;

                CommandExecuted?.Invoke(commandNumber);
            }
        }
Exemplo n.º 12
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);
            };
        }
        public void ExecuteCombinedCommands(string name, Action combinedCommandContext)
        {
            lock (_SessionLock)
            {
                _InCombinedContext = true;
                var list = _CombinedCommands = new List <XTMFCommand>();
                combinedCommandContext();
                // only add to the undo list if a command was added successfully
                if (list.Count > 0)
                {
                    // create a command to undo everything in a single shot [do is empty]
                    _UndoStack.Add(XTMFCommand.CreateCommand(name, (ref string error) => { return(true); },
                                                             (ref string error) =>
                    {
                        foreach (var command in ((IEnumerable <XTMFCommand>)list).Reverse())
                        {
                            if (command.CanUndo())
                            {
                                if (!command.Undo(ref error))
                                {
                                    return(false);
                                }
                            }
                        }

                        return(true);
                    },
                                                             (ref string error) =>
                    {
                        foreach (var command in list)
                        {
                            if (command.CanUndo())
                            {
                                if (!command.Redo(ref error))
                                {
                                    return(false);
                                }
                            }
                        }

                        return(true);
                    }));
                    CommandExecuted?.Invoke(this, new EventArgs());
                }

                _InCombinedContext = false;
                _CombinedCommands  = null;
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Actual command execution
        /// </summary>
        public object Execute(Command command)
        {
            EnsureNotDisposed();
            EnsureAuthorized(command);
            FireExecutingEvent(command);

            lock (_commandSequenceLock)
            {
                var  ctx             = ExecutionContext.Begin();
                bool exceptionThrown = false;
                _executionTimer.Restart();
                _config.Isolation.Commands.Apply(ref command);

                ulong lastEntryId = (_config.PersistenceMode == PersistenceMode.Journaling)
                    ? _journalAppender.Append(command)
                    : 0;

                try
                {
                    return(_kernel.ExecuteCommand(command));
                }
                catch (Exception ex)
                {
                    exceptionThrown = true;
                    if (_config.PersistenceMode == PersistenceMode.Journaling)
                    {
                        _journalAppender.AppendRollbackMarker();
                    }
                    if (!(ex is CommandAbortedException))
                    {
                        Rollback();
                        ex = new CommandFailedException("Command failed with rollback, see inner exception for details", ex);
                    }
                    throw ex;
                }
                finally
                {
                    _synchronizer.Exit();
                    if (!exceptionThrown)
                    {
                        var args = new CommandExecutedEventArgs(lastEntryId, command, ctx.Timestamp, _executionTimer.Elapsed, ctx.Events);
                        CommandExecuted.Invoke(this, args);
                    }
                    ExecutionContext.Current = null;
                }
            }
        }
Exemplo n.º 15
0
        public void Execute(Command cmd)
        {
            if (cmd is null)
            {
                throw new ArgumentNullException(nameof(cmd));
            }

            if (BeginExecute(cmd) == false)
            {
                return;
            }
            CommandExecutedEventArg cmmdExecutedEventArg =
                new CommandExecutedEventArg(cmd.UpdateEntity.Entity);

            try
            {
                var history = _historyRepositoryFactory.GetRepository(cmd);
                IEntityRepository entityRepository = _entityRepositoryFactory.GetRepository(cmd);

                var lastUpdatedEntity = entityRepository.LastUpdated;

                if (lastUpdatedEntity != null)
                {
                    CheckData(cmd, entityRepository, lastUpdatedEntity);
                }

                if (history != null)
                {
                    history.Record(lastUpdatedEntity, cmd.UpdateEntity.Entity);
                }

                entityRepository.Save();

                cmd.UpdateEntity.Entity.LastUpdateDateTime = cmd.CurrentUpdateDateTime;
                cmd.UpdateEntity.Entity.LastUpdatedUser    = cmd.CurrentUpdateUser;
            }
            catch (Exception exp)
                when(exp is DataUpdateConflictException || exp is ReferenceObsoleteException)
                {
                    cmmdExecutedEventArg.IsSuccess    = false;
                    cmmdExecutedEventArg.ErrorMessage = exp.Message;
                }

            CommandExecuted?.Invoke(this, cmmdExecutedEventArg);
        }
Exemplo n.º 16
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);
                }
            }
        }
        /// <summary>
        ///     Redo the last undone command
        /// </summary>
        /// <param name="error">The reason why it failed</param>
        public bool Redo(ref string error)
        {
            lock (_SessionLock)
            {
                if (_RedoStack.TryPop(out var command))
                {
                    if (command != null)
                    {
                        if (command.Redo(ref error))
                        {
                            HasChanged = true;
                            _UndoStack.Add(command);
                            CommandExecuted?.Invoke(this, new EventArgs());
                            return(true);
                        }

                        return(false);
                    }
                }

                error = "There was nothing to redo.";
                return(false);
            }
        }
Exemplo n.º 18
0
 public void InvokeCommand(CmdlineCommand command, string args)
 {
     CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(command, args));
 }
Exemplo n.º 19
0
        /**********************************************************************/
        #region Private Methods

        private void CommandExecute(object parameter)
        => CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(parameter));
Exemplo n.º 20
0
 /// <summary>
 /// The trigger function for <see cref="CommandExecuted"/> event.
 /// </summary>
 /// <param name="commandText">The <see cref="FbCommand.CommandText"/> of the executed SQL command.</param>
 /// <param name="dataReader">The <see cref="FbDataReader"/> instance with the returned data. If the
 /// command executed is not meant to return data (ex: UPDATE, INSERT...) this parameter must be
 /// setled to <b>null</b>.</param>
 /// <param name="rowsAffected">The rows that were affected by the executed SQL command. If the executed
 /// command is not meant to return this kind of information (ex: SELECT) this parameter must
 /// be setled to <b>-1</b>.</param>
 private void OnCommandExecuted(FbDataReader dataReader, string commandText, SqlStatementType statementType, int rowsAffected)
 {
     CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(dataReader, commandText, statementType, rowsAffected));
 }
Exemplo n.º 21
0
 protected virtual void OnCommandExecuted()
 {
     CommandExecuted?.Invoke(this, EventArgs.Empty);
 }
Exemplo n.º 22
0
 public void OnCommandExecuted()
 {
     CommandExecuted?.Invoke(this);
 }
 protected void RaiseCommandExecuted()
 {
     CommandExecuted?.Invoke(this, EventArgs.Empty);
 }
 private void OnCommandExecuted(DbCommand command)
 {
     CommandExecuted?.Invoke(this, command);
 }
Exemplo n.º 25
0
 protected virtual void OnCommandExecuted(string msg)
 {
     CommandExecuted?.Invoke(this, new CommandEventArgs {
         Message = msg
     });
 }
Exemplo n.º 26
0
 /// <summary>
 /// Raise the command executed event.
 /// </summary>
 /// <param name="command">The command that was executed.</param>
 internal void RaiseCommandExecuted(SmtpCommand command)
 {
     CommandExecuted?.Invoke(this, new SmtpCommandEventArgs(this, command));
 }
Exemplo n.º 27
0
 private void FireOnCommandExecuted()
 {
     CommandExecuted?.Invoke(this, EventArgs.Empty);
 }
Exemplo n.º 28
0
 protected void FireExecutedEvent(IDbCommand command, IDataConnection connection, IDbConnection conn)
 {
     CommandExecuted?.Invoke(this, new CommandExecutingEventArgs(command, connection, conn));
 }
Exemplo n.º 29
0
 public void RaiseCommandExecuted(DbCommand command, int?rowsAffected)
 {
     CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(command, rowsAffected));
 }
Exemplo n.º 30
0
 /// <summary>
 /// Raises a command executed event.
 /// </summary>
 /// <param name="eventArgs">Holds data generated during command execution</param>
 protected void RaiseCommandExecutedEvent(CommandExecutedArgs eventArgs)
 {
     CommandExecuted?.Invoke(eventArgs);
 }