Exemple #1
0
        /// <summary>
        ///     Calls the script
        /// </summary>
        /// <param name="job">The current job</param>
        /// <returns>An ActionResult to determine the success and a list of errors</returns>
        public ActionResult ProcessJob(Job job)
        {
            _logger.Debug("Launched Script-Action");

            ApplyPreSpecifiedTokens(job);
            var actionResult = Check(job.Profile, job.Accounts, CheckLevel.Job);

            if (!actionResult)
            {
                return(actionResult);
            }

            var scriptFile = job.Profile.Scripting.ScriptFile;

            _logger.Debug("Script-File: " + scriptFile);

            IProcess process = _processStarter.CreateProcess(scriptFile);

            var parameters = ComposeScriptParameters(job.Profile.Scripting.ParameterString, job.OutputFiles, job.TokenReplacer);

            process.StartInfo.Arguments = parameters;
            _logger.Debug("Script-Parameters: " + parameters);

            var scriptDir = _pathSafe.GetDirectoryName(scriptFile);

            if (scriptDir != null)
            {
                process.StartInfo.WorkingDirectory = scriptDir;
            }

            _logger.Debug("Script-Working Directory: " + scriptDir);

            process.EnableRaisingEvents = true;
            process.Exited += (sender, args) => process.Close();

            try
            {
                _logger.Debug("Launching script...");
                process.Start();

                if (job.Profile.Scripting.WaitForScript)
                {
                    _logger.Debug("Waiting for script to end");
                    process.WaitForExit();
                    _logger.Debug("Script execution ended");
                }
                else
                {
                    _logger.Debug("The script is executed in the background");
                }

                return(new ActionResult());
            }
            catch (Exception ex)
            {
                _logger.Error("Exception while running the script file \"" + scriptFile + "\"\r\n" + ex.Message);
                return(new ActionResult(ErrorCode.Script_GenericError));
            }
        }