libs.cron.Cron.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
import asyncio
2
import multiprocessing
3
import time
4
5
from .tools import Tools
6
7
"""
8
    Copyright (c) 2020 Star Inc.(https://starinc.xyz)
9
10
    This Source Code Form is subject to the terms of the Mozilla Public
11
    License, v. 2.0. If a copy of the MPL was not distributed with this
12
    file, You can obtain one at http://mozilla.org/MPL/2.0/.
13
"""
14
15
16
class Cron:
17
    def __init__(self, pbp_handle):
18
        """
19
        Create a child process as the scheduler
20
21
        :param pbp_handle: Analytics object
22
        """
23
        self.handle = pbp_handle
24
        self.task = None
25
26
    def start(self):
27
        """
28
        Start scheduler
29
30
        :return:
31
        """
32
        self.task = CronTimer(self.handle)
33
        self.task.start()
34
35
    def stop(self):
36
        """
37
        Stop scheduler
38
39
        :return:
40
        """
41
        if self.task:
42
            self.task.terminate()
43
44
45
class CronTimer(multiprocessing.Process):
46
    def __init__(self, pbp_handle):
47
        """
48
        Scheduler for database
49
50
        :param pbp_handle: Analytics object
51
        """
52
        multiprocessing.Process.__init__(self)
53
        self.handle = pbp_handle
54
        self.last_time = -1
55
56
    def run(self):
57
        """
58
        Task of Scheduler to check databases
59
60
        :return:
61
        """
62
        while True:
63
            if time.localtime().tm_hour != self.last_time:
64
                try:
65
                    threads = Update(self.handle)
66
                    threads.start()
67
                    threads.join()
68
                    print(
69
                        Tools.get_time(),
70
                        "[Update] Database was refreshed."
71
                    )
72
                except:
73
                    error_report = Tools.error_report()
74
                    Tools.logger(error_report)
75
                if self.last_time == -1:
76
                    Tools.set_ready(True)
77
                self.last_time = time.localtime().tm_hour
78
79
80
class Update(multiprocessing.Process):
81
    def __init__(self, pbp_handle):
82
        """
83
        Action to update databases
84
85
        :param pbp_handle: Analytics object
86
        """
87
        multiprocessing.Process.__init__(self)
88
        self.handle = pbp_handle
89
90
    def run(self):
91
        """
92
        Action to checkout
93
94
        :return:
95
        """
96
        asyncio.run(self.handle.gen_sample())
97
        self.handle.data_control.clean_result_cache()
98
        self.handle.update_blacklist_from_phishtank()
99