コード例 #1
0
        static int TriggerTask(EDXTask t)
        {
            /*
             * Completed   0   The task has completed successfully. (same as 6)
             * Waiting     1   The task is waiting to be executed.
             * Running     2   The task is running
             * Aborting    3   The task is aborting the execution.
             * Failed      4   The task failed.
             * Warning     5   The task completed with a warning.
             * Completed   6   The task has completed successfully.
             * Error       9   An unknown error occured
             * Exception   10  Catch exception error
             */

            var exitCode = 0;

            LogProperties logProperties = new LogProperties {
                TaskNameOrId = t.TaskNameOrId, ExecId = "-1"
            };

            try
            {
                // Create a QMS API client
                IQMS apiClient = String.IsNullOrEmpty(t.ServiceAddress) ? new QMSClient() : new QMSClient("BasicHttpBinding_IQMS", t.ServiceAddress);

                // Retrieve a time limited service key
                ServiceKeyClientMessageInspector.ServiceKey = apiClient.GetTimeLimitedServiceKey();

                TaskInfo taskInfo = new TaskInfo();

                if (!IsGuid(t.TaskNameOrId))
                {
                    List <TaskInfo> taskList = apiClient.FindEDX(t.TaskNameOrId);

                    // Find correct task with support for multiple qds
                    if (taskList.Count > 0)
                    {
                        int i = 0;

                        for (i = 0; i < taskList.Count; i++)
                        {
                            if (taskList[i].Name == t.TaskNameOrId)
                            {
                                break;
                            }
                        }

                        taskInfo = new TaskInfo
                        {
                            Name    = taskList[i].Name,
                            ID      = taskList[i].ID,
                            QDSID   = taskList[i].QDSID,
                            Enabled = taskList[i].Enabled
                        };
                    }
                }
                else
                {
                    taskInfo = apiClient.GetTask(Guid.Parse(t.TaskNameOrId));
                }

                if (taskInfo.Name != null)
                {
                    // Trigger the task
                    TriggerEDXTaskResult result = apiClient.TriggerEDXTask(Guid.Empty, taskInfo.Name, t.Password, t.VariableName, t.VariableValues);

                    if (result.EDXTaskStartResult == EDXTaskStartResult.Success)
                    {
                        logProperties.ExecId = result.ExecId.ToString();

                        if (t.Verbosity > 0)
                        {
                            LogHelper.Log(LogLevel.Info, String.Format("Name: {0}, ID: {1}, Enabled: {2}, Sleep: {3} seconds, Timeout: {4}", taskInfo.Name, taskInfo.ID, taskInfo.Enabled ? "Yes" : "No", t.Sleep / 1000, t.TimeOut == -1 ? "Indefinitely" : t.TimeOut / 60000 + " minutes"), logProperties);
                        }

                        LogHelper.Log(LogLevel.Info, "Started", logProperties);

                        EDXStatus executionStatus = null;

                        if (t.TimeOut != 0)
                        {
                            // Wait until the task is completed or TIMEOUT has passed.
                            SpinWait.SpinUntil(() =>
                            {
                                Thread.Sleep(t.Sleep);

                                // Retrieve a new service key if sleep time is above 18 minutes to be safe (timeout is 20 minutes in QV11)
                                if (t.Sleep > 18 * 60 * 1000)
                                {
                                    if (t.Verbosity > 1)
                                    {
                                        LogHelper.Log(LogLevel.Info, "GetTimeLimitedServiceKey()", logProperties);
                                    }

                                    ServiceKeyClientMessageInspector.ServiceKey = apiClient.GetTimeLimitedServiceKey();
                                }

                                // Get the current state of the task.
                                try
                                {
                                    executionStatus = apiClient.GetEDXTaskStatus(Guid.Empty, result.ExecId);
                                }
                                catch (Exception ex)
                                {
                                    LogHelper.Log(LogLevel.Warn, String.Format("{0}", ex.Message.Replace(Environment.NewLine, " ")), logProperties);
                                }

                                if (executionStatus != null && t.Verbosity > 1 && executionStatus.TaskStatus != TaskStatusValue.Running)
                                {
                                    LogHelper.Log(LogLevel.Info, executionStatus.TaskStatus.ToString(), logProperties);
                                }

                                // Return true if the task has completed.
                                return(executionStatus != null && (executionStatus.TaskStatus != TaskStatusValue.Running && executionStatus.TaskStatus != TaskStatusValue.Waiting));
                            }, t.TimeOut);

                            // Write the result
                            if (executionStatus != null)
                            {
                                if (executionStatus.TaskStatus == TaskStatusValue.Completed)
                                {
                                    // datetime parsing needs culture formatting, catch it for now and avoid...
                                    try
                                    {
                                        TimeSpan span = DateTime.Parse(executionStatus.FinishTime).Subtract(DateTime.Parse(executionStatus.StartTime));
                                        LogHelper.Log(LogLevel.Info, String.Format("{0} (Duration: {1})", executionStatus.TaskStatus, span), logProperties);
                                    }
                                    catch (Exception)
                                    {
                                        LogHelper.Log(LogLevel.Info, String.Format("{0}", executionStatus.TaskStatus), logProperties);
                                    }
                                }
                                else
                                {
                                    // If something went wrong, point to the logfile for the task execution
                                    exitCode = (Int32)executionStatus.TaskStatus;
                                    LogHelper.Log(LogLevel.Error, String.Format("{0} (Error code: {1})", executionStatus.TaskStatus, exitCode), logProperties);
                                    LogHelper.Log(LogLevel.Error, "Logfile: " + executionStatus.LogFileFullPath, logProperties);
                                }
                            }
                            else
                            {
                                exitCode = 9;
                                LogHelper.Log(LogLevel.Error, String.Format("Failed to get execution status (Error code: {0})", exitCode), logProperties);
                            }
                        }
                    }
                    else
                    {
                        exitCode = 9;
                        LogHelper.Log(LogLevel.Error, String.Format("{0} (Error code: {1})", result.EDXTaskStartResult, exitCode), logProperties);
                    }
                }
                else
                {
                    exitCode = 9;
                    LogHelper.Log(LogLevel.Error, "TaskNotFound (Error code: 9)", logProperties);
                }
            }
            catch (Exception ex)
            {
                exitCode = 10;
                LogHelper.Log(LogLevel.Error, String.Format("{0} (Error code: {1})", ex.Message.Replace(Environment.NewLine, " "), exitCode), logProperties);
            }

            return(exitCode);
        }