So I made a Discord bot and wanted to host it on anvil, but suddently after 14 Days it just stopped the background tasks. Next try: same issue, after exact 14 days it just stops it. The code is fine, I’v looked throu it multiple times. Idk why it does it and the support couldnt help me either.
Just hoping someone here has run into this and found a fix. Thanks in advance!
I got a plan thats why i was considuring it, my bot cant go offline because it looses its settings so id need a way to rlly keep the code running 24/7
Perhaps this is relevant Background job every 5 seconds - #2 by divyeshlakhotia
The only solution to this is to either upgrade your anvil subscription or host your own anvil uplink which runs forever. (I chose the latter)
Good luck with that!
A server with a process that runs forever has yet to be invented.
You can use data tables to store those data
Also, just to make it clear, Anvil isn’t really made for this. It is meant for making web apps. There are other hosting services out there that are more easier (and even less cheaper) for discord bots than Anvil.
If you are not using anvil for its actual purpose (making web apps), you should consider something else. I only went for hosting discord bots on Anvil since I use Anvil for other stuff anyway.
I have got the buisness subscription and there is a setting named “Keep background tasks running forever” so id like to have mine running atleast longer than two weeks, how do i make the uplink?
Background Tasks and Uplinked Programs are two entirely separate approaches to running Python code. They run code in different places, under different constraints, and with different resources.
A Background Task runs your Anvil App’s server-side code on Anvil’s servers. See Background Tasks.
An Uplink Program runs on your own computer(s). Its source code is not stored in your Anvil App, but on whatever machine is running it. See Uplink: Code outside Anvil.
In both cases, circumstances beyond your control can halt the Task/Program in its tracks. For example, when running an Uplink program on your own PC, it might receive a Windows Update (today is Patch Tuesday) and forcibly reboot the computer. Or there might be an extended power or network outage.
So, even in circumstances where you own all the hardware, one should not count on computer memory being preserved indefinitely. A robust system will save its state periodically, so that when (not if) a restart is performed, it will be able to pick up where it left off.
I have had Uplink programs running for weeks at a time. Even then, after running for several weeks, their reliability began to drop, and it became wise to restart them anyway. They were designed to be restarted, so there were no ill effects.
It is possible to run multiple copies of the same Uplink program, even on different PCs, at the same time, and have them all share the load. They’re not sharing memory. It is possible for one of them to disconnect, and let the others take up the load. This can increase reliability. But for obvious reasons, nothing can guarantee 100% uptime.
Oh, i see. I have actually not tried out the business plan - and to be honest, i was expecting that the “keep background task” feature to run for very long (longer then 2 weeks).
Perhaps its worth double checking with the anvil team: support@anvil.works
Regarding the uplink i dont have a sharable code example - but the core principle is (at least for me):
- Fire up the uplink with
anvil.server.wait_forever()
in the main thread. - Start a new thread or spawn a process with your continuously running code.
anvil.server.wait_forever()
already spawns a new (Python) thread for each incoming call. So watch out for any shared global state, e.g., variables, database connections, or files that may be accessed from multiple threads. Your program might end up receiving two (or more) calls only microseconds apart.
I did end up adding a saving system to the bot but still the two week thing is kinda anoying.
You can try checking with the Anvil team, might be worth confirming with them.
But it’s just the nature of a web server: it can be restarted at any time.
The Anvil team can try to keep it running as long as possible, but I wouldn’t count on a background task lasting more than a few hours, let alone two full weeks.
It’s just how servers work, anything running on them must be ready to be restarted at any moment.
And not only will background tasks not run forever, they must also be able to recover if they get killed. All my background tasks that may run for more than a few minutes check at startup whether they were previously killed or crashed. If so, they retry the job. They also track whether it’s the first or second attempt: on the first try, they just attempt to complete the task again; on the second, they assume it was a crash and send me an email.
All my apps use the Keep server running option, which helps performance since the server doesn’t restart with every call. With that option, 99.9% of requests are fast, and just a few each day are slower. That’s a big improvement over restarting on every call, but it still doesn’t mean the server will stay up forever.
Just want it to run longer than only two weeks😭
The plan makes no guarantees about persistent server… You have to design the app to accommodate this
I use this kind of setup all the time, on various projects with anvil and without, like previously stated, there is no such thing as a process that runs forever*.
The solution to this has always been a system architecture that on some time-based periodic basis a process is set to run, and either:
- Checks to see if your “run forever” process is running, and if it is not, start it.
- Attempts to just start the “run forever” process all the time (periodically by timer) over and over, but the process itself is designed to be idempotent when starting. (only one can run at a time, starting it a second time will just make it finish instead of duplicating).
*I mean you might make the theoretically perfect, most best computer in the entire world, eventually the earth will be fried by the sun, so still will not run forever.