pybackive/backive/core/scheduler.py

64 lines
1.8 KiB
Python
Raw Normal View History

2019-05-03 21:36:44 +02:00
import os
import json
2020-01-06 13:26:52 +01:00
import logging
from datetime import datetime
2019-05-03 21:36:44 +02:00
class Scheduler():
__shared_state = dict()
__data = dict()
2019-05-03 21:36:44 +02:00
def __init__(self):
self.__dict__ = self.__shared_state
if not self.__data:
# who are we?
uid = os.getuid()
if uid == 0:
2020-01-06 13:26:52 +01:00
logging.info("Executing as root.")
self._data_file = "/var/lib/backive/data.json"
else:
2020-01-06 13:26:52 +01:00
logging.info("Executing as user.")
self._data_file = os.path.join(
2019-05-03 21:36:44 +02:00
os.path.expanduser("~"),
".config",
"backive",
"data.json"
)
if not os.path.exists(os.path.dirname(self._data_file)):
os.makedirs(os.path.dirname(self._data_file))
self.save()
2019-05-03 21:36:44 +02:00
def save(self):
with open(self._data_file, "w") as stream:
json.dump(self.__data, stream, indent=2)
2019-05-03 21:36:44 +02:00
def load(self):
with open(self._data_file, "r") as stream:
self.__data = json.load(stream)
2019-05-03 21:36:44 +02:00
def register_backup(self, name, frequency):
backups = self.__data.get("backups", dict())
if not backups:
self.__data["backups"] = backups
if (
name not in backups.keys() or
backups[name] != frequency
):
backups[name] = frequency
self.save()
2019-05-03 21:36:44 +02:00
def register_run(self, name):
runs = self.__data.get("runs", dict())
if not runs:
self.__data["runs"] = runs
if name not in runs.keys():
runs[name] = [datetime.now().isoformat()]
else:
runs[name].append(datetime.now().isoformat())
self.save()
2019-05-03 21:36:44 +02:00
def should_run(self, name):
pass
def get_overtimed(self):
return list()