-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import sys | ||
import datetime | ||
import multiprocessing | ||
import sys | ||
import time | ||
from unittest import skipIf, mock | ||
from unittest.mock import patch, PropertyMock, MagicMock | ||
|
@@ -37,6 +38,8 @@ | |
from django_rq.utils import get_jobs, get_statistics, get_scheduler_pid | ||
from django_rq.workers import get_worker, get_worker_class | ||
|
||
from .utils import query_queue | ||
|
||
try: | ||
from rq_scheduler import Scheduler | ||
from ..queues import get_scheduler | ||
|
@@ -303,6 +306,18 @@ def test_pass_queue_via_commandline_args(self): | |
self.assertTrue(job['job'].is_finished) | ||
self.assertIn(job['job'].id, job['finished_job_registry'].get_job_ids()) | ||
|
||
def test_rqworker_pool_process_start_method(self) -> None: | ||
for start_method in ['spawn', 'fork']: | ||
with mock.patch.object(multiprocessing, "get_start_method", return_value=start_method): | ||
queue_name = 'django_rq_test' | ||
queue = get_queue(queue_name) | ||
job = queue.enqueue(query_queue) | ||
finished_job_registry = FinishedJobRegistry(queue.name, queue.connection) | ||
call_command('rqworker-pool', queue_name, burst=True) | ||
|
||
self.assertTrue(job.is_finished) | ||
self.assertIn(job.id, finished_job_registry.get_job_ids()) | ||
|
||
def test_configure_sentry(self): | ||
rqworker.configure_sentry('https://[email protected]/1') | ||
self.mock_sdk.init.assert_called_once_with( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import django | ||
from multiprocessing import Process, get_start_method | ||
from typing import Any | ||
|
||
from rq.worker_pool import WorkerPool, run_worker | ||
|
||
|
||
class DjangoWorkerPool(WorkerPool): | ||
def get_worker_process( | ||
self, | ||
name: str, | ||
burst: bool, | ||
_sleep: float = 0, | ||
logging_level: str = "INFO", | ||
) -> Process: | ||
"""Returns the worker process""" | ||
return Process( | ||
target=run_django_worker, | ||
args=(name, self._queue_names, self._connection_class, self._pool_class, self._pool_kwargs), | ||
kwargs={ | ||
'_sleep': _sleep, | ||
'burst': burst, | ||
'logging_level': logging_level, | ||
'worker_class': self.worker_class, | ||
'job_class': self.job_class, | ||
'serializer': self.serializer, | ||
}, | ||
name=f'Worker {name} (WorkerPool {self.name})', | ||
) | ||
|
||
|
||
def run_django_worker(*args: Any, **kwargs: Any) -> None: | ||
# multiprocessing library default process start method may be | ||
# `spawn` or `fork` depending on the host OS | ||
if get_start_method() == 'spawn': | ||
django.setup() | ||
|
||
run_worker(*args, **kwargs) |