mirror of
https://github.com/jointakahe/takahe.git
synced 2024-11-22 15:21:01 +00:00
StatorRunner will refresh Config.system on each schedule_interval
This commit is contained in:
parent
975c205d1d
commit
9014d53399
2 changed files with 36 additions and 2 deletions
|
@ -2,6 +2,7 @@ from functools import partial
|
||||||
from typing import ClassVar
|
from typing import ClassVar
|
||||||
|
|
||||||
import pydantic
|
import pydantic
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
from django.core.files import File
|
from django.core.files import File
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.templatetags.static import static
|
from django.templatetags.static import static
|
||||||
|
@ -88,26 +89,56 @@ class Config(models.Model):
|
||||||
{"identity__isnull": True, "user__isnull": True},
|
{"identity__isnull": True, "user__isnull": True},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def aload_system(cls):
|
||||||
|
"""
|
||||||
|
Async loads the system config options object
|
||||||
|
"""
|
||||||
|
return await sync_to_async(cls.load_values)(
|
||||||
|
cls.SystemOptions,
|
||||||
|
{"identity__isnull": True, "user__isnull": True},
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load_user(cls, user):
|
def load_user(cls, user):
|
||||||
"""
|
"""
|
||||||
Loads a user config options object
|
Loads a user config options object
|
||||||
"""
|
"""
|
||||||
return cls.load_values(
|
return cls.load_values(
|
||||||
cls.SystemOptions,
|
cls.UserOptions,
|
||||||
|
{"identity__isnull": True, "user": user},
|
||||||
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def aload_user(cls, user):
|
||||||
|
"""
|
||||||
|
Async loads the user config options object
|
||||||
|
"""
|
||||||
|
return await sync_to_async(cls.load_values)(
|
||||||
|
cls.UserOptions,
|
||||||
{"identity__isnull": True, "user": user},
|
{"identity__isnull": True, "user": user},
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load_identity(cls, identity):
|
def load_identity(cls, identity):
|
||||||
"""
|
"""
|
||||||
Loads a user config options object
|
Loads an identity config options object
|
||||||
"""
|
"""
|
||||||
return cls.load_values(
|
return cls.load_values(
|
||||||
cls.IdentityOptions,
|
cls.IdentityOptions,
|
||||||
{"identity": identity, "user__isnull": True},
|
{"identity": identity, "user__isnull": True},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def aload_identity(cls, identity):
|
||||||
|
"""
|
||||||
|
Async loads an identity config options object
|
||||||
|
"""
|
||||||
|
return await sync_to_async(cls.load_values)(
|
||||||
|
cls.IdentityOptions,
|
||||||
|
{"identity": identity, "user__isnull": True},
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_value(cls, key, value, options_class, filters):
|
def set_value(cls, key, value, options_class, filters):
|
||||||
config_field = options_class.__fields__[key]
|
config_field = options_class.__fields__[key]
|
||||||
|
|
|
@ -8,6 +8,7 @@ from typing import List, Optional, Type
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from core import exceptions
|
from core import exceptions
|
||||||
|
from core.models import Config
|
||||||
from stator.models import StatorModel
|
from stator.models import StatorModel
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,6 +45,8 @@ class StatorRunner:
|
||||||
while True:
|
while True:
|
||||||
# Do we need to do cleaning?
|
# Do we need to do cleaning?
|
||||||
if (time.monotonic() - self.last_clean) >= self.schedule_interval:
|
if (time.monotonic() - self.last_clean) >= self.schedule_interval:
|
||||||
|
# Refresh the config
|
||||||
|
Config.system = await Config.aload_system()
|
||||||
print(f"{self.handled} tasks processed so far")
|
print(f"{self.handled} tasks processed so far")
|
||||||
print("Running cleaning and scheduling")
|
print("Running cleaning and scheduling")
|
||||||
for model in self.models:
|
for model in self.models:
|
||||||
|
|
Loading…
Reference in a new issue