Blame view

src/analyzer/tasks.py 2.62 KB
5b5566ab   haribo   added celery
1
from __future__ import absolute_import
ddf59dd4   haribo   Remaniement :
2
from common.models import *
c53a13e0   Jeremy   Updating a lot of...
3
4
5
from utils.Logger import setupLogger
from django.conf import settings
import subprocess
77816f10   haribo   Workflow implemen...
6
7
import os

c53a13e0   Jeremy   Updating a lot of...
8
9
10
11
12
'''
    Gets the folder path and the plan id
    it makes an analysis of the calibrated images (in folder "calibrated")
'''

5b5566ab   haribo   added celery
13

5e45ba9f   Etienne Pallier   Bye Bye Celery (f...
14
15
#class Analysis(Task):
class Analysis():
c53a13e0   Jeremy   Updating a lot of...
16
17
18
19
20
21
    logger = setupLogger("analysis", "analysis")

    def log(self, message: str) -> int:
        if settings.DEBUG:
            self.logger.info(message)
        return 0
5b5566ab   haribo   added celery
22

c53a13e0   Jeremy   Updating a lot of...
23
24
25
    def logDB(self, message: str) -> int:
        Log.objects.create(agent='Analyzer', message=message)
        return 0
aed99094   haribo   Debug routine MGR...
26

c53a13e0   Jeremy   Updating a lot of...
27
28
29
    def execProcess(self, command: str) -> int:
        self.process = subprocess.Popen(command, shell=True)
        return 0
aed99094   haribo   Debug routine MGR...
30

c53a13e0   Jeremy   Updating a lot of...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    def start(self, plan_id: int, folder: str) -> int:
        self.plan_id = plan_id
        self.folder = folder
        self.path_dir_file = os.path.dirname(os.path.realpath(__file__))
        try:
            self.plan = Plan.objects.get(id=self.plan_id)
        except:
            self.log("analysis not found")
            self.logDB(message="Plan with id %d not found"%(self.plan_id))
            return 1
        self.dir_name = self.folder + os.sep + self.plan.name + "_" + str(self.plan_id) + os.sep + "analysis"
        self.plan_folder = self.plan.name + "_" + str(self.plan_id)
        if self.createDirectory():
            self.log("analysis err dir")
            self.logDB(message="Could not create the directory for analysis")
            return 1
        self.log("analysis start")
        return self.logDB("Starting analysis for plan " + self.plan.name)
0cf09544   haribo   Date: 05/07/2016
49

c53a13e0   Jeremy   Updating a lot of...
50
51
52
53
54
55
56
    def createDirectory(self):
        try:
            if not os.path.isdir(self.dir_name):
                os.makedirs(self.dir_name)
        except:
            return 1
        return 0
0cf09544   haribo   Date: 05/07/2016
57

c53a13e0   Jeremy   Updating a lot of...
58
59
60
61
62
63
    def changeDirectory(self, path: str):
        os.chdir(path)
        return 0

    def execute(self) -> int:
        self.changeDirectory(self.path_dir_file)
3224f14a   Jeremy   Fixed some simula...
64
        return self.execProcess("./analyzer '"+str(self.folder)+"' '"+self.plan_folder+"' '"+str(self.plan.duration) + "'")
c53a13e0   Jeremy   Updating a lot of...
65
66
67
68
69
70
71
72
73
74

    def end(self) -> int:
        self.process.wait()
        if self.process.returncode == 0:
            self.log("analysis executed")
            self.logDB(message="Analysis executed successfully for plan " + str(self.plan.name))
        else:
            self.log("analysis failed")
            self.logDB(message="Could not make an analysis for plan " + str(self.plan.name))
        return self.process.returncode
0cf09544   haribo   Date: 05/07/2016
75

c53a13e0   Jeremy   Updating a lot of...
76
77
78
79
80
81
    def run(self, plan_id, folder):
        if self.start(plan_id, folder):
            return 1
        self.execute()
        self.end()
        return 0