-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5517 from archesproject/5474_celery
Implements celery in Arches
- Loading branch information
Showing
13 changed files
with
124 additions
and
329 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
from __future__ import absolute_import | ||
from arches.setup import get_version | ||
|
||
VERSION = (4, 4, 1, 'final', 0) | ||
try: | ||
from .celery import app as celery_app | ||
except ModuleNotFoundError as e: | ||
print(e) | ||
|
||
VERSION = (5, 0, 0, 'final', 0) | ||
|
||
__version__ = get_version(VERSION) | ||
|
||
# This will make sure the app is always imported when | ||
# Django starts so that shared_task will use this app. | ||
__all__ = ('celery_app',) |
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,9 @@ | ||
# Create your tasks here | ||
from __future__ import absolute_import, unicode_literals | ||
from celery import shared_task | ||
from django.core import management | ||
|
||
@shared_task | ||
def sync(surveyid, userid): | ||
management.call_command('mobile', operation='sync_survey', id=surveyid, user=userid) | ||
return 'sync complete' |
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,24 @@ | ||
import logging | ||
from kombu import Connection | ||
from django.utils.translation import ugettext as _ | ||
from arches.app.models.system_settings import settings | ||
from arches.celery import app | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def check_if_celery_available(): | ||
try: | ||
conn = Connection(settings.CELERY_BROKER_URL) | ||
conn.ensure_connection(max_retries=2) | ||
except Exception as e: | ||
logger.warning(_("Unable to connect to a celery broker")) | ||
return False | ||
inspect = app.control.inspect() | ||
result = inspect.ping() | ||
if result is None: | ||
logger.warning(_("A celery broker is running, but a celery worker is not available")) | ||
result = False # ping returns True or None, assigning False here so we return only a boolean value | ||
else: | ||
result = True | ||
return result |
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,22 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
import os | ||
import celery | ||
from celery import Celery | ||
|
||
# set the default Django settings module for the 'celery' program. | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') | ||
|
||
app = Celery('arches') | ||
|
||
# Using a string here means the worker doesn't have to serialize | ||
# the configuration object to child processes. | ||
# - namespace='CELERY' means all celery-related configuration keys | ||
# should have a `CELERY_` prefix. | ||
app.config_from_object('django.conf:settings', namespace='CELERY') | ||
|
||
# Load task modules from all registered Django app configs. | ||
app.autodiscover_tasks() | ||
|
||
@app.task(bind=True) | ||
def debug_task(self): | ||
print('Request: {0!r}'.format(self.request)) |
21 changes: 21 additions & 0 deletions
21
arches/install/arches-templates/project_name/celery.py-tpl
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,21 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
import os | ||
from celery import Celery | ||
|
||
# set the default Django settings module for the 'celery' program. | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings') | ||
|
||
app = Celery('{{ project_name }}') | ||
|
||
# Using a string here means the worker doesn't have to serialize | ||
# the configuration object to child processes. | ||
# - namespace='CELERY' means all celery-related configuration keys | ||
# should have a `CELERY_` prefix. | ||
app.config_from_object('django.conf:settings', namespace='CELERY') | ||
|
||
# Load task modules from all registered Django app configs. | ||
app.autodiscover_tasks() | ||
|
||
@app.task(bind=True) | ||
def debug_task(self): | ||
print('Request: {0!r}'.format(self.request)) |
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
Oops, something went wrong.