-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tweaks to not use celery AsyncResult and EagerResult for task status #89
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('csv_imports', '0002_auto_20161118_1920'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='importtask', | ||
name='task_status', | ||
field=models.CharField(default='PENDING', max_length=32), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,14 @@ def generate_file_path(instance, filename): | |
|
||
|
||
class ImportTask(SmartModel): | ||
PENDING = 'PENDING' | ||
STARTED = 'STARTED' | ||
RUNNING = 'RUNNING' | ||
SUCCESS = 'SUCCESS' | ||
FAILURE = 'FAILURE' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
READY_STATES = [SUCCESS, FAILURE] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just realized you might be using these to keep compatibility with what the Celery states are. If so then that's fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right I got those from Celery even if it is not guaranteed that the will always be in sync. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, fair enough and a good reason. Looks good then, merge at will. |
||
|
||
csv_file = models.FileField(upload_to=generate_file_path, verbose_name="Import file", | ||
help_text="A comma delimited file of records to import") | ||
|
||
|
@@ -32,31 +40,23 @@ class ImportTask(SmartModel): | |
|
||
task_id = models.CharField(null=True, max_length=64) | ||
|
||
task_status = models.CharField(max_length=32, default=PENDING) | ||
|
||
def start(self): | ||
from .tasks import csv_import | ||
self.log("Queued import at %s" % timezone.now()) | ||
self.save(update_fields=['import_log']) | ||
self.task_status = self.STARTED | ||
self.save(update_fields=['import_log', 'task_status']) | ||
result = csv_import.delay(self.pk) | ||
self.task_id = result.task_id | ||
self.save(update_fields=['task_id']) | ||
|
||
def done(self): | ||
if self.task_id: | ||
if getattr(settings, 'CELERY_ALWAYS_EAGER', False): | ||
result = EagerResult(self.task_id, None, 'SUCCESS') | ||
else: | ||
result = AsyncResult(self.task_id) | ||
return result.ready() | ||
return self.task_status in self.READY_STATES | ||
|
||
def status(self): | ||
status = "PENDING" | ||
if self.task_id: | ||
if getattr(settings, 'CELERY_ALWAYS_EAGER', False): | ||
result = EagerResult(self.task_id, None, 'SUCCESS') | ||
else: | ||
result = AsyncResult(self.task_id) | ||
status = result.state | ||
return status | ||
return self.task_status | ||
|
||
def log(self, message): | ||
self.import_log += "%s\n" % message | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can probably just be
status
, no need to repeat task here.