Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): adds optional logging to workers #2581

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python/bullmq/types/worker_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ class WorkerOptions(TypedDict, total=False):
"""
Options for connecting to a Redis instance.
"""

enable_logging: bool
"""
Whether to enable basic logging for the worker.

@default False
"""
10 changes: 10 additions & 0 deletions python/bullmq/worker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import Callable
from uuid import uuid4
from bullmq.custom_errors import WaitingChildrenError
Expand All @@ -19,6 +20,9 @@
# Obviously we can still process much faster than 1 job per millisecond but delays and rate limits will never work with more accuracy than 1ms.
minimum_block_timeout = 0.001

logger = logging.getLogger(__name__)


class Worker(EventEmitter):
def __init__(self, name: str, processor: Callable[[Job, str], asyncio.Future], opts: WorkerOptions = {}):
super().__init__()
Expand Down Expand Up @@ -56,6 +60,12 @@ def __init__(self, name: str, processor: Callable[[Job, str], asyncio.Future], o
if opts.get("autorun", True):
asyncio.ensure_future(self.run())

if opts.get("enable_logging", False):
self.on("completed",
lambda job, result: logger.info(f"Worker {self.name}: Finished job {job.id}"))
self.on("failed",
lambda job, err: logger.error(f"Worker {self.name}: Job {job.id} failed: `{repr(err)}`"))

async def run(self):
if self.running:
raise Exception("Worker is already running")
Expand Down