コード例 #1
0
        /*
         * static void SaveBlobToFile(AzureBlobDataReference blobLocation, string resultsLabel, TraceWriter log)
         * {
         *  const string OutputFileLocation = @"C:Temp\myresultsfile.ilearner"; // Replace this with the location you would like to use for your output file, and valid file extension (usually .csv for scoring results, or .ilearner for trained models)
         *
         *  var credentials = new StorageCredentials(blobLocation.SasBlobToken);
         *  var blobUrl = new Uri(new Uri(blobLocation.BaseLocation), blobLocation.RelativeLocation);
         *  var cloudBlob = new CloudBlockBlob(blobUrl, credentials);
         *
         *  log.Info(string.Format("Reading the result from {0}", blobUrl.ToString()));
         *  cloudBlob.DownloadToFile(OutputFileLocation, FileMode.Create);
         *
         *  log.Info(string.Format("{0} have been written to the file {1}", resultsLabel, OutputFileLocation));
         * }*/

        static void ProcessResults(BatchScoreStatus status, TraceWriter log)
        {
            ;
            foreach (var output in status.Results)
            {
                var blobLocation = output.Value;
                log.Info(string.Format("The result '{0}' is available at the following Azure Storage location:", output.Key));
                log.Info(string.Format("BaseLocation: {0}", blobLocation.BaseLocation));
                log.Info(string.Format("RelativeLocation: {0}", blobLocation.RelativeLocation));
                log.Info(string.Format("SasBlobToken: {0}", blobLocation.SasBlobToken));
                log.Info("");
            }
        }
コード例 #2
0
        static async Task InvokeBatchExecutionService(string name, Stream blob, TraceWriter log)
        {
            // Web Service Info
            string BaseUrl = ConfigurationManager.AppSettings.Get("SUPPLY_DEMAND_PREDICT_BATCH_LEARNING_API_URL");
            string apiKey  = ConfigurationManager.AppSettings.Get("SUPPLY_DEMAND_PREDICT_BATCH_LEARNING_API_KEY");

            // Storage Account Info
            string StorageAccountName          = ConfigurationManager.AppSettings.Get("STORAGE_ACCOUNT_NAME_4_BATCH_LEARNING");
            string StorageAccountKey           = ConfigurationManager.AppSettings.Get("STORAGE_ACCOUNT_KEY_4_BATCH_LEARNING");
            string StorageContainerName4Input  = ConfigurationManager.AppSettings.Get("STORAGE_CONTAINER_NAME_4_BATCH_LEARNING_INPUT");
            string StorageContainerName4Output = ConfigurationManager.AppSettings.Get("STORAGE_CONTAINER_NAME_4_BATCH_LEARNING_OUTPUT");
            string storageConnectionString     = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageAccountName, StorageAccountKey);

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

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

                    Outputs = new Dictionary <string, AzureBlobDataReference>()
                    {
                        {
                            "output2",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("{0}/output2results.ilearner", StorageContainerName4Output) /* output file (.csv for scoring results, or .ilearner for trained models) */
                            }
                        },
                        {
                            "output1",
                            new AzureBlobDataReference()
                            {
                                ConnectionString = storageConnectionString,
                                RelativeLocation = string.Format("{0}/output1results.csv", StorageContainerName4Output) /*output file (.csv for scoring results, or .ilearner for trained models) */
                            }
                        },
                    },

                    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)

                log.Info("Submitting the job...");

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

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

                    return;
                }

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

                log.Info(string.Format("Job ID: {0}", jobId));

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

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

                    return;
                }

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

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

                        return;
                    }

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

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

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

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

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

                    case BatchScoreStatusCode.Finished:
                        done = true;
                        log.Info(string.Format("Job {0} finished!", jobId));
                        ProcessResults(status, log);
                        break;
                    }

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