-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstateful.py
92 lines (74 loc) · 2.59 KB
/
stateful.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import typing
from . import tf_cfg
__author__ = "Tempesta Technologies, Inc."
__copyright__ = "Copyright (C) 2018 Tempesta Technologies, Inc."
__license__ = "GPL2"
STATE_BEGIN_START = "begin_start"
STATE_STARTED = "started"
STATE_STOPPED = "stopped"
STATE_ERROR = "error"
class Stateful(object):
"""Class for stateful items, who have states
stopped -> started -> stopped"""
_state = STATE_STOPPED
stop_procedures = []
@property
def state(self) -> str:
return self._state
@state.setter
def state(self, new_state: str) -> None:
if new_state not in [STATE_BEGIN_START, STATE_STARTED, STATE_STOPPED, STATE_ERROR]:
raise ValueError('Please use valid values for "Stateful".')
self._state = new_state
@property
def exceptions(self) -> typing.List[Exception]:
# TODO it should be change after #534 issue
return self._exceptions
def append_exception(self, exception: Exception) -> None:
# TODO it should be change after #534 issue
self._exceptions.append(exception)
self.state = STATE_ERROR
def run_start(self):
"""Should be overridden"""
pass
def restart(self):
self.stop()
self.start()
def start(self, obj=""):
"""Try to start object"""
if self.state != STATE_STOPPED:
if obj == "":
tf_cfg.dbg(3, "Not stopped")
else:
tf_cfg.dbg(3, "%s not stopped" % obj)
return
self.state = STATE_BEGIN_START
self._exceptions = list()
self.run_start()
self.state = STATE_STARTED
def force_stop(self):
"""Stop object"""
for stop_proc in self.stop_procedures:
try:
stop_proc()
except Exception as exc:
tf_cfg.dbg(1, f"Exception in stopping process: {exc}, type: {type(exc)}")
self.append_exception(exc)
if self.state != STATE_ERROR:
self.state = STATE_STOPPED
def stop(self, obj=""):
"""Try to stop object"""
if self.state != STATE_STARTED and self.state != STATE_BEGIN_START:
if obj == "":
tf_cfg.dbg(3, "Not started")
else:
tf_cfg.dbg(3, "%s not started" % obj)
return
self.force_stop()
def is_running(self):
return self.state == STATE_STARTED
def check_exceptions(self):
"""Raise exception if error was received."""
# TODO it should be change after #534 issue
if self.state == STATE_ERROR:
raise self.exceptions[0]