Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Commit

Permalink
Ensure recursive monitor tasks properly log to result field of celery…
Browse files Browse the repository at this point in the history
… task result table
  • Loading branch information
aaronstephenson committed Oct 24, 2019
1 parent fd2e3ca commit b43b12d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ To use Celery in development, run `celery -A lide worker -l info` (note that thi

In a production environment (or really, any non-development environment) this Django project should be run through a dedicated web server, likely using the Web Server Gateway Interface [(WSGI)](https://wsgi.readthedocs.io/en/latest/). This repository includes sample configuration files (*.conf in the root folder) for running this project in [Apache HTTP Server](https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/).

Additionally, Celery must be set up as a service or daemon for Django to use it. On Linux (note that Celery is no longer supported on Windows) follow the instructions (here)[https://docs.celeryproject.org/en/latest/userguide/daemonizing.html#daemonizing]. For convenience, the necessary documents are in this repository:
* `init.d_celeryd` (sourced from the [official Celery repo](https://github.com/celery/celery/blob/master/extra/generic-init.d/celeryd), note that this file should be saved on the server as `/etc/init.d/celeryd`, and its file permissions should be set to 755 (which can be done with the command `sudo chmod 755 /etc/init.d/celeryd`))
Additionally, Celery must be set up as a service or daemon for Django to use it. On Linux (note that Celery is no longer supported on Windows) follow the instructions [here](https://docs.celeryproject.org/en/latest/userguide/daemonizing.html#daemonizing) (also read the docs about how Celery and Django connect [here](https://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#django-first-steps)). For convenience, the necessary documents are in this repository:
* `default_celeryd` (sourced from the [official Celery documentation](https://docs.celeryproject.org/en/latest/userguide/daemonizing.html#example-configuration), note that this file should be saved on the server as `/etc/default/celeryd`))

* `init.d_celeryd` (sourced from the [official Celery repo](https://github.com/celery/celery/blob/master/extra/generic-init.d/celeryd), note that this file should be saved on the server as `/etc/init.d/celeryd`, and its file permissions should be set to 755 (which can be done with the command `sudo chmod 755 /etc/init.d/celeryd`); also register the script to run on boot with the command `sudo update-rc.d lide defaults`)

## Authors

Expand Down
2 changes: 1 addition & 1 deletion code.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "lide",
"organization": "U.S. Geological Survey",
"description": "Web services for LILI (LIDE (Laboratory for Infectious Disease and the Environment) Information Management System)",
"version": "v0.121.2",
"version": "v0.121.3",
"status": "Release Candidate",

"permissions": {
Expand Down
10 changes: 8 additions & 2 deletions lideservices/tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from time import sleep
from datetime import datetime
from collections import Counter, OrderedDict
from django.db.models import Q, Case, When, Value, Count, Sum, Min, Max, Avg, FloatField, CharField
from django.db.models.functions import Cast
Expand All @@ -26,8 +27,8 @@ def monitor_task(task_id, datetimestart_str, report_file_id):
sleep(settings.TASK_SLEEP)
task = AsyncResult(task_id)
print('monitoring task ID ' + str(task) + ' for report ID ' + str(report_file_id))
datetimestart = datetime.strptime(datetimestart_str, '%Y-%m-%d_%H:%M:%S')
if task.status in ['PENDING', 'RECEIVED', 'STARTED']:
datetimestart = datetime.strptime(datetimestart_str, '%Y-%m-%d_%H:%M:%S')
datetimediff = datetime.now() - datetimestart
message = 'task ID ' + str(task) + ' for report ID ' + str(report_file_id) + ' has state ' + str(task.status)
message += ' at ' + str(datetimediff.total_seconds()) + ' seconds after it started'
Expand All @@ -41,7 +42,12 @@ def monitor_task(task_id, datetimestart_str, report_file_id):
report_file.save()
return message
else:
monitor_task(task, datetimestart, report_file_id)
message = 'task ID ' + str(task) + ' for report ID ' + str(report_file_id) + ' is still running'
print(message)
try:
return message
finally:
monitor_task(task.id, datetimestart_str, report_file_id)
else:
message = 'monitoring task ID ' + str(task) + ' for report ID ' + str(report_file_id)
message += ' completed with status ' + str(task.status)
Expand Down

0 comments on commit b43b12d

Please sign in to comment.