mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Bugfix for auto-backup task (#4406)
* Bugfix for auto-backup task - Dont' mix datetime with timezone * Use different variable names - If old names are stuck in the database, error could still occur * Exclude internal settings from API * Make INVENTREE_LATEST_VERSION setting an internal one
This commit is contained in:
parent
f523fb44f6
commit
fd84e590ec
@ -377,7 +377,7 @@ def check_for_updates():
|
|||||||
|
|
||||||
# Save the version to the database
|
# Save the version to the database
|
||||||
common.models.InvenTreeSetting.set_setting(
|
common.models.InvenTreeSetting.set_setting(
|
||||||
'INVENTREE_LATEST_VERSION',
|
'_INVENTREE_LATEST_VERSION',
|
||||||
tag,
|
tag,
|
||||||
None
|
None
|
||||||
)
|
)
|
||||||
@ -440,11 +440,11 @@ def run_backup():
|
|||||||
time.sleep(random.randint(1, 5))
|
time.sleep(random.randint(1, 5))
|
||||||
|
|
||||||
# Check for records of previous backup attempts
|
# Check for records of previous backup attempts
|
||||||
last_attempt = InvenTreeSetting.get_setting('INVENTREE_BACKUP_ATTEMPT', '', cache=False)
|
last_attempt = InvenTreeSetting.get_setting('_INVENTREE_BACKUP_ATTEMPT', '', cache=False)
|
||||||
last_success = InvenTreeSetting.get_setting('INVENTREE_BACKUP_SUCCESS', '', cache=False)
|
last_success = InvenTreeSetting.get_setting('_INVENTREE_BACKUP_SUCCESS', '', cache=False)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
backup_n_days = int(InvenTreeSetting.get_setting('INVENTREE_BACKUP_DAYS', 1, cache=False))
|
backup_n_days = int(InvenTreeSetting.get_setting('_INVENTREE_BACKUP_DAYS', 1, cache=False))
|
||||||
except Exception:
|
except Exception:
|
||||||
backup_n_days = 1
|
backup_n_days = 1
|
||||||
|
|
||||||
@ -456,14 +456,14 @@ def run_backup():
|
|||||||
|
|
||||||
if last_attempt:
|
if last_attempt:
|
||||||
# Do not attempt if the 'last attempt' at backup was within 12 hours
|
# Do not attempt if the 'last attempt' at backup was within 12 hours
|
||||||
threshold = timezone.now() - timezone.timedelta(hours=12)
|
threshold = datetime.now() - timedelta(hours=12)
|
||||||
|
|
||||||
if last_attempt > threshold:
|
if last_attempt > threshold:
|
||||||
logger.info('Last backup attempt was too recent - skipping backup operation')
|
logger.info('Last backup attempt was too recent - skipping backup operation')
|
||||||
return
|
return
|
||||||
|
|
||||||
# Record the timestamp of most recent backup attempt
|
# Record the timestamp of most recent backup attempt
|
||||||
InvenTreeSetting.set_setting('INVENTREE_BACKUP_ATTEMPT', timezone.now().isoformat(), None)
|
InvenTreeSetting.set_setting('_INVENTREE_BACKUP_ATTEMPT', datetime.now().isoformat(), None)
|
||||||
|
|
||||||
if not last_attempt:
|
if not last_attempt:
|
||||||
# If there is no record of a previous attempt, exit quickly
|
# If there is no record of a previous attempt, exit quickly
|
||||||
@ -479,7 +479,7 @@ def run_backup():
|
|||||||
|
|
||||||
# Exit early if the backup was successful within the number of required days
|
# Exit early if the backup was successful within the number of required days
|
||||||
if last_success:
|
if last_success:
|
||||||
threshold = timezone.now() - timezone.timedelta(days=backup_n_days)
|
threshold = datetime.now() - timedelta(days=backup_n_days)
|
||||||
|
|
||||||
if last_success > threshold:
|
if last_success > threshold:
|
||||||
logger.info('Last successful backup was too recent - skipping backup operation')
|
logger.info('Last successful backup was too recent - skipping backup operation')
|
||||||
@ -489,7 +489,7 @@ def run_backup():
|
|||||||
call_command("mediabackup", noinput=True, clean=True, compress=True, interactive=False)
|
call_command("mediabackup", noinput=True, clean=True, compress=True, interactive=False)
|
||||||
|
|
||||||
# Record the timestamp of most recent backup success
|
# Record the timestamp of most recent backup success
|
||||||
InvenTreeSetting.set_setting('INVENTREE_BACKUP_SUCCESS', datetime.now().isoformat(), None)
|
InvenTreeSetting.set_setting('_INVENTREE_BACKUP_SUCCESS', datetime.now().isoformat(), None)
|
||||||
|
|
||||||
|
|
||||||
def send_email(subject, body, recipients, from_email=None, html_message=None):
|
def send_email(subject, body, recipients, from_email=None, html_message=None):
|
||||||
|
@ -108,12 +108,12 @@ class InvenTreeTaskTests(TestCase):
|
|||||||
def test_task_check_for_updates(self):
|
def test_task_check_for_updates(self):
|
||||||
"""Test the task check_for_updates."""
|
"""Test the task check_for_updates."""
|
||||||
# Check that setting should be empty
|
# Check that setting should be empty
|
||||||
self.assertEqual(InvenTreeSetting.get_setting('INVENTREE_LATEST_VERSION'), '')
|
self.assertEqual(InvenTreeSetting.get_setting('_INVENTREE_LATEST_VERSION'), '')
|
||||||
|
|
||||||
# Get new version
|
# Get new version
|
||||||
InvenTree.tasks.offload_task(InvenTree.tasks.check_for_updates)
|
InvenTree.tasks.offload_task(InvenTree.tasks.check_for_updates)
|
||||||
|
|
||||||
# Check that setting is not empty
|
# Check that setting is not empty
|
||||||
response = InvenTreeSetting.get_setting('INVENTREE_LATEST_VERSION')
|
response = InvenTreeSetting.get_setting('_INVENTREE_LATEST_VERSION')
|
||||||
self.assertNotEqual(response, '')
|
self.assertNotEqual(response, '')
|
||||||
self.assertTrue(bool(response))
|
self.assertTrue(bool(response))
|
||||||
|
@ -64,9 +64,9 @@ def inventreeDocsVersion():
|
|||||||
def isInvenTreeUpToDate():
|
def isInvenTreeUpToDate():
|
||||||
"""Test if the InvenTree instance is "up to date" with the latest version.
|
"""Test if the InvenTree instance is "up to date" with the latest version.
|
||||||
|
|
||||||
A background task periodically queries GitHub for latest version, and stores it to the database as INVENTREE_LATEST_VERSION
|
A background task periodically queries GitHub for latest version, and stores it to the database as "_INVENTREE_LATEST_VERSION"
|
||||||
"""
|
"""
|
||||||
latest = common.models.InvenTreeSetting.get_setting('INVENTREE_LATEST_VERSION', backup_value=None, create=False)
|
latest = common.models.InvenTreeSetting.get_setting('_INVENTREE_LATEST_VERSION', backup_value=None, create=False)
|
||||||
|
|
||||||
# No record for "latest" version - we must assume we are up to date!
|
# No record for "latest" version - we must assume we are up to date!
|
||||||
if not latest:
|
if not latest:
|
||||||
|
@ -187,7 +187,7 @@ class SettingsList(ListAPI):
|
|||||||
class GlobalSettingsList(SettingsList):
|
class GlobalSettingsList(SettingsList):
|
||||||
"""API endpoint for accessing a list of global settings objects."""
|
"""API endpoint for accessing a list of global settings objects."""
|
||||||
|
|
||||||
queryset = common.models.InvenTreeSetting.objects.all()
|
queryset = common.models.InvenTreeSetting.objects.exclude(key__startswith="_")
|
||||||
serializer_class = common.serializers.GlobalSettingsSerializer
|
serializer_class = common.serializers.GlobalSettingsSerializer
|
||||||
|
|
||||||
|
|
||||||
@ -216,7 +216,7 @@ class GlobalSettingsDetail(RetrieveUpdateAPI):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
lookup_field = 'key'
|
lookup_field = 'key'
|
||||||
queryset = common.models.InvenTreeSetting.objects.all()
|
queryset = common.models.InvenTreeSetting.objects.exclude(key__startswith="_")
|
||||||
serializer_class = common.serializers.GlobalSettingsSerializer
|
serializer_class = common.serializers.GlobalSettingsSerializer
|
||||||
|
|
||||||
def get_object(self):
|
def get_object(self):
|
||||||
|
@ -180,6 +180,10 @@ class BaseInvenTreeSetting(models.Model):
|
|||||||
"""
|
"""
|
||||||
results = cls.objects.all()
|
results = cls.objects.all()
|
||||||
|
|
||||||
|
if exclude_hidden:
|
||||||
|
# Keys which start with an undersore are used for internal functionality
|
||||||
|
results = results.exclude(key__startswith='_')
|
||||||
|
|
||||||
# Optionally filter by user
|
# Optionally filter by user
|
||||||
if user is not None:
|
if user is not None:
|
||||||
results = results.filter(user=user)
|
results = results.filter(user=user)
|
||||||
|
Loading…
Reference in New Issue
Block a user