Fixed error on trying to send ws message to disconnected client

This could result in a major filament usage track failure since the Use http request would fail and Moonraker would keep retrying to save the used filament. However it was actually saved since the db.commit() happens before the websocket message fails.
This commit is contained in:
Donkie
2024-08-16 10:28:04 +02:00
parent 5c5bd4627b
commit e5f79493cc
4 changed files with 32 additions and 12 deletions

View File

@@ -92,8 +92,8 @@ async def create(
extra=[models.SpoolField(key=k, value=v) for k, v in (extra or {}).items()],
)
db.add(spool)
await db.commit()
await spool_changed(spool, EventType.ADDED)
await db.commit()
return spool
@@ -228,16 +228,16 @@ async def update(
spool.extra = [models.SpoolField(key=k, value=v) for k, v in v.items()]
else:
setattr(spool, k, v)
await db.commit()
await spool_changed(spool, EventType.UPDATED)
await db.commit()
return spool
async def delete(db: AsyncSession, spool_id: int) -> None:
"""Delete a spool object."""
spool = await get_by_id(db, spool_id)
await db.delete(spool)
await spool_changed(spool, EventType.DELETED)
await db.delete(spool)
async def clear_extra_field(db: AsyncSession, key: str) -> None:
@@ -291,8 +291,8 @@ async def use_weight(db: AsyncSession, spool_id: int, weight: float) -> models.S
spool.first_used = datetime.utcnow().replace(microsecond=0)
spool.last_used = datetime.utcnow().replace(microsecond=0)
await db.commit()
await spool_changed(spool, EventType.UPDATED)
await db.commit()
return spool
@@ -337,8 +337,12 @@ async def use_length(db: AsyncSession, spool_id: int, length: float) -> models.S
spool.first_used = datetime.utcnow().replace(microsecond=0)
spool.last_used = datetime.utcnow().replace(microsecond=0)
await db.commit()
await spool_changed(spool, EventType.UPDATED)
# Commit should be the last action, everything after that must never fail
# Otherwise you can end up in a non-atomic thing where the http use request fails
# but the data still has been committed.
await db.commit()
return spool
@@ -449,6 +453,6 @@ async def reset_initial_weight(db: AsyncSession, spool_id: int, weight: float) -
spool.initial_weight = weight
spool.used_weight = 0
await db.commit()
await spool_changed(spool, EventType.UPDATED)
await db.commit()
return spool