[fix][scheduler] status of scheduler task now saved again

This commit is contained in:
Tim Gröger 2024-10-15 13:37:23 +02:00
parent 607b29027b
commit cdb7958c57
2 changed files with 11 additions and 4 deletions

View File

@ -1,6 +1,7 @@
from __future__ import annotations # TODO: Remove if python requirement is >= 3.12 (? PEP 563 is defered) from __future__ import annotations # TODO: Remove if python requirement is >= 3.12 (? PEP 563 is defered)
from typing import Any, List, Dict from typing import Any, Dict, List
from sqlalchemy.orm.collections import attribute_mapped_collection from sqlalchemy.orm.collections import attribute_mapped_collection
from ..database import db from ..database import db
@ -68,7 +69,10 @@ class BasePlugin(db.Model):
value: Value to be stored value: Value to be stored
""" """
if value is None and name in self.__settings.keys(): if value is None and name in self.__settings.keys():
del self.__settings[name] pl = self.__settings[name]
db.session.delete(pl)
else: else:
setting = self.__settings.setdefault(name, PluginSetting(plugin_id=self.id, name=name, value=None)) setting = self.__settings.setdefault(name, PluginSetting(plugin_id=self.id, name=name, value=None))
setting.value = value setting.value = value
db.session.add(setting)
db.session.commit()

View File

@ -1,6 +1,7 @@
from flask import Blueprint
from datetime import datetime, timedelta from datetime import datetime, timedelta
from flask import Blueprint
from flaschengeist import logger from flaschengeist import logger
from flaschengeist.config import config from flaschengeist.config import config
from flaschengeist.plugins import Plugin from flaschengeist.plugins import Plugin
@ -66,15 +67,17 @@ class SchedulerPlugin(Plugin):
changed = False changed = False
now = datetime.now() now = datetime.now()
status = self.get_setting("status", default=dict()) status: dict = self.get_setting("status", default=dict())
for id, task in _scheduled_tasks.items(): for id, task in _scheduled_tasks.items():
last_run = status.setdefault(id, now) last_run = status.setdefault(id, now)
status[id] = last_run
if last_run + task.interval <= now: if last_run + task.interval <= now:
logger.debug( logger.debug(
f"Run task {id}, was scheduled for {last_run + task.interval}, next iteration: {now + task.interval}" f"Run task {id}, was scheduled for {last_run + task.interval}, next iteration: {now + task.interval}"
) )
task.function() task.function()
status[id] = now
changed = True changed = True
else: else:
logger.debug(f"Skip task {id}, is scheduled for {last_run + task.interval}") logger.debug(f"Skip task {id}, is scheduled for {last_run + task.interval}")