Timeout for Background Tasks

We have a background task for running a long production forecast. Tonight we had a case where a set of data caused the task to run in an infinite loop :weary:

Anyway the customer then started several of those tasks which caused the server to essentially run on 100% for a period of time.

While both bugs are beeing fixed - giving a background tasks a timeout when launching it would help to prevent such things.

1 Like

Hi Mark,

Wether or not Anvil will provide the option to set a time out, it seems you can communicate with background tasks (e.g. check status, kill them etc.). Maybe use a scheduled task to check the status every x minutes and kill the other task when it takes too long. This allows you to build your own simple task management solution, e.g. task type X is allowed to run for X minutes and this or that needs to happen when it runs too long, a time out or an exception occurs.

Note, I dont know the effect of a server load of 100% on the execution of scheduled tasks. But that should be fairly easy to test.

1 Like

Hi @mark.breuss,

This sounds like a useful feature! I’ll add it to our internal list.

1 Like

@gerbertdelangen I agree - this can proboably be achived with existing background task apis :ok_hand:
Proboably even with pure python: python - Timeout on a function call - Stack Overflow

3 Likes

I do this quite often similar to the method that @gerbertdelangen suggests. Others who heavily use background tasks have had many previous discussions on how they register the task objects or ID’s in tables to handle management with some kind of supervisor background task that runs as a scheduled task checking states against the table.
This could include timestamps of when the task was registered, and so could be managed to no run after a specified time, however…

This sounds like an even better idea, like something either passed in the background task decorator or the background task call itself like max_timeout=300 or something. (For a 5 minute automatic task kill/timeout)

4 Likes