Completed
Pull Request — master (#51)
by Paolo
06:48
created

zooma.tasks.AnnotateAll.run()   A

Complexity

Conditions 1

Size

Total Lines 31
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 31
rs 9.85
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 ExclusiveTask, NotifyAdminTaskMixin
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_uberon,
20
    annotate_dictdevelstage, annotate_dictphysiostage)
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, ExclusiveTask):
47
    name = "Annotate Countries"
48
    description = """Annotate countries with ontologies using Zooma tools"""
49
    model = DictCountry
50
    annotate_func = staticmethod(annotate_country)
51
    lock_id = "AnnotateCountries"
52
53
54
class AnnotateBreeds(AnnotateTaskMixin, ExclusiveTask):
55
    name = "Annotate Breeds"
56
    description = """Annotate breeds with ontologies using Zooma tools"""
57
    model = DictBreed
58
    annotate_func = staticmethod(annotate_breed)
59
    lock_id = "AnnotateBreeds"
60
61
62
class AnnotateSpecies(AnnotateTaskMixin, ExclusiveTask):
63
    name = "Annotate Species"
64
    description = """Annotate species with ontologies using Zooma tools"""
65
    model = DictSpecie
66
    annotate_func = staticmethod(annotate_specie)
67
    lock_id = "AnnotateSpecies"
68
69
70
class AnnotateUberon(AnnotateTaskMixin, ExclusiveTask):
71
    name = "Annotate Uberon"
72
    description = "Annotate organism parts with ontologies using Zooma tools"
73
    model = DictUberon
74
    annotate_func = staticmethod(annotate_uberon)
75
    lock_id = "AnnotateUberon"
76
77
78
class AnnotateDictDevelStage(AnnotateTaskMixin, ExclusiveTask):
79
    name = "Annotate DictDevelStage"
80
    description = (
81
        "Annotate developmental stages with ontologies using Zooma tools")
82
    model = DictDevelStage
83
    annotate_func = staticmethod(annotate_dictdevelstage)
84
    lock_id = "AnnotateDictDevelStage"
85
86
87
class AnnotateDictPhysioStage(AnnotateTaskMixin, ExclusiveTask):
88
    name = "Annotate DictPhysioStage"
89
    description = (
90
        "Annotate physiological stages with ontologies using Zooma tools")
91
    model = DictPhysioStage
92
    annotate_func = staticmethod(annotate_dictphysiostage)
93
    lock_id = "AnnotateDictPhysioStage"
94
95
96
class AnnotateAll(ExclusiveTask):
97
    name = "Annotate All"
98
    description = """Annotate all dict tables using Zooma"""
99
    lock_id = "AnnotateAll"
100
101
    def run(self):
102
        """
103
        This function is called when delay is called. It will acquire a lock
104
        in redis, so those tasks are mutually exclusive
105
106
        Returns:
107
            str: success if everything is ok. Different messages if task is
108
            already running or exception is caught"""
109
110
        # debugging instance
111
        self.debug_task()
112
113
        tasks = [
114
            AnnotateCountries(), AnnotateBreeds(), AnnotateSpecies(),
115
            AnnotateUberon(), AnnotateDictDevelStage(),
116
            AnnotateDictPhysioStage()
117
        ]
118
119
        # instantiate the group
120
        annotate_task = group([task.s() for task in tasks])
121
122
        logger.debug("Starting task %s" % (annotate_task))
123
124
        # start the group task - shortcut for apply_asyinc
125
        result = annotate_task.delay()
126
127
        logger.debug(result)
128
129
        # forget about called tasks and exit
130
131
        return "success"
132
133
134
# --- task registering
135
136
137
# register explicitly tasks
138
# https://github.com/celery/celery/issues/3744#issuecomment-271366923
139
celery_app.tasks.register(AnnotateCountries)
140
celery_app.tasks.register(AnnotateBreeds)
141
celery_app.tasks.register(AnnotateSpecies)
142
celery_app.tasks.register(AnnotateUberon)
143
celery_app.tasks.register(AnnotateDictDevelStage)
144
celery_app.tasks.register(AnnotateDictPhysioStage)
145
celery_app.tasks.register(AnnotateAll)
146