Skip to content

Commit

Permalink
Don't create an separate Celery app instance to manage Celery (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
SonnyBA authored Feb 5, 2025
1 parent 513dc28 commit 74b1252
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 52 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: ["3.8", "3.9", "3.10", "3.11"]
django: ["3.2", "4.1", "4.2"]
exclude:
- python: "3.11"
django: "3.2"
python: ["3.11"]
django: ["4.2"]

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -51,7 +48,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.10"
python-version: "3.11"

- name: Build sdist and wheel
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.11'
- name: Install dependencies
run: pip install tox
- run: tox
Expand Down
32 changes: 12 additions & 20 deletions django_celery_monitor/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""Result Task Admin interface."""
from __future__ import absolute_import, unicode_literals

# from celery.task.control import broadcast, revoke, rate_limit
from celery import Celery, current_app, states
from celery import current_app, states
from celery.utils.text import abbrtask
from django.contrib import admin
from django.contrib.admin import helpers
Expand All @@ -27,8 +26,6 @@
NODE_STATE_COLORS = {'ONLINE': 'green',
'OFFLINE': 'gray'}

app = Celery('django_celery_monitor', broker='redis://localhost:6379/0')

class MonitorList(main_views.ChangeList):
"""A custom changelist to set the page title automatically."""

Expand Down Expand Up @@ -170,22 +167,18 @@ class Media:

@action(_('Revoke selected tasks'))
def revoke_tasks(self, request, queryset):
with current_app.default_connection() as connection:
for state in queryset:
app.control.revoke(state.task_id, connection=connection)
for state in queryset:
current_app.control.revoke(state.task_id)

@action(_('Terminate selected tasks'))
def terminate_tasks(self, request, queryset):
with current_app.default_connection() as connection:
for state in queryset:
app.control.revoke(state.task_id, connection=connection, terminate=True)
for state in queryset:
current_app.control.revoke(state.task_id, terminate=True)

@action(_('Kill selected tasks'))
def kill_tasks(self, request, queryset):
with current_app.default_connection() as connection:
for state in queryset:
app.control.revoke(state.task_id, connection=connection,
terminate=True, signal='KILL')
for state in queryset:
current_app.control.revoke(state.task_id, terminate=True, signal='KILL')

@action(_('Rate limit selected tasks'))
def rate_limit_tasks(self, request, queryset):
Expand All @@ -194,9 +187,8 @@ def rate_limit_tasks(self, request, queryset):
app_label = opts.app_label
if request.POST.get('post'):
rate = request.POST['rate_limit']
with current_app.default_connection() as connection:
for task_name in tasks:
app.control.rate_limit(task_name, rate, connection=connection)
for task_name in tasks:
current_app.control.rate_limit(task_name, rate)
return None

context = {
Expand Down Expand Up @@ -238,16 +230,16 @@ class WorkerMonitor(ModelMonitor):

@action(_('Shutdown selected worker nodes'))
def shutdown_nodes(self, request, queryset):
app.control.broadcast('shutdown', destination=[n.hostname for n in queryset])
current_app.control.broadcast('shutdown', destination=[n.hostname for n in queryset])

@action(_('Enable event mode for selected nodes.'))
def enable_events(self, request, queryset):
app.control.broadcast('enable_events',
current_app.control.broadcast('enable_events',
destination=[n.hostname for n in queryset])

@action(_('Disable event mode for selected nodes.'))
def disable_events(self, request, queryset):
app.control.broadcast('disable_events',
current_app.control.broadcast('disable_events',
destination=[n.hostname for n in queryset])

def get_actions(self, request):
Expand Down
2 changes: 1 addition & 1 deletion requirements/default.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
celery>=5.3.5
django>=3.2.0
django>=4.2.0
16 changes: 1 addition & 15 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,23 @@ def _pyimp():

NAME = 'django_celery_monitor'

E_UNSUPPORTED_PYTHON = '%s 1.0 requires %%s %%s or later!' % (NAME,)

PYIMP = _pyimp()
PY26_OR_LESS = sys.version_info < (2, 7)
PY3 = sys.version_info[0] == 3
PY33_OR_LESS = PY3 and sys.version_info < (3, 4)
PYPY_VERSION = getattr(sys, 'pypy_version_info', None)
PYPY = PYPY_VERSION is not None
PYPY24_ATLEAST = PYPY_VERSION and PYPY_VERSION >= (2, 4)

if PY26_OR_LESS:
raise Exception(E_UNSUPPORTED_PYTHON % (PYIMP, '2.7'))
elif PY33_OR_LESS and not PYPY24_ATLEAST:
raise Exception(E_UNSUPPORTED_PYTHON % (PYIMP, '3.4'))

# -*- Classifiers -*-

classes = """
Development Status :: 5 - Production/Stable
License :: OSI Approved :: BSD License
Programming Language :: Python
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: Implementation :: CPython
Programming Language :: Python :: Implementation :: PyPy
Framework :: Django
Framework :: Django :: 3.2
Framework :: Django :: 4.1
Framework :: Django :: 4.2
Operating System :: OS Independent
Topic :: Communications
Expand Down
10 changes: 1 addition & 9 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
[tox]
envlist =
py{py,36,37,39,310}-django{32, 41, 42}
py311-django{41, 42}
py311-django{42}
isort


[gh-actions]
python =
3.6: py36
3.7: py37
3.8: py38
3.9: py39
3.10: py310
3.11: py311

[testenv]
Expand All @@ -26,8 +20,6 @@ deps=
pytest-runner
celery[pytest]
pytest-celery
django32: Django~=3.2.0
django41: Django~=4.1.0
django42: Django~=4.2.0

commands=
Expand Down

0 comments on commit 74b1252

Please sign in to comment.