locked
Bug with Azure Batch, initializing job object from taskitem RRS feed

  • Question

  • To add a task, as shown in the official tutorial from Microsoft, I have to make a chain of initialization. Here is the code.

    var cred = new BatchCredentials(Credentials.AzureBatch.Name, Credentials.AzureBatch.AccountKey);
            var batchClient = BatchClient.Connect(Credentials.AzureBatch.Uri, cred);
            var workItemManager = batchClient.OpenWorkItemManager();
            _job = workItemManager.GetJob(Credentials.AzureBatch.Name, "job-0000000001");

    Problem is that the code execution stops on the next line.

    _job = workItemManager.GetJob(Credentials.AzureBatch.Name, "job-0000000001");
    Then throws an exception with the description {"The remote server returned an error: (404) Not Found."}. I assume, job with the same name is not found on the server. But according to the tutorial, the name given job at its automatic creation, together with the creation of workitem. What's wrong?
    Saturday, January 10, 2015 8:27 AM

Answers

  • Hi Dmitry,

    In the code snippet you showed, I don't see where you actually create the work item, but I am assuming you created it just above somewhere (the code should look something like the below):

       ICloudWorkItem cloudWorkItem = wm.CreateWorkItem(WorkItemName);
       cloudWorkItem.JobExecutionEnvironment = new JobExecutionEnvironment() {PoolName = PoolName};
       cloudWorkItem.Commit();

    The issue is that the job creation does not happen in-line with work item creation (it happens but may take a few seconds).  What you need to do is sleep for a few seconds or even better wait for the system to say that it has successfully created a job for the given work item:

                
    private async Task<ICloudWorkItem> WaitForJobAsync(string workItemName)
    {
                ICloudWorkItem workItem = //Your work item here.
                DateTime jobCreationWaitStartTime = DateTime.UtcNow;
                while (workItem.ExecutionInformation == null || workItem.ExecutionInformation.RecentJob == null)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                    await workItem.RefreshAsync();
                    if (DateTime.UtcNow > jobCreationWaitStartTime.Add(jobCreationTimeout))
                    {
                        throw new Exception("Timed out waiting for job");
                    }
                }
    }

    We plan on publishing a helper method to assist you in doing this in a future iteration of the client library.

    Hope that helps,

    -Matt


    Monday, January 12, 2015 5:45 PM

All replies

  • Hi Dmitry,

    In the code snippet you showed, I don't see where you actually create the work item, but I am assuming you created it just above somewhere (the code should look something like the below):

       ICloudWorkItem cloudWorkItem = wm.CreateWorkItem(WorkItemName);
       cloudWorkItem.JobExecutionEnvironment = new JobExecutionEnvironment() {PoolName = PoolName};
       cloudWorkItem.Commit();

    The issue is that the job creation does not happen in-line with work item creation (it happens but may take a few seconds).  What you need to do is sleep for a few seconds or even better wait for the system to say that it has successfully created a job for the given work item:

                
    private async Task<ICloudWorkItem> WaitForJobAsync(string workItemName)
    {
                ICloudWorkItem workItem = //Your work item here.
                DateTime jobCreationWaitStartTime = DateTime.UtcNow;
                while (workItem.ExecutionInformation == null || workItem.ExecutionInformation.RecentJob == null)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                    await workItem.RefreshAsync();
                    if (DateTime.UtcNow > jobCreationWaitStartTime.Add(jobCreationTimeout))
                    {
                        throw new Exception("Timed out waiting for job");
                    }
                }
    }

    We plan on publishing a helper method to assist you in doing this in a future iteration of the client library.

    Hope that helps,

    -Matt


    Monday, January 12, 2015 5:45 PM
  • Used the workitem state to make sure workitem is active before submitting the job. Here is the code snippet.

     ICloudWorkItem cloudWorkItem = wm.CreateWorkItem(WorkItemName);
                    cloudWorkItem.JobExecutionEnvironment = new JobExecutionEnvironment() { PoolName = PoolName };
                    cloudWorkItem.Commit();
                    var detailLevel = new ODATADetailLevel() { SelectClause = "name,state,creationTime" };
                    cloudWorkItem = wm.GetWorkItem(WorkItemName, detailLevel);
                    int i = 0;
                    while (cloudWorkItem.State != WorkItemState.Active && i < 10)
                    {
                        i++;
                        Thread.Sleep(TimeSpan.FromSeconds(5));
                        cloudWorkItem = wm.GetWorkItem(WorkItemName, detailLevel);
                    }
                    ICloudJob jobs = wm.GetJob(WorkItemName, JobName);


    • Edited by Raghu VR Tuesday, March 17, 2015 11:26 AM
    Tuesday, March 17, 2015 11:25 AM