How to handle if server side anvil goes down

Hi guys,

The last friday at 6 cet there seemed that anvil was down for a short period of time.
In my case i have a background task which runs every minute and sends an email if a device is offline/online. The background task needs access to the anvil database.

The error that i get is the following.

anvil.tables.TableError: Internal database error: ERROR: canceling statement due to user request
<running on the server>
called from /downlink/anvil/_server.py, line 43
called from BackgroundTasks, line 63

Which in the context of the script is the last two lines which is failing. How can i prevent this error?

            if device_online:
                #dt_send = datetime.datetime.fromtimestamp(last_active).strftime("%Y-%m-%d %H:%M:%S")
                dt_send = ConvertLastActiveTime(last_active)
                anvil.server.call('send_error', f"{device_name} is offline. is was last active {dt_send}", device_email)
                r = app_tables.devices.get(license_code=device_serial)
                r.update(online=False)

Hi there,

The only way I can imagine it would be with a try block.
Like:

            if device_online:
                #dt_send = datetime.datetime.fromtimestamp(last_active).strftime("%Y-%m-%d %H:%M:%S")
                dt_send = ConvertLastActiveTime(last_active)
                try:
                    r = app_tables.devices.get(license_code=device_serial)
                    r.update(online=False)
                    anvil.server.call('send_error', f"{device_name} is offline. is was last active {dt_send}", device_email)
                except anvil.tables.TableError:
                    #pass to silently continue or anything else e.g. send an alarm email

This is a general untested idea but a try block is the only way I know to handle errors before stoping you code completely.

Hope I was helpfull !!!

2 Likes