1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Thu Sep 13 11:33:09 2018 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
|
8
|
|
|
Inspired from |
9
|
|
|
|
10
|
|
|
http://docs.celeryproject.org/en/latest/tutorials/task-cookbook.html |
11
|
|
|
|
12
|
|
|
""" |
13
|
|
|
|
14
|
|
|
from celery.utils.log import get_task_logger |
15
|
|
|
|
16
|
|
|
from common.tasks import BaseTask, exclusive_task |
17
|
|
|
from image.celery import app as celery_app |
18
|
|
|
from submissions.tasks import ImportGenericTaskMixin |
19
|
|
|
|
20
|
|
|
from .helpers import cryoweb_import, upload_cryoweb |
21
|
|
|
from .models import truncate_database |
22
|
|
|
|
23
|
|
|
# get a logger for tasks |
24
|
|
|
logger = get_task_logger(__name__) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
# clean cryoweb database after calling decorated function |
28
|
|
|
def clean_cryoweb_database(f): |
29
|
|
|
logger.debug("Decorating %s" % (f)) |
30
|
|
|
|
31
|
|
|
def wrap(*args, **kwargs): |
32
|
|
|
result = f(*args, **kwargs) |
33
|
|
|
|
34
|
|
|
# cleaning up database without knowing if load is successful or not |
35
|
|
|
logger.info("Cleaning up cryoweb database") |
36
|
|
|
truncate_database() |
37
|
|
|
|
38
|
|
|
return result |
39
|
|
|
|
40
|
|
|
wrap.__doc__ = f.__doc__ |
41
|
|
|
wrap.__name__ = f.__name__ |
42
|
|
|
|
43
|
|
|
# return decorated function |
44
|
|
|
return wrap |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
class ImportCryowebTask(ImportGenericTaskMixin, BaseTask): |
48
|
|
|
""" |
49
|
|
|
An exclusive task wich upload a *data-only* cryoweb dump in cryoweb |
50
|
|
|
database and then fill up :ref:`UID <The Unified Internal Database>` |
51
|
|
|
tables. After data import (wich could be successful or not) cryoweb |
52
|
|
|
helper database is cleanded and restored in the original status:: |
53
|
|
|
|
54
|
|
|
from cryoweb.tasks import ImportCryowebTask |
55
|
|
|
|
56
|
|
|
# call task asynchronously |
57
|
|
|
task = ImportCryowebTask() |
58
|
|
|
res = task.delay(submission_id) |
59
|
|
|
|
60
|
|
|
Args: |
61
|
|
|
submission_id (int): the submission primary key |
62
|
|
|
|
63
|
|
|
Returns: |
64
|
|
|
str: a message string (ex. success) |
65
|
|
|
""" |
66
|
|
|
|
67
|
|
|
name = "Import Cryoweb" |
68
|
|
|
description = """Import Cryoweb data from Cryoweb dump""" |
69
|
|
|
action = "cryoweb import" |
70
|
|
|
|
71
|
|
|
# decorate function in order to cleanup cryoweb database after data import |
72
|
|
|
@exclusive_task( |
73
|
|
|
task_name="Import Cryoweb", |
74
|
|
|
lock_id="ImportFromCryoWeb", |
75
|
|
|
blocking=True) |
76
|
|
|
def run(self, submission_id): |
77
|
|
|
return super().run(submission_id) |
78
|
|
|
|
79
|
|
|
@clean_cryoweb_database |
80
|
|
|
def import_data_from_file(self, submission_obj): |
81
|
|
|
"""Call the custom import method""" |
82
|
|
|
|
83
|
|
|
# upload data into cryoweb database |
84
|
|
|
status = upload_cryoweb(submission_obj.id) |
85
|
|
|
|
86
|
|
|
# if something went wrong, uploaded_cryoweb has token the exception |
87
|
|
|
# ad update submission.message field |
88
|
|
|
if status is False: |
89
|
|
|
return status |
90
|
|
|
|
91
|
|
|
# load cryoweb data into UID |
92
|
|
|
# check status |
93
|
|
|
status = cryoweb_import(submission_obj) |
94
|
|
|
|
95
|
|
|
return status |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
# register explicitly tasks |
99
|
|
|
# https://github.com/celery/celery/issues/3744#issuecomment-271366923 |
100
|
|
|
celery_app.tasks.register(ImportCryowebTask) |
101
|
|
|
|