From 2bf4271e04e8127d1b293cf8f17baf39764aac64 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 11 Oct 2023 16:18:26 -0700 Subject: [PATCH] glib: Refactor schedule debugging in a central place Tests: - Print the interval time in schedule() method and verify that the times are as expected in develop mode and production mode. - Notification shows up for RAM usage if the check hardcoded to True. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/glib.py | 7 +++++++ plinth/modules/backups/__init__.py | 6 ++---- plinth/modules/diagnostics/__init__.py | 8 +++----- plinth/modules/dynamicdns/__init__.py | 6 ++---- plinth/modules/storage/__init__.py | 5 ++--- plinth/modules/upgrades/__init__.py | 15 ++++++--------- 6 files changed, 22 insertions(+), 25 deletions(-) diff --git a/plinth/glib.py b/plinth/glib.py index 85a48c872..e4df1ba42 100644 --- a/plinth/glib.py +++ b/plinth/glib.py @@ -9,6 +9,8 @@ import threading from plinth import dbus, network from plinth.utils import import_from_gi +from . import cfg + glib = import_from_gi('GLib', '2.0') _thread = None @@ -67,4 +69,9 @@ def schedule(interval, method, data=None, in_thread=True, repeat=True): thread.start() return repeat + # When running in development mode, reduce the interval for tasks so that + # they are triggered quickly and frequently to facilitate debugging. + if cfg.develop and interval > 180: + interval = 180 + glib.timeout_add(int(interval * 1000), _run_bare_or_thread, None) diff --git a/plinth/modules/backups/__init__.py b/plinth/modules/backups/__init__.py index f4ab92f1b..3cee6898c 100644 --- a/plinth/modules/backups/__init__.py +++ b/plinth/modules/backups/__init__.py @@ -56,10 +56,8 @@ class BackupsApp(app_module.App): @staticmethod def post_init(): """Perform post initialization operations.""" - # Check every hour (every 3 minutes in debug mode) to perform scheduled - # backups. - interval = 180 if cfg.develop else 3600 - glib.schedule(interval, backup_by_schedule) + # Check every hour to perform scheduled backups + glib.schedule(3600, backup_by_schedule) def setup(self, old_version): """Install and configure the app.""" diff --git a/plinth/modules/diagnostics/__init__.py b/plinth/modules/diagnostics/__init__.py index 7e589c6a9..0009438b9 100644 --- a/plinth/modules/diagnostics/__init__.py +++ b/plinth/modules/diagnostics/__init__.py @@ -13,7 +13,7 @@ from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_noop from plinth import app as app_module -from plinth import cfg, daemon, glib, menu +from plinth import daemon, glib, menu from plinth import operation as operation_module from plinth.modules.apache.components import diagnose_url_on_all from plinth.modules.backups.components import BackupRestore @@ -63,12 +63,10 @@ class DiagnosticsApp(app_module.App): def post_init(): """Perform post initialization operations.""" # Check periodically for low RAM space - interval = 180 if cfg.develop else 3600 - glib.schedule(interval, _warn_about_low_ram_space) + glib.schedule(3600, _warn_about_low_ram_space) # Run diagnostics once a day - interval = 180 if cfg.develop else 86400 - glib.schedule(interval, start_diagnostics, in_thread=False) + glib.schedule(24 * 3600, start_diagnostics, in_thread=False) def setup(self, old_version): """Install and configure the app.""" diff --git a/plinth/modules/dynamicdns/__init__.py b/plinth/modules/dynamicdns/__init__.py index a6904e0c5..97053fa22 100644 --- a/plinth/modules/dynamicdns/__init__.py +++ b/plinth/modules/dynamicdns/__init__.py @@ -90,10 +90,8 @@ class DynamicDNSApp(app_module.App): for domain_name in config['domains']: notify_domain_added(domain_name) - # Check every 5 minutes (every 3 minutes in debug mode) to perform - # dynamic DNS updates. - interval = 180 if cfg.develop else 300 - glib.schedule(interval, update_dns) + # Check every 5 minutes to perform dynamic DNS updates. + glib.schedule(300, update_dns) def setup(self, old_version): """Install and configure the app.""" diff --git a/plinth/modules/storage/__init__.py b/plinth/modules/storage/__init__.py index 675934f07..26b551b11 100644 --- a/plinth/modules/storage/__init__.py +++ b/plinth/modules/storage/__init__.py @@ -62,9 +62,8 @@ class StorageApp(app_module.App): @staticmethod def post_init(): """Perform post initialization operations.""" - # Check every hour for low disk space, every 3 minutes in debug mode - interval = 180 if cfg.develop else 3600 - glib.schedule(interval, warn_about_low_disk_space) + # Check every hour for low disk space + glib.schedule(3600, warn_about_low_disk_space) # Schedule initialization of UDisks2 initialization glib.schedule(3, udisks2.init, repeat=False) diff --git a/plinth/modules/upgrades/__init__.py b/plinth/modules/upgrades/__init__.py index e4ee45869..9685ce5a6 100644 --- a/plinth/modules/upgrades/__init__.py +++ b/plinth/modules/upgrades/__init__.py @@ -95,16 +95,13 @@ class UpgradesApp(app_module.App): """Perform post initialization operations.""" self._show_new_release_notification() - # Check every day (every 3 minutes in debug mode): - # - backports becomes available -> configure it if selected by user - interval = 180 if cfg.develop else 24 * 3600 - glib.schedule(interval, setup_repositories) + # Check every day if backports becomes available, then configure it if + # selected by user. + glib.schedule(24 * 3600, setup_repositories) - # Check every day (every 3 minutes in debug mode): - # - new stable release becomes available -> perform dist-upgrade if - # updates are enabled - interval = 180 if cfg.develop else 24 * 3600 - glib.schedule(interval, check_dist_upgrade) + # Check every day if new stable release becomes available, then perform + # dist-upgrade if updates are enabled + glib.schedule(24 * 3600, check_dist_upgrade) def _show_new_release_notification(self): """When upgraded to new release, show a notification."""