Skip to content

Commit

Permalink
add disconnect and trigger for read only db error
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Jul 7, 2024
1 parent b6d05e8 commit f9dace8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cashu/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ async def _handle_lock_retry(retry_delay, timeout, start_time) -> float:
return retry_delay

def _is_lock_exception(e):
if "database is locked" in str(e) or "could not obtain lock" in str(e):
if (
"database is locked" in str(e)
or "could not obtain lock" in str(e)
or "already locked" in str(e)
or "attempt to write a readonly database" in str(e)
):
logger.trace(f"Lock exception: {e}")
return True

Expand Down
6 changes: 6 additions & 0 deletions cashu/mint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ..core.settings import settings
from .router import router
from .router_deprecated import router_deprecated
from .startup import shutdown_mint as shutdown_mint_init
from .startup import start_mint_init

if settings.debug_profiling:
Expand Down Expand Up @@ -103,3 +104,8 @@ async def catch_exceptions(request: Request, call_next):
@app.on_event("startup")
async def startup_mint():
await start_mint_init()


@app.on_event("shutdown")
async def shutdown_mint():
await shutdown_mint_init()
5 changes: 5 additions & 0 deletions cashu/mint/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,8 @@ async def start_mint_init():
await ledger.startup_ledger()
logger.info("Mint started.")
# asyncio.create_task(rotate_keys())


async def shutdown_mint():
if ledger.db._connection:
await ledger.db._connection.close()

0 comments on commit f9dace8

Please sign in to comment.