Ejemplo n.º 1
0
        static void ProcessResults(BatchScoreStatus status)
        {
            bool first = true;

            foreach (var output in status.Results)
            {
                var blobLocation = output.Value;
                Console.WriteLine(string.Format("The result '{0}' is available at the following Azure Storage location:", output.Key));
                Console.WriteLine(string.Format("BaseLocation: {0}", blobLocation.BaseLocation));
                Console.WriteLine(string.Format("RelativeLocation: {0}", blobLocation.RelativeLocation));
                Console.WriteLine(string.Format("SasBlobToken: {0}", blobLocation.SasBlobToken));
                Console.WriteLine();


                // Save the first output to disk
                if (first)
                {
                    first = false;
                    SaveBlobToFile(blobLocation, string.Format("The results for {0}", output.Key));
                }
            }
        }
Ejemplo n.º 2
0
        static async Task InvokeBatchExecutionService()
        {
            // How this works:
            //
            // 1. Assume the input is present in a local file (if the web service accepts input)
            // 2. Upload the file to an Azure blob - you'd need an Azure storage account
            // 3. Call the Batch Execution Service to process the data in the blob. Any output is written to Azure blobs.
            // 4. Download the output blob, if any, to local file

            const string BaseUrl = "https://ussouthcentral.services.azureml.net/workspaces/8c68810612b849a994aec53eb470d02d/services/69bd399586ba43a98384b72bd9240b2b/jobs";

            const string StorageAccountName      = "mystorageacct";                     // Replace this with your Azure Storage Account name
            const string StorageAccountKey       = "Dx9WbMIThAvXRQWap/aLnxT9LV5txxw=="; // Replace this with your Azure Storage Key
            const string StorageContainerName    = "mycontainer";                       // Replace this with your Azure Storage Container name
            string       storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, StorageAccountKey);
            const string apiKey = "abc123";                                             // Replace this with the API key for the web service

            // set a time out for polling status
            const int TimeOutInMilliseconds = 120 * 1000; // Set a timeout of 2 minutes



            UploadFileToBlob("input1data.csv" /*Replace this with the location of your input file*/,
                             "input1datablob.csv" /*Replace this with the name you would like to use for your Azure blob; this needs to have the same extension as the input file */,
                             StorageContainerName, storageConnectionString);

            using (HttpClient client = new HttpClient())
            {
                var request = new BatchExecutionRequest()
                {
                    Inputs = new Dictionary <string, AzureBlobDataReference>()
                    {
                        {
                            "input1",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("{0}/input1datablob.csv", StorageContainerName)
                            }
                        },
                    },

                    Outputs = new Dictionary <string, AzureBlobDataReference>()
                    {
                        {
                            "output1",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("/{0}/output1results.csv", StorageContainerName)
                            }
                        },
                    },
                    GlobalParameters = new Dictionary <string, string>()
                    {
                    }
                };

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

                // WARNING: The 'await' statement below can result in a deadlock if you are calling this code from the UI thread of an ASP.Net application.
                // One way to address this would be to call ConfigureAwait(false) so that the execution does not attempt to resume on the original context.
                // For instance, replace code such as:
                //      result = await DoSomeTask()
                // with the following:
                //      result = await DoSomeTask().ConfigureAwait(false)


                Console.WriteLine("Submitting the job...");

                // submit the job
                var response = await client.PostAsJsonAsync(BaseUrl + "?api-version=2.0", request);

                if (!response.IsSuccessStatusCode)
                {
                    await WriteFailedResponse(response);

                    return;
                }

                string jobId = await response.Content.ReadAsAsync <string>();

                Console.WriteLine(string.Format("Job ID: {0}", jobId));


                // start the job
                Console.WriteLine("Starting the job...");
                response = await client.PostAsync(BaseUrl + "/" + jobId + "/start?api-version=2.0", null);

                if (!response.IsSuccessStatusCode)
                {
                    await WriteFailedResponse(response);

                    return;
                }

                string    jobLocation = BaseUrl + "/" + jobId + "?api-version=2.0";
                Stopwatch watch       = Stopwatch.StartNew();
                bool      done        = false;
                while (!done)
                {
                    Console.WriteLine("Checking the job status...");
                    response = await client.GetAsync(jobLocation);

                    if (!response.IsSuccessStatusCode)
                    {
                        await WriteFailedResponse(response);

                        return;
                    }

                    BatchScoreStatus status = await response.Content.ReadAsAsync <BatchScoreStatus>();

                    if (watch.ElapsedMilliseconds > TimeOutInMilliseconds)
                    {
                        done = true;
                        Console.WriteLine(string.Format("Timed out. Deleting job {0} ...", jobId));
                        await client.DeleteAsync(jobLocation);
                    }
                    switch (status.StatusCode)
                    {
                    case BatchScoreStatusCode.NotStarted:
                        Console.WriteLine(string.Format("Job {0} not yet started...", jobId));
                        break;

                    case BatchScoreStatusCode.Running:
                        Console.WriteLine(string.Format("Job {0} running...", jobId));
                        break;

                    case BatchScoreStatusCode.Failed:
                        Console.WriteLine(string.Format("Job {0} failed!", jobId));
                        Console.WriteLine(string.Format("Error details: {0}", status.Details));
                        done = true;
                        break;

                    case BatchScoreStatusCode.Cancelled:
                        Console.WriteLine(string.Format("Job {0} cancelled!", jobId));
                        done = true;
                        break;

                    case BatchScoreStatusCode.Finished:
                        done = true;
                        Console.WriteLine(string.Format("Job {0} finished!", jobId));

                        ProcessResults(status);
                        break;
                    }

                    if (!done)
                    {
                        Thread.Sleep(1000); // Wait one second
                    }
                }
            }
        }