crons.py 2.78 KB
Newer Older
1
2
import os
import sys
3
import re
Floreal Cabanettes's avatar
Floreal Cabanettes committed
4
import getpass
5
import psutil
Floreal Cabanettes's avatar
Floreal Cabanettes committed
6
from crontab import CronTab
7
from dgenies.config_reader import AppConfigReader
Floreal Cabanettes's avatar
Floreal Cabanettes committed
8
9
10
11


class Crons:

12
    def __init__(self, base_dir, debug):
Floreal Cabanettes's avatar
Floreal Cabanettes committed
13
        self.base_dir = base_dir
14
        self.debug = debug
Floreal Cabanettes's avatar
Floreal Cabanettes committed
15
        self.my_cron = CronTab(user=getpass.getuser())
Floreal Cabanettes's avatar
Floreal Cabanettes committed
16
        self.config = AppConfigReader()
Floreal Cabanettes's avatar
Floreal Cabanettes committed
17
        self.local_scheduler_pid_file = os.path.join(self.config.config_dir, ".local_scheduler_pid")
Floreal Cabanettes's avatar
Floreal Cabanettes committed
18

19
    def clear(self, kill_scheduler=True):
Floreal Cabanettes's avatar
Floreal Cabanettes committed
20
21
22
        # Remove old crons:
        self.my_cron.remove_all(comment="dgenies")
        self.my_cron.write()
23
24
25
26
27
28
29
30
        if kill_scheduler:
            # Kill local scheduler:
            if os.path.exists(self.local_scheduler_pid_file):
                with open(self.local_scheduler_pid_file) as p_f:
                    pid = int(p_f.readline().strip("\n"))
                    if psutil.pid_exists(pid):
                        p = psutil.Process(pid)
                        p.terminate()
Floreal Cabanettes's avatar
Floreal Cabanettes committed
31

Floreal Cabanettes's avatar
Floreal Cabanettes committed
32
    def start_all(self):
33
        self.clear(False)
34
        self.init_clean_cron()
35
        self.init_launch_local_cron()
Floreal Cabanettes's avatar
Floreal Cabanettes committed
36

Floreal Cabanettes's avatar
Floreal Cabanettes committed
37
38
39
40
41
42
43
44
    @staticmethod
    def _get_python_exec():
        pyexec = sys.executable
        match = re.match(r"^(.+)/lib/(python[^/]+)/((site-packages/bin/python)|())$", pyexec)
        if match:
            pyexec = "%s/bin/%s" % (match.group(1), match.group(2))
        return pyexec

45
    def init_clean_cron(self):
Floreal Cabanettes's avatar
Floreal Cabanettes committed
46
        """
47
        Clean cron is launched at 1h00am each day
Floreal Cabanettes's avatar
Floreal Cabanettes committed
48
        """
49
50
        clean_time = self.config.cron_clean_time
        clean_freq = self.config.cron_clean_freq
Floreal Cabanettes's avatar
Floreal Cabanettes committed
51
        if self.base_dir is not None:
Floreal Cabanettes's avatar
Floreal Cabanettes committed
52
53
54
            job = self.my_cron.new(self._get_python_exec() +
                                   " {0}/bin/clean_jobs.py > {1}/clean.log 2>&1".format(self.base_dir,
                                                                                        self.config.log_dir),
Floreal Cabanettes's avatar
Floreal Cabanettes committed
55
                                   comment="dgenies")
56
57
58
            job.day.every(clean_freq)
            job.hour.on(clean_time[0])
            job.minute.on(clean_time[1])
Floreal Cabanettes's avatar
Floreal Cabanettes committed
59
60
61
            self.my_cron.write()
        else:
            raise Exception("Crons: base_dir must not be None")
62
63
64
65
66
67
68

    def init_launch_local_cron(self):
        """
        Try to launch local scheduler (if not already launched)
        :return:
        """
        if self.base_dir is not None:
Floreal Cabanettes's avatar
Floreal Cabanettes committed
69
            pyexec = self._get_python_exec()
70
71
72
            logs = os.path.join(self.config.log_dir, "local_scheduler.log") if self.debug else "/dev/null"
            job = self.my_cron.new("{0}/bin/start_local_scheduler.sh {0} {1} {2} {3} > /dev/null 2>&1 &".
                                   format(self.base_dir, pyexec, self.local_scheduler_pid_file, logs),
73
74
75
                                   comment="dgenies")
            job.minute.every(1)
            self.my_cron.write()