refactor(queue): use utf-8, drop unused imports, atomic fail()
This commit is contained in:
parent
fbed5829db
commit
b87462fb4c
|
|
@ -1,7 +1,6 @@
|
||||||
import json
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
class QueueItem(BaseModel):
|
class QueueItem(BaseModel):
|
||||||
|
|
@ -34,7 +33,7 @@ class Queue:
|
||||||
def enqueue(self, item: QueueItem) -> None:
|
def enqueue(self, item: QueueItem) -> None:
|
||||||
target = self._path(self.pending, item.update_id)
|
target = self._path(self.pending, item.update_id)
|
||||||
tmp = target.with_suffix(".tmp")
|
tmp = target.with_suffix(".tmp")
|
||||||
tmp.write_text(item.model_dump_json())
|
tmp.write_text(item.model_dump_json(), encoding="utf-8")
|
||||||
tmp.replace(target)
|
tmp.replace(target)
|
||||||
|
|
||||||
def claim_next(self) -> Optional[QueueItem]:
|
def claim_next(self) -> Optional[QueueItem]:
|
||||||
|
|
@ -44,7 +43,7 @@ class Queue:
|
||||||
src.rename(dst)
|
src.rename(dst)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
continue
|
continue
|
||||||
return QueueItem.model_validate_json(dst.read_text())
|
return QueueItem.model_validate_json(dst.read_text(encoding="utf-8"))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def complete(self, item: QueueItem) -> None:
|
def complete(self, item: QueueItem) -> None:
|
||||||
|
|
@ -59,8 +58,11 @@ class Queue:
|
||||||
dst = self._path(self.failed, item.update_id)
|
dst = self._path(self.failed, item.update_id)
|
||||||
else:
|
else:
|
||||||
dst = self._path(self.pending, item.update_id)
|
dst = self._path(self.pending, item.update_id)
|
||||||
tmp = src.with_suffix(".tmp")
|
# Write tmp inside destination dir, then atomically replace into dst.
|
||||||
tmp.write_text(item.model_dump_json())
|
# If we crash before unlinking src, dst is already in place; the
|
||||||
|
# working/ entry becomes an orphan but no item is ever missing.
|
||||||
|
tmp = dst.with_suffix(".tmp")
|
||||||
|
tmp.write_text(item.model_dump_json(), encoding="utf-8")
|
||||||
tmp.replace(dst)
|
tmp.replace(dst)
|
||||||
if src.exists():
|
if src.exists():
|
||||||
src.unlink()
|
src.unlink()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue