Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/vorta/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

logger = logging.getLogger(__name__)

RESCHEDULE_INTERVAL_MS = 15 * 60 * 1000
WAKE_CHECK_INTERVAL_MS = 60 * 1000
WAKE_GAP_THRESHOLD = timedelta(minutes=3)


class ScheduleStatusType(enum.Enum):
SCHEDULED = enum.auto() # date provided
Expand Down Expand Up @@ -55,11 +59,10 @@ def __init__(self) -> None:
# pausing will prevent scheduling for a specified time
self.pauses: dict[int, tuple[dt, QtCore.QTimer]] = dict()

# Set additional timer to make sure background tasks stay scheduled.
# E.g. after hibernation
# Periodic reschedule, in case a run was missed
self.qt_timer = QTimer()
self.qt_timer.timeout.connect(self.reload_all_timers)
self.qt_timer.setInterval(15 * 60 * 1000)
self.qt_timer.setInterval(RESCHEDULE_INTERVAL_MS)
self.qt_timer.start()

# connect signals
Expand All @@ -81,15 +84,37 @@ def __init__(self) -> None:
self.bus = bus
self.bus.connect(service, path, interface, name, "b", self.loginSuspendNotify)
else:
logger.warning('Failed to connect to DBUS interface to detect sleep/resume events')
logger.info('No systemd-logind to notify us of sleep/resume, watching for clock gaps instead')

self._last_wake_check = dt.now()
self.wake_timer = QTimer()
self.wake_timer.timeout.connect(self.checkForResume)
self.wake_timer.setInterval(WAKE_CHECK_INTERVAL_MS)
self.wake_timer.start()

@QtCore.pyqtSlot(bool)
def loginSuspendNotify(self, suspend: bool) -> None:
if not suspend:
logger.debug("Got login suspend/resume notification")
# Defensively refetch in case the network status didn't arrive
self._net_up = self.net_status.is_network_active()
self.reload_all_timers()
self._handle_resume()

@QtCore.pyqtSlot()
def checkForResume(self) -> None:
now = dt.now()
elapsed = now - self._last_wake_check
self._last_wake_check = now

if elapsed < WAKE_GAP_THRESHOLD:
return

logger.debug('Clock jumped %s since the last wake check, assuming the machine slept', elapsed)
self._handle_resume()

def _handle_resume(self) -> None:
self._last_wake_check = dt.now()
# Defensively refetch in case the network status didn't arrive
self._net_up = self.net_status.is_network_active()
self.reload_all_timers()

@QtCore.pyqtSlot(bool)
def networkStatusChanged(self, up: bool):
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,49 @@ def test_set_timer_records_skip_when_network_down_for_catchup(clockmock):
# Re-evaluating the same missed run must not add a second row.
scheduler.set_timer_for_profile(profile.id)
assert JobModel.select().count() == jobs_before + 1


def test_wall_clock_gap_is_treated_as_a_resume(mocker, clockmock):
"""Without logind, a jump in wall clock time is the only sign that the machine slept."""
clockmock.now.return_value = dt(2020, 5, 6, 4, 0)
scheduler = VortaScheduler()
# qapp keeps every scheduler alive, and this one's baseline is 2020
scheduler.wake_timer.stop()
reload_all = mocker.patch.object(scheduler, 'reload_all_timers')
scheduler.net_status = MagicMock()
scheduler.net_status.is_network_active.return_value = True
scheduler._net_up = False

clockmock.now.return_value = dt(2020, 5, 6, 6, 0)
scheduler.wake_timer.timeout.emit()

reload_all.assert_called_once()
assert scheduler._net_up is True


def test_timely_wake_check_does_not_reschedule(mocker, clockmock):
"""An on-time check must do nothing, or every profile gets rescheduled once a minute."""
clockmock.now.return_value = dt(2020, 5, 6, 4, 0)
scheduler = VortaScheduler()
scheduler.wake_timer.stop()
reload_all = mocker.patch.object(scheduler, 'reload_all_timers')

clockmock.now.return_value = dt(2020, 5, 6, 4, 1)
scheduler.wake_timer.timeout.emit()

reload_all.assert_not_called()


def test_logind_resume_signal_reloads_timers(mocker, clockmock):
"""The logind fast path must survive the resume body moving into a helper."""
clockmock.now.return_value = dt(2020, 5, 6, 4, 0)
scheduler = VortaScheduler()
scheduler.wake_timer.stop()
reload_all = mocker.patch.object(scheduler, 'reload_all_timers')
scheduler.net_status = MagicMock()

scheduler.loginSuspendNotify(True)
reload_all.assert_not_called()

scheduler.loginSuspendNotify(False)
reload_all.assert_called_once()
Loading