Skip to content

Commit

Permalink
Merge 0b9f7be into 1d1d0a9
Browse files Browse the repository at this point in the history
  • Loading branch information
jv3ga committed May 9, 2024
2 parents 1d1d0a9 + 0b9f7be commit d029a51
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
14 changes: 12 additions & 2 deletions django_q/cluster.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Standard
import signal
import sys
import socket
import uuid
from multiprocessing import Event, Process, Value, current_process
Expand Down Expand Up @@ -43,8 +44,14 @@ def __init__(self, broker: Broker = None):
self.cluster_id = uuid.uuid4()
self.host = socket.gethostname()
self.timeout = None
signal.signal(signal.SIGTERM, self.sig_handler)
signal.signal(signal.SIGINT, self.sig_handler)
if sys.platform == "win32":
import win32api
win32api.SetConsoleCtrlHandler(self.sig_handler_windows, True)
signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGTERM, signal.SIG_IGN)
else:
signal.signal(signal.SIGTERM, self.sig_handler)
signal.signal(signal.SIGINT, self.sig_handler)

def start(self) -> int:
if setproctitle:
Expand Down Expand Up @@ -90,6 +97,9 @@ def sig_handler(self, signum, frame):
)
self.stop()

def sig_handler_windows(self, signum):
self.sig_handler(signum, None)

@property
def stat(self) -> Status:
if self.sentinel:
Expand Down
6 changes: 6 additions & 0 deletions django_q/monitor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import signal
import sys

from multiprocessing.process import current_process
from multiprocessing.queues import Queue

Expand Down Expand Up @@ -32,6 +35,9 @@ def monitor(result_queue: Queue, broker: Broker = None):
:type broker: brokers.Broker
:type result_queue: multiprocessing.Queue
"""
if sys.platform == "win32":
signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGTERM, signal.SIG_IGN)
if not broker:
broker = get_broker()
proc_name = current_process().name
Expand Down
8 changes: 8 additions & 0 deletions django_q/pusher.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import signal
import sys

from multiprocessing import Event
from multiprocessing.process import current_process
from multiprocessing.queues import Queue
Expand Down Expand Up @@ -31,6 +34,11 @@ def pusher(task_queue: Queue, event: Event, broker: Broker = None):
:type task_queue: multiprocessing.Queue
:type event: multiprocessing.Event
"""

if sys.platform == "win32":
signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGTERM, signal.SIG_IGN)

if not broker:
broker = get_broker()
proc_name = current_process().name
Expand Down
8 changes: 8 additions & 0 deletions django_q/worker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import signal
import sys

import pydoc
import traceback
from multiprocessing import Value
Expand Down Expand Up @@ -42,6 +45,11 @@ def worker(
:type result_queue: multiprocessing.Queue
:type timer: multiprocessing.Value
"""

if sys.platform == "win32":
signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGTERM, signal.SIG_IGN)

proc_name = current_process().name
logger.info(
_("%(proc_name)s ready for work at %(id)s")
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ django-q-sentry = {version = ">=0.1", optional = true}
redis = {version = "^4.3.4", optional = true}
setproctitle = {version = "^1.3.2", optional = true}
importlib-metadata = {version = ">=3.6", python = "<3.10"}
pywin32 = { version = ">=306", markers = "python_version >= '3.7' and sys_platform == 'win32'" }


[tool.poetry.dev-dependencies]
Expand Down

0 comments on commit d029a51

Please sign in to comment.