Skip to content

Commit beb93af

Browse files
committed
Update process_checker.py
Removed Globals Added ProcessMonitorData class Added Two Functions to add to choicelist and dict Fixed logger issue removed print statements and now using all debug/info/error
1 parent ae8a175 commit beb93af

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

src/process_checker.py

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,20 @@
2929
# DEFAULT_CONFIG_SAVE_PATH = path.join(path.dirname(path.realpath(__file__)), "color_config.json")
3030

3131

32+
class ProcessMonitorData:
33+
def __init__(self):
34+
self.process_monitor_choiceList = []
35+
self.process_monitor_dict = {}
3236

37+
def add_to_choiceList(self, item):
38+
self.process_monitor_choiceList.append(item)
39+
40+
def add_to_dict(self, key, value):
41+
self.process_monitor_dict[key] = value
3342

34-
process_monitor_choiceList = []
35-
process_monitor_dict = {}
43+
PM = ProcessMonitorData()
44+
#process_monitor_choiceList = []
45+
#process_monitor_dict = {}
3646

3747
class ProcessChecker:
3848
def __init__(self, process_name):
@@ -52,7 +62,8 @@ def time_completion(self, data, the_process):
5262
def the_task(self, process_name, the_process):
5363
process_checked = self.is_running()
5464

55-
global process_monitor_choiceList
65+
66+
# global process_monitor_choiceList
5667

5768
# print("This is process checked the is_running stuff", process_checked)
5869

@@ -68,21 +79,23 @@ def the_task(self, process_name, the_process):
6879
TPClient.createState(stateId=PLUGIN_ID + f".state.{self.process_name}.process_info.status", description=f"PM | {self.process_name} - status", value="Closed", parentGroup=str(self.process_name))
6980

7081
if process_checked:
71-
process_monitor_dict[process_name] = the_process
72-
the_list = list(process_monitor_dict.keys())
82+
PM.add_to_dict(self.process_name, the_process)
83+
# process_monitor_dict[process_name] = the_process
84+
the_list = list(PM.process_monitor_dict.keys())
7385
the_list.append("ALL")
7486

7587
# Checking to see if the Process monitor Choice List is the same, if so we dont update it
76-
if process_monitor_choiceList != the_list:
88+
if PM.process_monitor_choiceList != the_list:
7789
## submitted a PR for this to be added to the API by default
7890
TPClient.choiceUpdate(choiceId=PLUGIN_ID + ".act.process_name.stop", values=the_list)
79-
80-
process_monitor_choiceList = the_list
91+
92+
PM.add_to_choiceList(the_list)
93+
# process_monitor_choiceList = the_list
8194

8295
## update a state showing how many values are in the list minus the "ALL" value
8396
TPClient.stateUpdate(stateId=PLUGIN_ID + ".state.process_monitor.count", stateValue=str(len(the_list) - 1))
8497

85-
# print(f"{the_process.process_name} is running")
98+
g_log.debug(f"{the_process.process_name} is running")
8699

87100
for x in process_checked:
88101
if x == 'memory_percent':
@@ -120,7 +133,6 @@ def is_running(self):
120133
def check_continuously(self, interval, process_name, the_process):
121134
while self.should_continue:
122135
g_log.debug("Checking if " + self.process_name + " is running")
123-
# print("Checking if ", self.process_name, " is running")
124136

125137
self.the_task(process_name = process_name, the_process=the_process)
126138
time.sleep(interval)
@@ -184,10 +196,9 @@ def onSettingUpdate(data):
184196

185197
@TPClient.on(TP.TYPES.onAction)
186198
def onAction(data):
187-
global process_monitor_dict
199+
# global process_monitor_dict
188200
g_log.debug(f"Action: {data}")
189201

190-
print(data)
191202

192203
if not (action_data := data.get('data')) or not (aid := data.get('actionId')):
193204
return
@@ -200,10 +211,10 @@ def onAction(data):
200211
the_process = ProcessChecker(data['data'][0]['value'])
201212
the_process.the_task(process_name=data['data'][0]['value'], the_process=the_process)
202213
else:
203-
print(f"Checking every {str(data['data'][1]['value'])} seconds for {data['data'][0]['value']}")
214+
g_log.info('Checking every ' + str(data['data'][1]['value']) + ' seconds for ' + data['data'][0]['value'])
204215
the_process = ProcessChecker(data['data'][0]['value'])
205216

206-
if data['data'][0]['value'] not in process_monitor_dict.keys():
217+
if data['data'][0]['value'] not in PM.process_monitor_dict.keys():
207218

208219
th = threading.Thread(target=the_process.check_continuously, args=(int(data['data'][1]['value']), data['data'][0]['value'], the_process))
209220
th.start()
@@ -216,31 +227,27 @@ def onAction(data):
216227
the_process = data['data'][0]['value']
217228
try:
218229
if the_process =="ALL":
219-
# print("Trying to remove all processes from the dict")
220-
for x in process_monitor_dict:
221-
222-
# print("Stopping the process: ", x)
230+
for x in PM.process_monitor_dict:
231+
g_log.info(f"Stopping the process monitor for: {x}")
223232
if x != "ALL":
224-
process_monitor_dict[x].stop()
225-
g_log.debug(stopped_process := f"Stopping the process: {x}")
233+
PM.process_monitor_dict[x].stop()
234+
g_log.info(f"Stopping the process monitor for: {x}")
226235

227-
process_monitor_dict = {}
236+
PM.process_monitor_dict = {}
228237
else:
229-
process_monitor_dict[the_process].stop()
238+
PM.process_monitor_dict[the_process].stop()
230239
## delete the key from the dict
231-
# print("Trying to remove the process {} from the dict".format(the_process))
232-
process_monitor_dict.pop(the_process)
240+
g_log.info(f"Stopping the process monitor for: {the_process}")
241+
PM.process_monitor_dict.pop(the_process)
233242

234-
TPClient.stateUpdate(stateId=PLUGIN_ID + ".state.process_monitor.count", stateValue=str(len(process_monitor_dict.keys())))
235-
# del process_monitor_dict[the_process]
243+
TPClient.stateUpdate(stateId=PLUGIN_ID + ".state.process_monitor.count", stateValue=str(len(PM.process_monitor_dict.keys())))
236244
except Exception as e:
237-
print(e)
245+
g_log.error(f"Error stopping the process: {e}")
238246

239247

240248

241249

242250
def handleSettings(settings, on_connect=False):
243-
#print("Settings: " + str(settings))
244251
pass
245252

246253

@@ -343,10 +350,10 @@ def main():
343350
except Exception as e:
344351
opts.s = True
345352
print(f"Error while creating file logger, falling back to stdout. {repr(e)}")
346-
if not opts.l or opts.s:
347-
sh = StreamHandler(sys.stdout)
348-
sh.setFormatter(fmt)
349-
g_log.addHandler(sh)
353+
#if not opts.l or opts.s:
354+
# sh = StreamHandler(sys.stdout)
355+
# sh.setFormatter(fmt)
356+
# g_log.addHandler(sh)
350357

351358

352359
try:

0 commit comments

Comments
 (0)