32 lines
749 B
Python
32 lines
749 B
Python
"""Main job queue implementation."""
|
|
import asyncio
|
|
import time
|
|
from dataclasses import dataclass
|
|
from typing import Optional
|
|
|
|
@dataclass
|
|
class Job:
|
|
"""A job to be executed."""
|
|
id: str
|
|
handler: str
|
|
payload: dict
|
|
created_at: float
|
|
|
|
class Queue:
|
|
"""Manages pending jobs."""
|
|
def __init__(self):
|
|
self.jobs = []
|
|
self._lock = asyncio.Lock()
|
|
|
|
async def enqueue(self, job: Job) -> None:
|
|
"""Add a job to the queue."""
|
|
async with self._lock:
|
|
self.jobs.append(job)
|
|
|
|
async def dequeue(self) -> Optional[Job]:
|
|
"""Get the next job from the queue."""
|
|
async with self._lock:
|
|
if self.jobs:
|
|
return self.jobs.pop(0)
|
|
return None
|