Exemplo n.º 1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("Batch Job Requested...");

            try
            {
                var settings = JobSettings.FromAppSettings();

                using (var batchClient = await BatchClient.OpenAsync(new BatchSharedKeyCredentials(settings.BatchAccountUrl, settings.BatchAccountName, settings.BatchAccountKey)))
                {
                    // Add a retry policy. The built-in policies are No Retry (default), Linear Retry, and Exponential Retry.

                    batchClient.CustomBehaviors.Add(RetryPolicyProvider.ExponentialRetryProvider(TimeSpan.FromSeconds(settings.RetryDeltaBackoff), settings.RetryMaxCount));

                    var jobId     = $"BlobMover-{ Guid.NewGuid().ToString() }";
                    var queryArgs = req.GetQueryNameValuePairs();
                    var container = queryArgs.FirstOrDefault(kvp => String.Equals(kvp.Key, "container", StringComparison.InvariantCultureIgnoreCase)).Value;
                    var blob      = queryArgs.FirstOrDefault(kvp => String.Equals(kvp.Key, "blob", StringComparison.InvariantCultureIgnoreCase)).Value;

                    await BlobEventHook.SubmitJobAsync(settings, batchClient, container, blob, jobId, log);

                    log.Info("Batch Job Created.");

                    return(req.CreateResponse(HttpStatusCode.OK, nameof(HttpStatusCode.OK)));
                }
            }

            catch (Exception ex)
            {
                log.Info("");
                log.Error("An error occurred while submitting the job", ex);
                log.Info("");

                return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }