django-maintenance-mode icon indicating copy to clipboard operation
django-maintenance-mode copied to clipboard

Add time window

Open stanim opened this issue 5 years ago • 10 comments

The business want to be able to specify a time window (start time and end time) in which the website turns automatically the website on and off. What would be the best method? Maybe a new setting which defines a function which is called in need_maintenance_response? Would you accept a pull request for this?

Fund with Polar

stanim avatar Nov 24 '19 17:11 stanim

@stanim I have this feature in mind since long time, but actually I have not time to implement it, so I share with you my idea in case you want to send a PR:

MAINTENANCE_MODE_SCHEDULE={
    'mon': {
        'on': '03:00', 
        'off': '03:30',
    },
    'tue': {
        'on': '14:30', 
        'off': '14:45',
    },
    # for each day, or use * for all days
}

I would add support to a new setting like this one, what do you think about it?

fabiocaccamo avatar Nov 24 '19 18:11 fabiocaccamo

The problem is that the business needs to be able to configure this from a webview. So Django settings are no solution, as settings are supposed to be immutable. Would it be possible to store the settings in the maintenance lock file?

stanim avatar Nov 24 '19 19:11 stanim

I understand your needs, but it seems a specific case. The maintenance state file is not for this, you should write a custom state backend that return values based on the configuration done in the webview. Check MAINTENANCE_MODE_STATE_BACKEND setting in the README.

fabiocaccamo avatar Nov 24 '19 21:11 fabiocaccamo

@stanim check #66 if you are interested to implement a quick schedule system by @scwall using the backend/database, or check #48 if you need to control maintenance mode state using the admin.

fabiocaccamo avatar Jan 07 '21 10:01 fabiocaccamo

I use https://django-constance.readthedocs.io/en/latest/ for configuring stuff like this. For example:

CONSTANCE_CONFIG = {
    "MAINTENANCE_ENABLED": (False, "Maintenance mode is enabled", bool),
    "MAINTENANCE_END": (datetime(2023, 11, 7, 11), "Maintenance mode end", datetime),
}

So you just need to implement MAINTENANCE_MODE_STATE_BACKEND as mentioned before to use this setting.

@fabiocaccamo Maybe you want to add this backend to your package, so people can choose the bundled backend if they use Constance?

miklevtsov avatar Nov 07 '23 15:11 miklevtsov

@miklevtsov of which backend are you talking about?

I think that this should be managed at settings level as described here https://github.com/fabiocaccamo/django-maintenance-mode/issues/66#issuecomment-578055873 and also that any backend implementation would be too specific.

fabiocaccamo avatar Nov 07 '23 15:11 fabiocaccamo

@fabiocaccamo can i contribute to this ??

Pavankumardontha avatar Dec 05 '23 23:12 Pavankumardontha

@Pavankumardontha sure, but please read my prev comment.

fabiocaccamo avatar Dec 05 '23 23:12 fabiocaccamo

@fabiocaccamo i have read the discussions. I feel webview is not possible for now since we need db and you want to keep it db free. What can i do more to contribute to this ? Any suggestion from your end ?

Pavankumardontha avatar Dec 06 '23 11:12 Pavankumardontha

@Pavankumardontha my idea is to add a new MAINTENANCE_MODE_SCHEDULE setting:

# or this to schedule maintenance every day:
MAINTENANCE_MODE_SCHEDULE = {
    '*': {'start':'01:00', 'end':'02:00'},
}

# or this to specify days
MAINTENANCE_MODE_SCHEDULE = {
    'sun': {'start':'01:00', 'end':'02:00'},
    'mon': None,
    'tue': None,
    'wed': {'start':'01:00', 'end':'02:00'},
    'thu': None,
    'fri': None,
    'sat': None,
}

Then you only need to update this function: https://github.com/fabiocaccamo/django-maintenance-mode/blob/main/maintenance_mode/http.py#L184


An alternative solution that is already supported, is to use cronjobs for calling the managements commands at specific days/hours.

fabiocaccamo avatar Dec 06 '23 14:12 fabiocaccamo