Dynamics AX Batch job recurrence “Hurry up and wait”.

Since AX 2009, batch jobs have been processed by the AOS. You’ll know the batch jobs sit in a queue at Systems administration > Inquires > Batch job > Batch job:

Within a batch job you have one or more batch tasks. Many batch jobs only have one task, but if you have more than one task you can set dependencies so that the tasks are executed in sequence:

The batch job also has a recurrence – and we sometimes come across batch jobs that have been setup with a recurrence of 1 minute:

System administrators sometimes set a 1 minute recurrence on interface (AIF) or workflow batch jobs to minimise the delay experienced by users.

I’m hoping that by the end of this post I can persuade you that this is not a good idea.

We occasionally see batch jobs getting ‘stuck’ – their status is ‘Executing’ but they run for much longer than normal – in fact they just never end. The last time we had this issue reported we could see database deadlocks, and the deadlocks were processes trying to set the status on the Batch job table and on the individual tasks. There’s also an LSC issue 3272330 referring to users receiving a deadlock issue on project timesheets which happens when the workflow batch job has a recurrence of 1 minute. But even without these odd issues cropping up a look at the underlying architecture of the batch job processing system shows that a 1 minute recurrence isn’t a good idea. There’s a nice detailed blog post on this in the Microsoft Dynamics AX Technical Support Blog it was written on AX 2009 but as far as I know the description is still valid.

The highlights for me are: firstly there’s an AOS class running once every 60 seconds checking to see if there are batch jobs ready to start. That 60 seconds time interval is fixed and can’t be changed. Then the batch task is run and [eventually] updates its status to Ended. Then there is another AOS class, also running every 60 seconds that updates the batch job status – you may have noticed this yourself if you’re closely monitoring a batch job – the tasks show ‘Ended’ before the batch job itself is reset ready for the next recurrence. Finally there is a clean-up task running every 5 minutes – and also every 5 minutes the system checks the batch groups and server setup to see which batch groups (if any) should be being processed.

Don’t use a recurrence of 1 minute – it’s technically impossible inside of Dynamics AX anyway and to me it’s a gaffe like using the wrong fork at a dinner party.

So as a rule of thumb – do not use a batch job recurrence of less than 10 minutes. Give Dynamics AX a chance to reset the batch jobs and queues ready for the next recurrence.

Desert island customisations 7 of 8

Continuing my occasional series of my favourite customisations, I was reminded of this one the other day when I was setting up batch jobs for a new site.

By the way, we found out the hard way that that if you disable the user that created the batch job, the batch job stops recurring. You’ve probably guessed how we found this out, but I’ll tell you anyway: we were given our own personal log-ins, so I created batch jobs logged on as my user. Later when I left the project my user id was disabled, and oops, the batch jobs I’d created stopped recurring. But I still don’t know why it took them six weeks to notice that the master planning batch job wasn’t running. So now I create all of the ‘system’ batch jobs, like alerts, as the Admin user.

Incidentally you have to be logged on as the user that created the batch job if you want to change the parameters the job is using (or you receive an ambiguous ‘AOS validation failed’ error message) – that’s another reason for using a generic service account type user to setup your batch jobs. You don’t have to be logged on as the user that created the batch job to change the recurrence though.

So what needs customising – well the recurrence tab lets you setup a recurrence for every few minutes:

Or Hours:

But what I really want is to be able to define something like ‘Every 10 minutes, but only between the hours of 8am and 5pm, Monday to Friday”

The Monday to Friday bit could be done if we could see these ‘Weeks’ settings when we select Minutes or Hours:

And then the only other thing we need to add to this form is an ‘Ending time’ and well as a ‘Starting time’, and the system’s got all the information it needs to setup a decent batch schedule.

I couldn’t resist a quick peek at AX7 ‘the new Dynamics AX’ to see if anything has changed, but unfortunately, not in this area:

Number of documents in batch task in AX 2012

Tucked away in the Accounts receivable (and Accounts payable) parameters, on the Update tab is a little parameter called ‘Number of documents in batch task’. Accounts receivable > Setup > Accounts receivable parameters > Update:

In the Hyper-V demo image this is set to 5, but if you run a batch job to post sales order invoices you’re often going to want to set this to a much larger figure.

Here’s the explanation. But to test this we need a reasonable number of sales orders. In an earlier post here, I explained how to import sales orders using the AIF in the demo Hyper-V image and I’ve used that to setup about 50 sales orders:

So next I’ll setup a batch job to post the invoices. Accounts receivable > Periodic > Sales Update > Invoice:

There are two parts to setting up a recurring batch job for sales invoicing. The first is to tick ‘Late selection’ and click on the ‘Select’ button to define the filter query of the sales orders you want to invoice. My criteria is very simple:

But a more realistic selection would be to invoice delivered sales order lines for those customers that don’t take an invoice with their goods.

The second part of the setup is to click on the Batch button:

Tick ‘Batch processing’; enter a Task description and Batch group; and use the Recurrence button to define when and how often the job runs. Incidentally in a multi-company environment I like to add the company name to the Task description – you’ll see in a minute that the company is shown on the Batch jobs inquiry form – but you can’t use it to filter for jobs in a specific company.

Back on the Posting invoice form, when you click OK you’ll get:

There are a couple of places you can view your batch jobs. Most people will head off to Systems administration > Inquires > Batch jobs, but you can also go to Home > Area page > Inquires > Batch jobs > My batch jobs:

Above I’ve waiting until the job has run, and I’ve clicked on ‘Batch job history’ and ‘View tasks’. Now I can see that the invoices were posted by ‘Multithread posting’ jobs. In this case because I’m posting 50 invoices and I’ve set my Number of documents in batch task to 5 I’ve got 10 of these tasks.

Now let’s change that parameter to a larger number (like 500) and try that again.

This time the batch job history shows that we only used one ‘Multithread posting’ job:

It also shows in this tiny example that the run time is a few seconds longer – but that’s not our experience in production systems posting hundreds of invoices. If you’re setting up batch jobs to post purchase orders, sales order picking lists or sales invoices you’re going to want to experiment with this parameter and select the setting which gives you the best performance.