counter is persisted
This commit is contained in:
parent
7c9bd6a700
commit
7da91b8aae
2 changed files with 24 additions and 3 deletions
|
@ -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:
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue