Friday, November 9, 2012

Asynchronous Job Monitoring in Active Batch

Automation frequently requires dealing with asynchronous processes. A very common example is to execute SQL server job.Let's deign a simple algorithm which can help us achieve the same.

Simple Algorithm
It’s not challenging to design an algorithm for this, simple version can be found below-
1. Run the process.
2. Get job status
3. Check if job status is finished/error/etc, if so exit else Go to Step 4.
4. Wait for polling time of 5 sec. and go to step 2.

This is very easy to implement in any programming language but it gave me some sleepless night when this same was requirement in Active Batch.

Please Note this is especially for Active Batch.

Tweaked Algorithm
The above algorithm looks perfectly fine for this type of requirement but it may lead to infinite loop if the status is never sufficient to exit job. For ex- Asyc process stopped with some status which is unknown. I will tweak the above algorithm as below which allows me to insert a failsafe.

1. Run the process.
2. Loop start (Value<Timeout)
3. Get job status
4. Check if job status is finished/error/etc., if so exit else Go to Step 4.
5. Wait for polling time of 5 sec. and go to step 2.
6. Loop end

Now the major challenge is to implement in Active batch workflow.

Earlier approach


This was earlier approach and most easiest of all.Let me explain a little bit on this -

1.Job Execute - This job is responsible to execute the async batch job.

2.Job Status - This is another plan as below-

This plan contains two jobs.As the name describes the job status fetches the job status from the batch job and passes to next job which is job status exit code. This job converts the status into meaningfull code which can be used for comparison easily.
For example -
Let say the status returned from Batch job is "Running", this job converts into 1,likewise finished is converted into 0 etc.

3.Wait - waits for specified interval of tiime.
4.Continue - performs the post execution activity  if any and exits.

The check for the wait and continue is done on the arrows connecting them like as shown -

This is simple enough to understand and based on our first algorithm and each step is implemented as 1 job.

Drawbacks of this approach

1.      The plan cannot be triggered as whole and the only way to trigger it , is to trigger the first job step. E.g. in this case "Job Execute". This opens plethora of issues like -
  •      If first job is triggered there is no way to track the execution of plan which means if the job is triggered again it will execute the whole plan again though we have the earlier executing instance. Which is not the case with Plan as; it will wait till last instance has completed execution.
  •   The logic specific to plan won’t execute since every job is treated as individual instance and executed separately as per condition. 
2.      Let say you have 10 jobs to be executed in sequence then the best practice is to create 10 separate workflows/plan and arrange them sequentially.


Since this involves the plan to be triggered this won’t work and all the job steps need to be merged into 1 single plan/workflow which is difficult to manage.

Limitation from Active Batch Framework

1. The plan trigger won't work as ASCI imposed limitation that the loop functionality won’t work if the whole plan is triggered but it can work if first job step in that workflow is triggered.

2.It’s not possible to cascade the variable between job steps so assigning a variable a specific value and checking in subsequent steps won’t work unless its two different jobs.

Refined Approach

The only way to achieve this was to push all the job steps in one single job step per batch job execution.
Let’s Design this-
Take a new job and the 5 job steps in below shown order.

·         Batch Job - This step will trigger the batch job, depending on which job is being triggered this will differ.
·         Log - This will log if the job execution failed/success.
·         For- This will be explained in details, for now this is the job which is responsible to get status from batch job and loop until the job has completed the execution.
·         Log - This will log the output of the For block
·         Exitjob- This will exit the job if the control crosses the For Job step. 

1.BatchJob - This will be responsible to invoke the batch job for first time.For Ex-

This step is using the web service to invoke a batch job.

2.  Log  step- This will just log the values which will make the execution tracking easier.

3.For Loop Funtionality

This Active batch object allows to iterate over certain number of steps.

This job contains number of sub steps. This job is responsible for -

1.GetBatchJobStatus- This will fetch the job status from the executing job. This job actually depends on logic on how the batch is executing and how we can get the Job status. In this example it was implemented by calling WCF service and this service returns the job status.

2.Log - This is important which allows to log the response of GetBatchJobStatus, which will help in troubleshooting.

3.IfElse-This object will contain the functionality which checks for the response and compare this response with a pre-defined response and depending on the value either waits and continue iterates the loop as in second case in below-

4.Log  step- This will just log the values which will make the execution tracking easier.

5.Exit Job
This job step exits the overall job with appropriate job status (Failure/Success).This should generally be failure as the job could not complete even are 100 iterations of 5 sec(configured) wait time. Depending on functionality this may be implemented in different way.


  1. Hi Anup,
    I am new to active batch . I have a filename in a variable and based on the file name I have to execute the job plan how can we do this using exit codes ?
    Thanks in advance,

  2. I will need little more detail on this. From your question what I understand is You need to selectively run the job depending on variable name(Which can be easily done by using if block in which you can use nested IF to find the job which should be executed for corresponding variable). I don't really understand the context of exit codes which you are referring.As far as your requirement is concerned have a variable which file name and I assume you are dynamically changing the file and have a IF block which checks the variable value and invokes the job. you can have multiple if blocks per values of your variable. If you are not changing the variable dynamically and have array of file names to be looped then you can use foreach loop and set variable value in last logic prior to IF block.

    For ex - (If you have array of filenames to be looped)

    IF Constructs to run job depending on variable value

    Let me know if have any questions

  3. Hi Anup ,

    I have a case where I have To just determine the job status(like GetjobStatus ) for all the jobs which are Long running/waiting to restart . Can i build an AB job for it? the version I use is Ab9 . I have worked on SSIS but have no idea of AB9 , so any help will be really appreciated