Skip to content

Commit 0d54354

Browse files
committed
ref(transport): Add abstract base class for worker implementation
Add an abstract bass class for implementation of the background worker. This was done to provide a shared interface for the current implementation of a threaded worker in the sync context as well as the upcoming async task-based worker implementation. GH-4578
1 parent 3607d44 commit 0d54354

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

sentry_sdk/worker.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import annotations
2+
from abc import ABC, abstractmethod
23
import os
34
import threading
45

@@ -16,7 +17,48 @@
1617
_TERMINATOR = object()
1718

1819

19-
class BackgroundWorker:
20+
class Worker(ABC):
21+
"""
22+
Base class for all workers.
23+
24+
A worker is used to process events in the background and send them to Sentry.
25+
"""
26+
27+
@property
28+
@abstractmethod
29+
def is_alive(self) -> bool:
30+
pass
31+
32+
@abstractmethod
33+
def kill(self) -> None:
34+
pass
35+
36+
@abstractmethod
37+
def flush(
38+
self, timeout: float, callback: Optional[Callable[[int, float], None]] = None
39+
) -> None:
40+
"""
41+
Flush the worker.
42+
43+
This method blocks until the worker has flushed all events or the specified timeout is reached.
44+
"""
45+
pass
46+
47+
@abstractmethod
48+
def full(self) -> bool:
49+
pass
50+
51+
@abstractmethod
52+
def submit(self, callback: Callable[[], None]) -> bool:
53+
"""
54+
Schedule a callback to be executed by the worker.
55+
56+
Returns True if the callback was scheduled, False if the queue is full.
57+
"""
58+
pass
59+
60+
61+
class BackgroundWorker(Worker):
2062
def __init__(self, queue_size: int = DEFAULT_QUEUE_SIZE) -> None:
2163
self._queue: Queue = Queue(queue_size)
2264
self._lock = threading.Lock()

0 commit comments

Comments
 (0)