Hacking Business Central Job Queue

Job queue is one amazing tool that I've been using for ages. From NAV 2017 and so on job queue (aka Task Scheduler) has been improved and there are some amazing features.

I'm not going to talk about how it works (people from Microsoft have done a great job on documentation) but I'm going to explain how to be a little more productive and avoid some annoying errors that makes job queue experience not 100% amazing.

Service tier setup give us the chance to have concurrent threads. This is one of the biggest improvements done by far. But I think it's not really operational how it's done. Let me explain it.

We all know that BC (and NAV) may have a deadlock when posting documents if another posting routine is working at the same time. When multithread processing is enabled on the task scheduler and we're posting invoices with job queue a perfect storm may hit the system. Deadlocks will start and job queue entries will get Error status due to this.

So, how can we avoid this situation and continue working with multithread processing?

First, we'll need to add some new fields to give to the job queue entry some extra functionality

So now we'll have 4 more options:
  • Concurrency handling:
    • Disable Concurrent Run
      • If enabled, when the task scheduler tries to start as many records as threads available, the system now will check before if other job queue entry for same process is working. 
                                In that case the system will reschedule the job queue entry to be done later.
    • Disable Concurrency
      • If one entry with this check is running no other job queue entry will start until current one is finished
  • Error handling:
    • Recurrent On Error
      • When job queue processing returns any error, the system will retry later.
        • This option should be used with a little wisdom or the process will be failing forever.
    • Recurrent On Selected Errors
      • Same idea as before but only for selected errors set by the user
        • Useful when connecting to external services that may have timeouts or may have errors that we know that trying trying again the record will be processed. 
                                        It's also useful to handle deadlocks (if not concurrency new options used).
      • To set the errors, we have available from "Job Queue Entry Card / List" a new button called "Allowed Errors" where we can set the list of errors to ignore
        • It's case sensitive

So, that's the theory. I suppose that if someone readed everything above will want some prove of everything said.

To prove that, I've created a small set of job queue demo entries in order to prove all scenarios. Let's check it!!

To verify the concurrency scenario, I've created 2 codeunits that will sleep for some time and will have or not concurrency option checked. I also created a log table to store execution times and verify everything is running smoothly.

You can now check the results. It seems to be working!!!

And what about error handling??

Last but not less important. I add an incredibly difficult action that helps users to see Job Queue Entry status. The REFRESH button. Sometimes Microsoft seems to forget obvious user friendly action ;-)

You can find all the code available at https://github.com/picazin/JobQueueHack

Please feel free to clone or donwload the app and use it.