Skip to main content

Notifications

Queuing Background Tasks

dkatson Profile Picture dkatson 2,261

I recently faced an interesting problem. The customer had production terminal at the end of a line, were the operator proceed the output. When he marked the production order as completed he clicked the button on the terminal and the request to post the output was sent to Business Central. For that I created the custom API. Nothing special.

On a very high level this looked like this:

5327.pastedimage1619495658415v1.png

Very strait-forward, worked like a sharm... until we started to test this on the floor. It appeared (oh my god who could predict that :) that the responses could be not only successful, there could be the errors as well. And the reasons were in 100% cases in Business Central: no location code, date is closed, item is blocked and so many other things related exclusively to Business Central itself. And of course the operator near the terminal had nothing to do with that! It's completely outside of his responsibilities and knowledge. He is responsible for the production line and his terminal, and the financial people are responsible for Business Central data to successfully register the output.  

So, what could be the possible solution? Split the responsibilities! And as a consequence - split the Request and PostOutput. I created the staging table, which i exposed as an API. I got the requests, saved and then immediately ran the background task to post the output. The errors I saved to the error log table with the manual opportunity to solve error in BC and run the post output once again. 

On a high level the first diagram changed into this one:

0247.pastedimage1619497327995v2.png

A bit more complicated, but still strait-forward. Operator was happy, financials were happy. Me was happy as well :)

However things become to break when there appeared more than one operator. Did you guess what happened?

A Locking problem!

When multiply terminals were sending requests to my BC CustomStaging API - all was perfect. I just saved the requests - that's it. But, when I run the background task for each individual request record to post output we stucked into the locking problem. As a result the error log was full of errors not related to BC data itself. And financials users had to manually start output process once again, at the same time when the operators were sending requests from terminals... This just had to be resolved somehow. 

Queuing Background Tasks

That could be a solution. But not possible ... outside of the box. Anyway, let's try to implement this. 

Just for simplicity, i will show the logic on a slight different example. 

Creating Items through API in a background

I have a staging table. Here I will save incoming requests. For the simplicity - only Item description.

8272.pastedimage1619498795236v3.png

Also created the API page. When I receive the request I save it to the staging table (line 25), and run the Background task to create item (line 28). In case of errors I save the error to the error log.

2045.pastedimage1619499959777v9.png

The background task logic looks like this. I create the item and delete the staging entry. To simulate the hard work and to get into the the locking problem during multiply calls in (that i will show in a minute) i also do such strange stuff in the beginning. 

6355.pastedimage1619504589808v20.png

In case of errors I save the error in my own error log entry table

2502.pastedimage1619501988713v15.png

5025.pastedimage1619502056840v16.png

Let's try to run this

0336.pastedimage1619500304045v10.png

And in Business Central we will see new Item. 

8836.pastedimage1619500373599v11.png

So far so good.

Creating multiply Items through batch API in a background

Now let's try simulate several services creating items in BC in a parallel. For that we will use batch calls.

2804.pastedimage1619500979909v12.png

The initial services received success responses. However if we look at Business Central items list we will see

8484.pastedimage1619504301726v18.png

Only 1 new item was created! Were is the second one? Let's go and see the error log table that we created.

5086.pastedimage1619504378100v19.png

And here we come into the necessity of queuing background tasks.

Creating multiply Items through batch API in a background with queuing 

The thing is that you cannot queue background tasks. There only two parameters that you can control : IsReady, NotBefore.

NotBefore will not help us, cos we don't know when to start the task. The incoming requests could come continuously, the execution time of each task could be different, there could be posting process inside business central itself. It's just not possible to calculate when task should be started. 

Let's have a closer look at IsReady. 

Sets the task to the ready state. A task cannot run unless it is ready.

Here it is! We can set the IsReady to true for the first task in the list, and for the all other tasks we will set IsReady to false. So the first task will be executed and the other tasks will be set OnHold. When the first task will be executed, we will find the second task and set the IsReady to true. And so we will start the loop. Simple and elegant!

First we will change the TaskScheduler.CreateTask into this

3652.pastedimage1619506022057v21.png

and when the first task will be executed we will mark the next one as IsReady.

8311.pastedimage1619506286753v22.png

Let's try to create two items in a batch now.

5078.Screenshot-2021_2D00_04_2D00_27-at-13.59.27.png

So far so good. And in Business Central

2821.pastedimage1619506871148v28.png

Great! Hope this will help some of you

The sourcecode is available here https://github.com/dkatson/Blog-Queuing-background-tasks 

Comments

*This post is locked for comments