counter is persisted

This commit is contained in:
boyska 2021-09-16 02:36:00 +02:00
parent 7c9bd6a700
commit 7da91b8aae
2 changed files with 24 additions and 3 deletions

View file

@ -6,5 +6,8 @@ services:
# XXX: prima o poi servirà una qualche /var/lib/ in cui tenere il contatore
volumes:
- .:/src/
- lib:/var/lib/pizzicore/
ports:
- 8000:8000
volumes:
lib:

View file

@ -1,4 +1,5 @@
import secrets
import dbm
from collections import defaultdict
from asyncio.queues import Queue
@ -30,7 +31,22 @@ class BaseStore:
return value
class SignalStore:
class PersistStoreMixin:
def __init__(self, *args, **kwargs):
self.db_path = kwargs.pop("db_path")
super().__init__(*args, **kwargs)
with dbm.open(self.db_path, "c") as db:
for key in db.keys():
self.values[int(key)] = int(db[key])
def set(self, key, value: int) -> int:
ret = super().set(key, value)
with dbm.open(self.db_path, "w") as db:
db[str(key)] = str(value)
return ret
class SignalStoreMixin:
"""make any BaseStore manager-aware"""
def __init__(self, *args, **kwargs):
@ -44,7 +60,7 @@ class SignalStore:
return ret
class Store(SignalStore, BaseStore):
class Store(SignalStoreMixin, PersistStoreMixin, BaseStore):
pass
@ -70,7 +86,9 @@ class Manager:
app = FastAPI()
manager = Manager()
counter_store = Store(n=1, manager=manager) # XXX: pesca da file di conf
counter_store = Store(
n=1, db_path="/var/lib/pizzicore/pizzicore.dbm", manager=manager
) # XXX: pesca da file di conf
security = HTTPBasic()