Repository: inventree/InvenTree

Issue:
Issues with maintenance_mode.txt
### Discussed in https://github.com/inventree/InvenTree/discussions/3617

<div type='discussions-op-text'>

<sup>Originally posted by **miggland** August 29, 2022</sup>
My Inventree server crashed today, while not in active use.

I have tried restarting the containers, but no luck yet.

------------------------------------------

There are two main issues here:

### Multiple File Copies

Multiple copies of the maintenance file are being created - should be limited to a single copy which gets overwritten.

### Error Handling

maintenance_mode code crashes if the file is empty


Discussion:
@miggland some questions for reproducing:
- a) Are you running in a cluster with +1 server (or gunicorn) instances?
- b) Are you running multiple maintenance scripts (invoke tasks) at the same time?
- c) Is the server stopped when you run maintenance scripts?
- d) Do you have plugins installed and enabled?
- e) Are you monitoring your instance (via webcrawler, auto-check etc - important thing is that a http/s call is made to the site)?
> @miggland some questions for reproducing:
> 
>     * a) Are you running in a cluster with +1 server (or gunicorn) instances?

No

>     * b) Are you running multiple maintenance scripts (invoke tasks) at the same time?

Not as far as I'm aware of.

>     * c) Is the server stopped when you run maintenance scripts?

Not always - I run `export records` when the server is running, for example. After an update, I usually run `invoke update` before starting all the containers.

>     * d) Do you have plugins installed and enabled?

Enabled yes, but none are installed - see the discussion topic; the install failed for the plugin I was trying to install.

>     * e) Are you monitoring your instance (via webcrawler, auto-check etc - important thing is that a http/s call is made to the site)?

No.

Ran into the same issue today, deleting all the `maintenance_mode_state*` files in the media folder fixes the issue for now


@matmair what should we do here? In particular I would like to prevent the multiple copies of maintenance_mode.txt file:

![image](https://user-images.githubusercontent.com/10080325/200980998-09898f23-f01b-4adb-84de-b156943c7bfd.png)

@SchrodingersGat I am not really sure where this comes from. Normally this should be handled by the library. Maybe we should move the file to static? Could be a problem with the location being in media.
I think it is going to be related to being in the media dir. Perhaps moving to static would be sufficient?
@SchrodingersGat I still was not able to reproduce this while running 50+ instances.
Sentry threw an error on the demo server today:

![image](https://user-images.githubusercontent.com/10080325/203678409-e9b3be2c-dd32-43e7-b6cf-3aca0147f66b.png)

@SchrodingersGat well this is very helpful. I should turn this on on my instances again - I got too many transactions for the free tier.
I will work out a PR. It should work if we move it to static and ensure the file exists on startup.
Sooo... I created a PR to upstream https://github.com/fabiocaccamo/django-maintenance-mode/pull/97
I think we should ship that change as part of InvenTree for now as it seems to fix the problems with multiple files etc.
@SchrodingersGat thoughts?

Resolving patch:
diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py
--- a/InvenTree/InvenTree/settings.py
+++ b/InvenTree/InvenTree/settings.py
@@ -826,7 +826,7 @@
 
 # Maintenance mode
 MAINTENANCE_MODE_RETRY_AFTER = 60
-MAINTENANCE_MODE_STATE_BACKEND = 'maintenance_mode.backends.DefaultStorageBackend'
+MAINTENANCE_MODE_STATE_BACKEND = 'maintenance_mode.backends.StaticStorageBackend'
 
 # Are plugins enabled?
 PLUGINS_ENABLED = get_boolean_setting('INVENTREE_PLUGINS_ENABLED', 'plugins_enabled', False)


Test patch: