示例#1
0
        private async Task <TaskResult> PreProcessSession(IServerSession session)
        {
            await session.InitializeAsync();

            try {
                session.OnPreBuildEvent();
            }
            catch (OperationCanceledException) {
                throw;
            }
            catch (Exception error) {
                throw new ApplicationException("Pre-Build event Failed!", error);
            }

            try {
                session.Output.WriteLine("Preparing working directory...", ConsoleColor.DarkCyan);

                await session.PrepareWorkDirectoryAsync();
            }
            catch (OperationCanceledException) {
                throw;
            }
            catch (Exception error) {
                var _e = error;
                if (error is AggregateException _ae)
                {
                    _e = _ae.Flatten();
                }

                throw new ApplicationException("Pre-Build event Failed!", _e);
            }

            try {
                session.Output.WriteLine("Running script...", ConsoleColor.DarkCyan);

                await session.RunAsync();

                return(TaskResult.Ok());
            }
            catch (OperationCanceledException) {
                throw;
            }
            catch (Exception error) {
                throw new ApplicationException("Script Failed!", error);
            }
        }
示例#2
0
        private async Task OnProcess(IServerSession session)
        {
            Log.Debug($"Processing Script Session '{session.SessionId}'.");

            var abort     = false;
            var errorList = new List <Exception>();

            try {
                session.OnPreBuildEvent();
            }
            catch (Exception error) {
                Log.Error("Pre-Build Event Failed!", error);
            }

            try {
                session.Output
                .AppendLine("Preparing working directory...", ConsoleColor.DarkCyan);

                await session.PrepareWorkDirectoryAsync();
            }
            catch (Exception error) {
                var _e = error;
                if (error is AggregateException _ae)
                {
                    _e = _ae.Flatten();
                }

                session.Output
                .Append("Failed to prepare working directory! ", ConsoleColor.DarkRed)
                .AppendLine(_e.UnfoldMessages(), ConsoleColor.DarkYellow);

                abort = true;
                errorList.Add(_e);
            }

            TaskResult result = null;

            if (!abort)
            {
                try {
                    session.Output
                    .AppendLine("Running script...", ConsoleColor.DarkCyan);

                    await session.RunAsync();

                    result = TaskResult.Ok();
                }
                catch (Exception error) {
                    result = TaskResult.Error(error);
                    errorList.Add(error);
                    //abort = true;

                    session.Output
                    .Append("Script Failed! ", ConsoleColor.DarkRed)
                    .AppendLine(error.UnfoldMessages(), ConsoleColor.DarkYellow);
                }
            }

            session.Output
            .AppendLine("Destroying working directory...", ConsoleColor.DarkCyan);

            try {
                await session.ReleaseAsync();
            }
            catch (Exception error) {
                session.Output
                .AppendLine(error.UnfoldMessages(), ConsoleColor.DarkYellow);

                errorList.Add(error);
            }
            finally {
                session.Complete(result);
            }

            try {
                session.OnPostBuildEvent();
            }
            catch (Exception error) {
                Log.Error("Post-Build Event Failed!", error);
            }

            if (errorList.Count > 1)
            {
                session.Exception = new AggregateException(errorList);
            }
            else if (errorList.Count == 1)
            {
                session.Exception = errorList[0];
            }

            if (session.Exception != null)
            {
                Log.Warn($"Completed Script Session '{session.SessionId}' with errors.", session.Exception);
            }
            else
            {
                Log.Debug($"Completed Script Session '{session.SessionId}' successfully.");
            }
        }