File tree Expand file tree Collapse file tree 3 files changed +46
-3
lines changed Expand file tree Collapse file tree 3 files changed +46
-3
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,18 @@ Library functions to monitor state of systemd spawned units
31
31
time.sleep(60 )
32
32
```
33
33
34
- 2 . Get unit properties
34
+ 2 . Single- thread realtime monitoring
35
+
36
+ ```python
37
+ from systemd_monitor.journald_worker import JournaldWorker
38
+
39
+ worker = JournaldWorker()
40
+
41
+ for unit, state in worker.run_iterate():
42
+ print (" Update received for unit {} " .format(unit))
43
+ ```
44
+
45
+ 3 . Get unit properties
35
46
36
47
```python
37
48
from systemd_monitor.systemd_dbus_adapter import SystemdDBusAdapter
Original file line number Diff line number Diff line change @@ -80,7 +80,8 @@ def __load_states(self):
80
80
'''
81
81
initial services load
82
82
'''
83
- self .__states = self .__systemd .get_all ()
83
+ with self .__lock :
84
+ self .__states = self .__systemd .get_all ()
84
85
85
86
def run (self ):
86
87
'''
@@ -112,6 +113,37 @@ def run(self):
112
113
self .__status = False
113
114
time .sleep (60 )
114
115
116
+ def run_iterate (self ):
117
+ '''
118
+ run thread and return states right in time
119
+ '''
120
+ self .__load_states ()
121
+ while self .__should_run :
122
+ try :
123
+ for line in self .__get_journal ():
124
+ self .__status = True
125
+ # exit on no actions
126
+ if not self .__should_run :
127
+ break
128
+ # parse line
129
+ if isinstance (line , bytes ):
130
+ line = line .decode ('utf8' )
131
+ data = json .loads (line )
132
+ # check if it is unit related infomation
133
+ if 'UNIT' not in data :
134
+ continue
135
+ # update internal storage
136
+ with self .__lock :
137
+ self .__states [
138
+ data ['UNIT' ]] = self .__systemd .get (
139
+ data ['UNIT' ])
140
+ self .__lst = datetime .datetime .utcnow ()
141
+ yield data ['UNIT' ], self .__states [data ['UNIT' ]]
142
+ except Exception :
143
+ self .__logger .warning ('Exception recieved' , exc_info = True )
144
+ self .__status = False
145
+ time .sleep (60 )
146
+
115
147
def stop (self ):
116
148
'''
117
149
stop worker
Original file line number Diff line number Diff line change @@ -113,7 +113,7 @@ def get(self, unit):
113
113
# type defined properties
114
114
interface = self .__define_type (unit )
115
115
properties .update (service .GetAll (interface ))
116
- return systemd_unit_state .SystemdUnit (path , ** properties )
116
+ return systemd_unit .SystemdUnit (path , ** properties )
117
117
118
118
def reload_unit (self , unit , mode = 'replace' ):
119
119
'''
You can’t perform that action at this time.
0 commit comments