Use correct JSON encoder for storing results.

parent 924544e8
import json
class ResultEncoder(json.JSONEncoder):
"""JSON encoder that serializes results from celery tasks."""
def default(self, value):
if isinstance(value, Exception):
error = {
'error': value.__class__.__name__,
'message': str(value)
}
if value.__cause__:
error['cause'] = self.default(value.__cause__)
return error
......@@ -10,25 +10,12 @@ from celery.result import ResultSet
from django.conf import settings
from django.core.management.base import BaseCommand
from failmap_admin.app.common import ResultEncoder
from failmap_admin.celery import app
log = logging.getLogger(__name__)
class ResultEncoder(json.JSONEncoder):
"""JSON encoder that serializes results from celery tasks."""
def default(self, value):
if isinstance(value, Exception):
error = {
'error': value.__class__.__name__,
'message': str(value)
}
if value.__cause__:
error['cause'] = self.default(value.__cause__)
return error
class TaskCommand(BaseCommand):
"""A command that performs it's intended behaviour through a Celery task.
......
......@@ -6,6 +6,7 @@ import datetime
from django.db import models
from jsonfield import JSONField
from ..app.common import ResultEncoder
from ..celery import app
......@@ -16,7 +17,7 @@ class Job(models.Model):
task = models.TextField(help_text="celery task signature in string form")
result_id = models.CharField(unique=True, max_length=255, help_text="celery asyncresult ID for tracing task")
status = models.CharField(max_length=255, help_text="status of the job")
result = JSONField(help_text="output of the task as JSON")
result = JSONField(help_text="output of the task as JSON", encoder_class=ResultEncoder)
created_on = models.DateTimeField(auto_now_add=True, blank=True, null=True, help_text="when task was created")
finished_on = models.DateTimeField(blank=True, null=True, help_text="when task ended")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment