Skip to main content

Azure Batch Service - A job that runs an .exe application (part 2)

In the last post related to Azure Batch we talk about the theoretical parts. We discover what kind of batch tasks we can have, what is a merge task and many more.
In this blog post we will focus on how we can create a batch and how we can push our own logic into a batch. We will focus on Azure Batch using a .NET library for now – running an .exe application as a task logic.

Base Terminology
Before going forward it would a good idea to review the batch terminology:
Account - A uniquely identified entity within the service. All processing is done through a Batch account.
Pool – A collection of task virtual machines on which task applications run.
Task Virtual Machine - A machine that is assigned to a pool and is used by tasks that are assigned to jobs that run in the pool.
Task Virtual Machine User – A user account on a task virtual machine.
Workitem - Specifies how computation is performed on task virtual machines in a pool.
Job - A running instance of a work item and consists of a collection of tasks.
Task - An application that is associated with a job and runs on a task virtual machine.
File – Contains the information that is processed by a task.

Azure Batch SDK
Before starting, you should download from Visual Studio Gallery (Tools->Extensions and Updates), the SDK for Azure Batch. This will contain a template that can be used to create a new Batch.

Create an Azure Batch Project
To be able to define an Azure Batch you will need to create a new VS project of type “Azure Batch Embarrassingly Parallel Cloud Application”. First step is to specify the Application Name and Job Name.

Entry Point
This information can be changed later on from ‘ApplicationDefinition’ that represent the main entry point in the application. Using this definition we can control:

  • Name of the Job
  • Name of the Application
  • Job Splitter – This step gives us the possibility to split a job in multiple tasks
  • Task Processor – The task logic itself 
public class ApplicationDefinition
    public static readonly CloudApplication Application = new ParallelCloudApplication
            ApplicationName = "FooApp",
            JobType = "FooJob",
            JobSplitterType = typeof(AzureBatchCloudApplication1JobSplitter),
            TaskProcessorType = typeof(AzureBatchCloudApplication1TaskProcessor)

Remarks: This template can be very useful, because we can hook directly our own logic, simple and clean.

Job Splitter
As the name says, this class will split your job in subtask. In this way you can run in parallel multiple tasks. Things that you should remember:

  • Task index - Is optionally, is used only to specify an index to each task
  • Task ID – Represent the Id of each task, you should specified this value
  • Parameters – Is a list of parameters that can be used to transmit different parameters to a task
  • RequiredFiles – Can be used to specify what files are needs to run the task
  • DependsOn – It is used when you want to specify that a task depends on another task. The ID of the depending task needs to be specified. In this way the current task will not start until the depending task has not run.
  • State – Is used to group multiple tasks in the same group. It is similar win ‘DependsOn’, but at collection level. In this way you can create a groups of tasks that will run in the order of State value. First, Azure Batch will run in parallel tasks with State value equal to 0, after that the one equal with 1 and so on. Using both solutions (DependsOn or State) you can create different execution model (similar with trees).
  • IsMerge – There can be only one task of this kind and is used to merge the result of the tasks. This task needs to be executed as the last task (by default, you don’t need to define this task, there is a default merge tasks implemented).
 protected override IEnumerable<TaskSpecifier> Split(IJob job, JobSplitSettings settings)
            return new List<TaskSpecifier>
                    new TaskSpecifier
                            RequiredFiles = job.Files,
                            TaskId = 1,
                            TaskIndex = 1,
                            State = 0
                            Parameters = job.Parameters,
                    new TaskSpecifier
                            RequiredFiles = job.Files,
                            TaskId = 2,
                            TaskIndex = 2,
                            DependsOn = 1,
                            State = 0
                            Parameters = job.Parameters,
                    new TaskSpecifier
                            RequiredFiles = job.Files,
                            TaskId = 2,
                            TaskIndex = 2,
                            DependsOn = 1,
                            State = 1
                            Parameters = job.Parameters,

Define a task (ParallelTaskProcessor.RunExternalTaskProcess)
At this level you define the task itselft. In our case, at this level you need to specify the executable that contains the logic of task, input and output files. Don’t forget that input and output of each task is based on files (from blob storage).
When you can execute an external executable you can specify the fallowing information:

  • CommandPath – The path to the executable file (the task logic)
  • Arguments – The arguments that are send to executable file
  • WorkingDirectory – The directory used as working directory
  • CancellationToken – You obtain it from TaskExecutionSettings.CancellationToken and is used to transmit the cancelation token to you executable.
protected override TaskProcessResult RunExternalTaskProcess(ITask task, TaskExecutionSettings settings)
    string inputFile = task.RequiredFiles[0].Name;
    string outputFile = string.Format("{0}.scv", task.TaskId);

    ExternalProcess process = new ExternalProcess
            CommandPath = ExecutablePath("fooTask.exe"),
            Arguments = string.Format("input:{0} output:{1}", inputFile, outputFile),
            WorkingDirectory = LocalStoragePath

    ExternalProcessResult processOutput = process.Run();

    return TaskProcessResult.FromExternalProcessResult(processOutput);
At this level, based on how you define your job splitter, each task can use different input files and to generate different outputs.

Merge Task (ParallelTaskProcessor.RunExternalMergeProcess)
To implement the merge task you will need to override the above method. In theory this task should take all the output files from all tasks and merge it in only one task. The default merge task is merging all output files in one result.
protected override JobResult RunExternalMergeProcess(ITask mergeTask, TaskExecutionSettings settings)

In this post we saw how we can define a job that runs an Azure Batch Application. In the next post we will see how we can define an Azure Batch job using a library and not an .exe application


Popular posts from this blog

How to check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

Fundamental Books of a Software Engineer (version 2018)

More then six years ago I wrote a blog post about fundamental books that any software engineer (developer) should read. Now it is an excellent time to update this list with new entries.

There are 5 different categories of books, that represent the recommended path. For example, you start with Coding books, after that, you read books about Programming, Design and so on.
There are some books about C++ that I recommend not because you shall know C++, only because the concepts that you can learn from it.


Writing solid codeCode completeProgramming Pearls, more programming pearls(recommended)[NEW] Introduction to Algorithms


Refactoring (M. Fowler)Pragmatic ProgrammerClean code[NEW] Software Engineering: A Practitioner's Approach[NEW] The Mythical Man-Month[NEW] The Art of Computer Programming


Applying UML and Patterns (GRASP patterns)C++ coding standards (Sutter, Alexandrescu)The C++ programming language (Stroustrup, Part IV)Object-oriented programming (Peter Coad)P…