public static bool IsExecuting(this EsriJobStatusResponse jobStatusResponse)
 {
     return(jobStatusResponse.jobStatus == EsriJobStatus.esriJobExecuting ||
            jobStatusResponse.jobStatus == EsriJobStatus.esriJobWaiting ||
            jobStatusResponse.jobStatus == EsriJobStatus.esriJobSubmitted);
 }
        public string RunJobRaw(Object requestObject)
        {
            var requestFormData = requestObject.ToKeyValue();

            //var requestContent = new FormUrlEncodedContent(requestFormData);

            requestFormData.Add("env:outSR", "");
            requestFormData.Add("env:processSR", "");
            requestFormData.Add("context", "");

            var retry    = true;
            var attempts = 0;
            EsriJobStatusResponse jobStatusResponse = null;

            while (retry && attempts < MAX_RETRIES)
            {
                jobStatusResponse = SubmitJob(requestFormData);
                JobID             = jobStatusResponse.jobId;
                int timeout;
                // wait 5 seconds before checking for process on first attempt, 30 on second, and 90 on third
                switch (attempts)
                {
                case 0:
                    timeout = 5000;
                    break;

                case 1:
                    timeout = 30000;
                    break;

                case 2:
                    timeout = 90000;
                    break;

                default:
                    timeout = 5000;
                    break;
                }
                retry = CheckShouldRetry(timeout);
                attempts++;
            }

            if (retry && attempts >= MAX_RETRIES)
            {
                throw new TimeoutException("Remote service failed to respond within the timeout.");
            }

            var isExecuting = jobStatusResponse.IsExecuting();

            while (isExecuting)
            {
                Thread.Sleep(DEFAULT_MILLISECONDS_TIMEOUT);
                var jobStatusHttpResponseMessage = HttpClient.GetAsync(JobStatusUrl).Result;
                jobStatusResponse =
                    JsonConvert.DeserializeObject <EsriJobStatusResponse>(jobStatusHttpResponseMessage.Content
                                                                          .ReadAsStringAsync().Result);
                isExecuting = jobStatusResponse.IsExecuting();
            }

            switch (jobStatusResponse.jobStatus)
            {
            case EsriJobStatus.esriJobSucceeded:
                var resultContent = HttpClient.GetAsync(JobResultUrl).Result.Content.ReadAsStringAsync().Result;
                return(resultContent);

            case EsriJobStatus.esriJobCancelling:
            case EsriJobStatus.esriJobCancelled:
                throw new EsriAsynchronousJobCancelledException(jobStatusResponse.jobId);

            case EsriJobStatus.esriJobFailed:
                throw new EsriAsynchronousJobFailedException(jobStatusResponse, requestObject.ToString());

            default:
                // ReSharper disable once NotResolvedInText
                throw new ArgumentOutOfRangeException("jobStatusResponse.jobStatus",
                                                      $"Unexpected job status from HRU job {jobStatusResponse.jobId}. Last message: {jobStatusResponse.messages.Last().description}");
            }
        }
 public EsriAsynchronousJobFailedException(EsriJobStatusResponse jobStatusResponse, string requestObjectString) : base($"{jobStatusResponse.jobId} failed. Last messages: {string.Join(", ", jobStatusResponse.messages.Select(x => x.description))}. Last request Oboject: {requestObjectString}")
 {
 }