Пример #1
0
        public HttpResponseMessage SubmitBatch(string[] urls)
        {
            try
            {
                //check for valid url
                urls.ToList().ForEach(url =>
                {
                    if (!Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
                    {
                        throw new Exception($"URL {url} is not in proper format");
                    }
                });

                var batchGuid = BatchWoker.QueueBatchRequest(urls.ToList());

                return(Request.CreateResponse(HttpStatusCode.Accepted, new SubmitBatchResponse()
                {
                    BatchGuid = batchGuid,
                    Status = StatusEnum.QUEUED
                }));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Пример #2
0
        public HttpResponseMessage GetBatchStatus(string id)
        {
            try
            {
                var status = BatchWoker.GetBatchStatus(id);

                return(Request.CreateResponse(HttpStatusCode.Accepted, status));
            }
            catch (Exception ex)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Пример #3
0
 public void AppStart()
 {
     Task.Factory.StartNew(() =>
     {
         try
         {
             while (true)
             {
                 BatchWoker.ExecuteBatchRequest();
             }
         }
         catch (Exception e)
         {
             Console.WriteLine(e);
             throw;
         }
     });
 }
Пример #4
0
        public StatusEnum Test_GetBatchStatus(bool checkCompleted)
        {
            try
            {
                //submiting batch request
                var submitResponse = Test_SubmitBatch();

                if (submitResponse != null)
                {
                    var batchGuid = submitResponse.BatchGuid;

                    var controller = new DownloadController
                    {
                        Request = new HttpRequestMessage()
                    };
                    controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey,
                                                      new HttpConfiguration());

                    var dequeueWorker = Task.Factory.StartNew(() => { BatchWoker.ExecuteBatchRequest(); });

                    //if test is for "COMPLETED" batch then wait for all tasks to finish, else provide the status immediately
                    if (checkCompleted)
                    {
                        dequeueWorker.Wait();
                        if (dequeueWorker.IsCompleted)
                        {
                            //calling target action
                            var response = controller.GetBatchStatus(submitResponse.BatchGuid.ToString());

                            if (response != null)
                            {
                                var responseReadTask = Task.Factory.StartNew(() =>
                                {
                                    return(response.Content.ReadAsStringAsync());
                                });

                                var result = responseReadTask.Result.Result;

                                if (string.IsNullOrEmpty(result))
                                {
                                    throw new Exception("empty response from batch status request");
                                }
                                else
                                {
                                    try
                                    {
                                        return(JsonConvert.DeserializeObject <StatusEnum>(result));
                                    }
                                    catch (Exception e)
                                    {
                                        throw new Exception("failed to deserialize batch status response", e);
                                    }
                                }
                            }
                            else
                            {
                                throw new Exception("null response from batch status request");
                            }
                        }
                        else
                        {
                            throw new Exception("failed to execute batch request");
                        }
                    }
                    else
                    {
                        //calling target action
                        var response = controller.GetBatchStatus(submitResponse.BatchGuid.ToString());

                        if (response != null)
                        {
                            var responseReadTask = Task.Factory.StartNew(() =>
                            {
                                return(response.Content.ReadAsStringAsync());
                            });

                            var result = responseReadTask.Result.Result;

                            if (string.IsNullOrEmpty(result))
                            {
                                throw new Exception("empty response from batch status request");
                            }
                            else
                            {
                                try
                                {
                                    return(JsonConvert.DeserializeObject <StatusEnum>(result));
                                }
                                catch (Exception e)
                                {
                                    throw new Exception("failed to deserialize batch status response", e);
                                }
                            }
                        }
                        else
                        {
                            throw new Exception("null response from batch status request");
                        }
                    }
                }
                else
                {
                    throw new Exception("null response for batch submit request");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }