1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Refactor ServerManager counter_storage method to clear counters if they are too old

This commit is contained in:
Adolfo Gómez García 2024-09-26 19:10:38 +02:00
parent fa31a30ba2
commit 4af39858bd
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23

View File

@ -60,7 +60,7 @@ class ServerManager(metaclass=singleton.Singleton):
BASE_PROPERTY_NAME: typing.Final[str] = 'sm_usr_'
# Singleton, can initialize here
last_counters_clean: datetime.datetime = model_utils.sql_now()
last_counters_clean: datetime.datetime = datetime.datetime.now() # This is local to server, so it's ok
@staticmethod
def manager() -> 'ServerManager':
@ -70,7 +70,8 @@ class ServerManager(metaclass=singleton.Singleton):
def counter_storage(self) -> typing.Iterator[StorageAsDict]:
with Storage(self.STORAGE_NAME).as_dict(atomic=True, group='counters') as storage:
# If counters are too old, restart them
if model_utils.sql_now() - self.last_counters_clean > self.MAX_COUNTERS_AGE:
if datetime.datetime.now() - self.last_counters_clean > self.MAX_COUNTERS_AGE:
self.last_counters_clean = datetime.datetime.now()
storage.clear()
yield storage