Completed
Push — master ( 2c16e2...28a4a1 )
by Paolo
17s queued 14s
created

zooma.tasks.AnnotateAll.run()   A

Complexity

Conditions 1

Size

Total Lines 32
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 32
rs 9.8
c 0
b 0
f 0
cc 1
nop 1
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Thu Oct 25 11:27:52 2018
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
from celery import group
10
from celery.utils.log import get_task_logger
11
12
from common.tasks import BaseTask, NotifyAdminTaskMixin, exclusive_task
13
from image.celery import app as celery_app
14
from image_app.models import (
15
    DictCountry, DictBreed, DictSpecie, DictUberon, DictDevelStage,
16
    DictPhysioStage)
17
18
from .helpers import (
19
    annotate_country, annotate_breed, annotate_specie, annotate_organismpart,
20
    annotate_develstage, annotate_physiostage)
21
22
# Get an instance of a logger
23
logger = get_task_logger(__name__)
24
25
26
class AnnotateTaskMixin(NotifyAdminTaskMixin):
27
    name = None
28
    descripttion = None
29
    model = None
30
    annotate_func = None
31
32
    def run(self):
33
        """This function is called when delay is called"""
34
35
        logger.debug("Starting %s" % self.name.lower())
36
37
        # get all countries without a term
38
        for term in self.model.objects.filter(term__isnull=True):
39
            self.annotate_func(term)
40
41
        logger.debug("%s completed" % self.name.lower())
42
43
        return "success"
44
45
46
class AnnotateCountries(AnnotateTaskMixin, BaseTask):
47
    name = "Annotate Countries"
48
    description = """Annotate countries with ontologies using Zooma tools"""
49
    model = DictCountry
50
    annotate_func = staticmethod(annotate_country)
51
52
    @exclusive_task(
53
            task_name="Annotate Countries", lock_id="AnnotateCountries")
54
    def run(self):
55
        return super().run()
56
57
58
class AnnotateBreeds(AnnotateTaskMixin, BaseTask):
59
    name = "Annotate Breeds"
60
    description = """Annotate breeds with ontologies using Zooma tools"""
61
    model = DictBreed
62
    annotate_func = staticmethod(annotate_breed)
63
64
    @exclusive_task(task_name="Annotate Breeds", lock_id="AnnotateBreeds")
65
    def run(self):
66
        return super().run()
67
68
69
class AnnotateSpecies(AnnotateTaskMixin, BaseTask):
70
    name = "Annotate Species"
71
    description = """Annotate species with ontologies using Zooma tools"""
72
    model = DictSpecie
73
    annotate_func = staticmethod(annotate_specie)
74
75
    @exclusive_task(task_name="Annotate Species", lock_id="AnnotateSpecies")
76
    def run(self):
77
        return super().run()
78
79
80
class AnnotateOrganismPart(AnnotateTaskMixin, BaseTask):
81
    name = "Annotate OrganismPart"
82
    description = "Annotate organism parts with ontologies using Zooma tools"
83
    model = DictUberon
84
    annotate_func = staticmethod(annotate_organismpart)
85
    lock_id = "AnnotateOrganismPart"
86
87
    @exclusive_task(task_name="Annotate Uberon", lock_id="AnnotateUberon")
88
    def run(self):
89
        return super().run()
90
91
92
class AnnotateDevelStage(AnnotateTaskMixin, BaseTask):
93
    name = "Annotate DevelStage"
94
    description = (
95
        "Annotate developmental stages with ontologies using Zooma tools")
96
    model = DictDevelStage
97
    annotate_func = staticmethod(annotate_develstage)
98
99
    @exclusive_task(
100
            task_name="Annotate DevelStage",
101
            lock_id="AnnotateDevelStage")
102
    def run(self):
103
        return super().run()
104
105
106
class AnnotatePhysioStage(AnnotateTaskMixin, BaseTask):
107
    name = "Annotate PhysioStage"
108
    description = (
109
        "Annotate physiological stages with ontologies using Zooma tools")
110
    model = DictPhysioStage
111
    annotate_func = staticmethod(annotate_physiostage)
112
113
    @exclusive_task(
114
            task_name="Annotate PhysioStage",
115
            lock_id="AnnotatePhysioStage")
116
    def run(self):
117
        return super().run()
118
119
120
class AnnotateAll(BaseTask):
121
    name = "Annotate All"
122
    description = """Annotate all dict tables using Zooma"""
123
124
    @exclusive_task(task_name="Annotate All", lock_id="AnnotateAll")
125
    def run(self):
126
        """
127
        This function is called when delay is called. It will acquire a lock
128
        in redis, so those tasks are mutually exclusive
129
130
        Returns:
131
            str: success if everything is ok. Different messages if task is
132
            already running or exception is caught"""
133
134
        # debugging instance
135
        self.debug_task()
136
137
        tasks = [
138
            AnnotateCountries(), AnnotateBreeds(), AnnotateSpecies(),
139
            AnnotateOrganismPart(), AnnotateDevelStage(),
140
            AnnotatePhysioStage()
141
        ]
142
143
        # instantiate the group
144
        annotate_task = group([task.s() for task in tasks])
145
146
        logger.debug("Starting task %s" % (annotate_task))
147
148
        # start the group task - shortcut for apply_asyinc
149
        result = annotate_task.delay()
150
151
        logger.debug(result)
152
153
        # forget about called tasks and exit
154
155
        return "success"
156
157
158
# --- task registering
159
160
161
# register explicitly tasks
162
# https://github.com/celery/celery/issues/3744#issuecomment-271366923
163
celery_app.tasks.register(AnnotateCountries)
164
celery_app.tasks.register(AnnotateBreeds)
165
celery_app.tasks.register(AnnotateSpecies)
166
celery_app.tasks.register(AnnotateOrganismPart)
167
celery_app.tasks.register(AnnotateDevelStage)
168
celery_app.tasks.register(AnnotatePhysioStage)
169
celery_app.tasks.register(AnnotateAll)
170