from __future__ import annotations
import time
from rustqueue import RustQueueClient, RustQueueError
def main() -> None:
client = RustQueueClient()
health = client.health()
print(f"Server is {health['status']} (v{health['version']}, uptime {health['uptime_seconds']}s)")
queue = "demo"
job_id = client.push(
queue,
"send-email",
{"to": "user@example.com", "subject": "Hello from RustQueue!"},
max_attempts=3,
timeout_ms=30_000,
)
print(f"Pushed job {job_id} to queue '{queue}'")
jobs = client.pull(queue)
if not jobs:
print("No jobs available (unexpected!)")
return
job = jobs[0]
print(f"Pulled job {job['id']} (name={job['name']})")
for pct in (25, 50, 75, 100):
time.sleep(0.1)
client.progress(job["id"], pct, message=f"Step {pct}%")
print(f" progress: {pct}%")
client.ack(job["id"], result={"delivered": True})
print(f"Acked job {job['id']}")
completed = client.get_job(job_id)
if completed:
print(f"Job state: {completed.get('state')}")
fail_id = client.push(queue, "might-fail", {"attempt": 1}, max_attempts=2)
pulled = client.pull(queue)
if pulled:
result = client.fail(pulled[0]["id"], "simulated error")
print(f"Failed job {pulled[0]['id']}: retry={result['retry']}")
stats = client.get_queue_stats(queue)
print(f"Queue '{queue}' stats: {stats}")
ids = client.push_batch(queue, [
{"name": "batch-job", "data": {"index": 0}},
{"name": "batch-job", "data": {"index": 1}},
{"name": "batch-job", "data": {"index": 2}},
])
print(f"Batch pushed {len(ids)} jobs: {ids}")
print("\nDone!")
if __name__ == "__main__":
try:
main()
except RustQueueError as exc:
print(f"RustQueue error: {exc}")
except ConnectionError as exc:
print(f"Could not connect to RustQueue server: {exc}")