private string GetTaskStatusText(AnalyseTask task)
        {
            string         text       = string.Empty;
            TaskStatusInfo statusInfo = Constant.TaskStatusInfos[(int)task.Status];

            if (task.Status != TaskStatus.Analysing)
            {
                text = statusInfo.Name;
            }
            else
            {
                //int countProcessed = task.PictureSource.RecognizedCount + task.PictureSource.ImageErrorCount;
                //if (countProcessed == 0)
                //{
                //    text = string.Format("{0} (0%)", statusInfo.Name);
                //}
                //else
                //{
                //    float percent = ((float)countProcessed) / ((float)task.PictureSource.Count);
                //    string tmp = (percent * 100).ToString("F1");
                //    text = string.Format("{0} ({1}%)", statusInfo.Name, tmp);
                //}
            }
            return(text);
        }
예제 #2
0
 public TaskReport(TaskStatusInfo taskStatusInfo)
 {
     this.TaskId       = taskStatusInfo.TaskId;
     this.TaskResult   = taskStatusInfo.Result;
     this.TaskStatus   = taskStatusInfo.Status;
     this.ErrorCode    = taskStatusInfo.Error.Code;
     this.ErrorMessage = taskStatusInfo.Error.Message;
     this.TaskSteps    = taskStatusInfo.TaskSteps;
 }
