| 1 |
# SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
|
| 3 |
""" |
| 4 |
A class that writes persistence data to disk when device state is updated. |
| 5 |
|
| 6 |
Due to the architecture of the daemon, it's not as simple to know when |
| 7 |
something has changed, so for now, an interval will periodically check for |
| 8 |
changes in memory and write them to disk. This also avoids excessive |
| 9 |
writes when lots of variables change at once. |
| 10 |
|
| 11 |
This is essential because many desktop environments actually kill off |
| 12 |
the daemon upon logout/shutdown, thereby persistence isn't retained across |
| 13 |
sessions. |
| 14 |
|
| 15 |
A known issue is that this doesn't monitor DPI changes via hardware buttons, |
| 16 |
so this won't be persisted until the state is updated via the API. |
| 17 |
""" |
| 18 |
import time |
| 19 |
|
| 20 |
|
| 21 |
class PersistenceAutoSave(object): |
| 22 |
def __init__(self, persistence, persistence_file, persistence_status, logger, interval, persistence_save_fn): |
| 23 |
self.persistence = persistence |
| 24 |
self.persistence_file = persistence_file |
| 25 |
self.persistence_status = persistence_status |
| 26 |
self.persistence_save_fn = persistence_save_fn |
| 27 |
self.logger = logger |
| 28 |
self.interval = interval |
| 29 |
|
| 30 |
def watch(self): |
| 31 |
# Run indefinitely until process is terminated |
| 32 |
while True: |
| 33 |
time.sleep(self.interval) |
| 34 |
|
| 35 |
if self.persistence_status["changed"]: |
| 36 |
self.logger.debug("State recently changed, writing to disk") |
| 37 |
self.persistence_status["changed"] = False |
| 38 |
self.persistence_save_fn(self.persistence_file) |