Background job every 5 seconds

Hi all,

I am planning to run the Anvil app server locally (selfhosted).

I need a background job that queries a mssql database every 5 seconds. Just to clearify, it shall work independently from the client side code, it shall only run on server side.

Is this solvable without anvil uplink?
If yes, how can this be achieved (in general)? :slight_smile:

If the way forward is anvil uplink, it will run on the same machine as the anvil app server. I just found it counter productive :slight_smile:

Thanks :pray:

Scheduled Task is the way to go.

Although a minor issue here is that Scheduled Tasks require a minimum interval of 1 minute

But if you want to still do it for every 5 seconds, there is a hack that may work.

Just schedule that task for every minute. In that task, create an infinite loop with sleep for 5 seconds. That task will be running continuously for a long time (Usually a week or two). After that time, when it vanishes, it will be restarted within a minute.

2 Likes

Thanks :slight_smile:

Interesting solution, but I have a followup question; if the scheduled task runs every minute, will it spawn a new task every minute even if I have a sleep method inside the task?

Or should I add some locking mechanisms?

No, it wonโ€™t spawn a new task while the current one is running

2 Likes

Learned something new here!

4 Likes

A little update.

For some reason the server seems to initiate twice. But only sometimes, not every time.
This sadly causes two background tasks to spawn.

I have tried implementing a โ€œlockingโ€ mechanism but for now its indeed hacky.

Does anyone have any idea why the server is initiating twice?

Warning: PDF Rendering not supported on Windows. Renderer not initialised
Connecting to ws://localhost:3030/_/downlink
Anvil websocket open
[INFO  anvil.executors.downlink] Downlink client connected with spec {:runtime "python3-full", :session_id "5WJbHVZh7legN1mxxivA"}
Downlink authenticated OK
[INFO  anvil.app-server.core] [SESSION] :background_task 2TO5SFDONKMVEKUSRWF77P4IVI2CLCFM {:func task:append_to_file, :scheduled_task VXZWZSEC}
[INFO  anvil.app-server.core] [SESSION] :background_task AQJCHTXGEA24OEPL4ZXFPWCGBUBRE4B7 {:func task:append_to_file, :scheduled_task VXZWZSEC}
Launching BG task 'task:append_to_file' for app 'LocalTest' (ID bg-FetanfQavxOSjg==)
Single-use worker <anvil_downlink_host.full_python.worker.Worker object at 0x000001EBEA273A10>
Launching BG task 'task:append_to_file' for app 'LocalTest' (ID bg-d18HXS3mzwvFtQ==)
Single-use worker <anvil_downlink_host.full_python.worker.Worker object at 0x000001EBEA2BB4D0>
[INFO  anvil.app-server.core] [SERVER] DOING SOMETHING
[INFO  anvil.app-server.core] [SERVER]
[INFO  anvil.app-server.core] [SERVER] DOING SOMETHING
[INFO  anvil.app-server.core] [SERVER]

Is the same background task in multiple runtime environments?

There is only one runtime environment, meaning I only start the app server once :slight_smile:

And btw, is anvil app server persistent? Like in the business plan.