1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Wed Feb 27 16:38:37 2019 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from celery.utils.log import get_task_logger |
10
|
|
|
|
11
|
|
|
from common.constants import ERROR |
12
|
|
|
from common.tasks import BatchUpdateMixin |
13
|
|
|
from image.celery import app as celery_app, MyTask |
14
|
|
|
from image_app.models import Submission |
15
|
|
|
from submissions.helpers import send_message |
16
|
|
|
|
17
|
|
|
# Get an instance of a logger |
18
|
|
|
logger = get_task_logger(__name__) |
19
|
|
|
|
20
|
|
|
|
21
|
|
View Code Duplication |
class BatchUpdateAnimals(MyTask, BatchUpdateMixin): |
|
|
|
|
22
|
|
|
name = "Batch update animals" |
23
|
|
|
description = """Batch update of field in animals""" |
24
|
|
|
|
25
|
|
|
# Ovverride default on failure method |
26
|
|
|
# This is not a failed validation for a wrong value, this is an |
27
|
|
|
# error in task that mean an error in coding |
28
|
|
|
def on_failure(self, exc, task_id, args, kwargs, einfo): |
29
|
|
|
logger.error('{0!r} failed: {1!r}'.format(task_id, exc)) |
30
|
|
|
|
31
|
|
|
# get submission object |
32
|
|
|
submission_obj = Submission.objects.get(pk=args[0]) |
33
|
|
|
|
34
|
|
|
# mark submission with ERROR |
35
|
|
|
submission_obj.status = ERROR |
36
|
|
|
submission_obj.message = ("Error in batch update for animals: %s" |
37
|
|
|
% (str(exc))) |
38
|
|
|
submission_obj.save() |
39
|
|
|
|
40
|
|
|
send_message(submission_obj) |
41
|
|
|
|
42
|
|
|
# send a mail to the user with the stacktrace (einfo) |
43
|
|
|
submission_obj.owner.email_user( |
44
|
|
|
"Error in batch update for animals: %s" % (args[0]), |
45
|
|
|
("Something goes wrong in batch update for animals. Please report " |
46
|
|
|
"this to InjectTool team\n\n %s" % str(einfo)), |
47
|
|
|
) |
48
|
|
|
|
49
|
|
|
# TODO: submit mail to admin |
50
|
|
|
|
51
|
|
|
def run(self, submission_id, animal_ids, attribute, item_type='animal'): |
52
|
|
|
"""Function for batch update attribute in animals |
53
|
|
|
Args: |
54
|
|
|
submission_id (int): id of submission |
55
|
|
|
animal_ids (dict): dict with id and values to update |
56
|
|
|
attribute (str): attribute to update |
57
|
|
|
""" |
58
|
|
|
|
59
|
|
|
logger.info("Start batch update for animals") |
60
|
|
|
super(BatchUpdateAnimals, self).batch_update(submission_id, animal_ids, |
61
|
|
|
attribute, item_type) |
62
|
|
|
return 'success' |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
# register explicitly tasks |
66
|
|
|
# https://github.com/celery/celery/issues/3744#issuecomment-271366923 |
67
|
|
|
celery_app.tasks.register(BatchUpdateAnimals) |
68
|
|
|
|