예제 #3
0
        internal virtual void HandleSyncTaskResponse(TaskStatusInfo taskStatus, string operationName)
        {
            string     msg        = string.Empty;
            TaskReport taskReport = new TaskReport(taskStatus);

            if (taskStatus.AsyncTaskAggregatedResult != AsyncTaskAggregatedResult.Succeeded)
            {
                msg = string.Format(Resources.FailureMessageCompleteJob, operationName);
                WriteObject(taskReport);
            }

            else
            {
                msg = string.Format(Resources.SuccessMessageCompleteJob, operationName);
                WriteObject(taskReport);
            }

            WriteVerbose(msg);
        }
        /// <summary>
        /// The Get Task Status returns the status of the specified task id.
        /// After calling an asynchronous task, you can call Get Task Status
        /// to determine whether the task has succeeded, failed, or is still
        /// in progress.
        /// </summary>
        /// <param name='taskId'>
        /// Required. The task Id for the request you wish to track.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Info about the async task
        /// </returns>
        public async Task <TaskStatusInfo> GetOperationStatusAsync(string taskId, CancellationToken cancellationToken)
        {
            // Validate
            if (taskId == null)
            {
                throw new ArgumentNullException("taskId");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("taskId", taskId);
                TracingAdapter.Enter(invocationId, this, "GetOperationStatusAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/";
            if (this.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Credentials.SubscriptionId);
            }
            url = url + "/cloudservices/";
            url = url + Uri.EscapeDataString(this.CloudServiceName);
            url = url + "/resources/";
            url = url + Uri.EscapeDataString(this.ResourceNamespace);
            url = url + "/~/";
            url = url + "CisVault";
            url = url + "/";
            url = url + Uri.EscapeDataString(this.ResourceName);
            url = url + "/api/jobs/";
            url = url + Uri.EscapeDataString(taskId);
            List <string> queryParameters = new List <string>();

            queryParameters.Add("api-version=2014-01-01.1.0");
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("Accept", "application/xml");
                httpRequest.Headers.Add("x-ms-version", "2014-01-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    TaskStatusInfo result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new TaskStatusInfo();
                        XDocument responseDoc = XDocument.Parse(responseContent);

                        XElement jobStatusInfoElement = responseDoc.Element(XName.Get("JobStatusInfo", "http://windowscloudbackup.com/CiS/V2013_03"));
                        if (jobStatusInfoElement != null)
                        {
                            XElement jobIdElement = jobStatusInfoElement.Element(XName.Get("JobId", "http://windowscloudbackup.com/CiS/V2013_03"));
                            if (jobIdElement != null)
                            {
                                string jobIdInstance = jobIdElement.Value;
                                result.TaskId = jobIdInstance;
                            }

                            XElement statusElement = jobStatusInfoElement.Element(XName.Get("Status", "http://windowscloudbackup.com/CiS/V2013_03"));
                            if (statusElement != null)
                            {
                                AsyncTaskStatus statusInstance = ((AsyncTaskStatus)Enum.Parse(typeof(AsyncTaskStatus), statusElement.Value, true));
                                result.Status = statusInstance;
                            }

                            XElement resultElement = jobStatusInfoElement.Element(XName.Get("Result", "http://windowscloudbackup.com/CiS/V2013_03"));
                            if (resultElement != null)
                            {
                                AsyncTaskResult resultInstance = ((AsyncTaskResult)Enum.Parse(typeof(AsyncTaskResult), resultElement.Value, true));
                                result.Result = resultInstance;
                            }

                            XElement errorElement = jobStatusInfoElement.Element(XName.Get("Error", "http://windowscloudbackup.com/CiS/V2013_03"));
                            if (errorElement != null)
                            {
                                ErrorDetails errorInstance = new ErrorDetails();
                                result.Error = errorInstance;

                                XElement codeElement = errorElement.Element(XName.Get("Code", "http://schemas.microsoft.com/wars"));
                                if (codeElement != null)
                                {
                                    string codeInstance = codeElement.Value;
                                    errorInstance.Code = codeInstance;
                                }

                                XElement messageElement = errorElement.Element(XName.Get("Message", "http://schemas.microsoft.com/wars"));
                                if (messageElement != null)
                                {
                                    string messageInstance = messageElement.Value;
                                    errorInstance.Message = messageInstance;
                                }
                            }

                            XElement taskResultElement = jobStatusInfoElement.Element(XName.Get("TaskResult", "http://windowscloudbackup.com/CiS/V2013_03"));
                            if (taskResultElement != null)
                            {
                                AsyncTaskAggregatedResult taskResultInstance = ((AsyncTaskAggregatedResult)Enum.Parse(typeof(AsyncTaskAggregatedResult), taskResultElement.Value, true));
                                result.AsyncTaskAggregatedResult = taskResultInstance;
                            }

                            XElement jobStepsSequenceElement = jobStatusInfoElement.Element(XName.Get("JobSteps", "http://windowscloudbackup.com/CiS/V2013_03"));
                            if (jobStepsSequenceElement != null)
                            {
                                foreach (XElement jobStepsElement in jobStepsSequenceElement.Elements(XName.Get("JobStep", "http://windowscloudbackup.com/CiS/V2013_03")))
                                {
                                    TaskStep jobStepInstance = new TaskStep();
                                    result.TaskSteps.Add(jobStepInstance);

                                    XElement messageElement2 = jobStepsElement.Element(XName.Get("Message", "http://windowscloudbackup.com/CiS/V2013_03"));
                                    if (messageElement2 != null)
                                    {
                                        string messageInstance2 = messageElement2.Value;
                                        jobStepInstance.Message = messageInstance2;
                                    }

                                    XElement statusElement2 = jobStepsElement.Element(XName.Get("Status", "http://windowscloudbackup.com/CiS/V2013_03"));
                                    if (statusElement2 != null)
                                    {
                                        AsyncTaskStatus statusInstance2 = ((AsyncTaskStatus)Enum.Parse(typeof(AsyncTaskStatus), statusElement2.Value, true));
                                        jobStepInstance.Status = statusInstance2;
                                    }

                                    XElement resultElement2 = jobStepsElement.Element(XName.Get("Result", "http://windowscloudbackup.com/CiS/V2013_03"));
                                    if (resultElement2 != null)
                                    {
                                        AsyncTaskResult resultInstance2 = ((AsyncTaskResult)Enum.Parse(typeof(AsyncTaskResult), resultElement2.Value, true));
                                        jobStepInstance.Result = resultInstance2;
                                    }

                                    XElement detailElement = jobStepsElement.Element(XName.Get("Detail", "http://windowscloudbackup.com/CiS/V2013_03"));
                                    if (detailElement != null)
                                    {
                                        string detailInstance = detailElement.Value;
                                        jobStepInstance.Detail = detailInstance;
                                    }

                                    XElement errorCodeElement = jobStepsElement.Element(XName.Get("ErrorCode", "http://windowscloudbackup.com/CiS/V2013_03"));
                                    if (errorCodeElement != null)
                                    {
                                        string errorCodeInstance = errorCodeElement.Value;
                                        jobStepInstance.ErrorCode = errorCodeInstance;
                                    }
                                }
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }