Skip to content

Commit 3d93e8e

Browse files
committed
fixed repeated updates on closed processes
fixed repeated updates on closed processes - Thanks `Dauthiatull` for reporting the issue small performance tweak as well with creating states
1 parent 8282fad commit 3d93e8e

File tree

5 files changed

+74
-41
lines changed

5 files changed

+74
-41
lines changed
2.64 KB
Binary file not shown.
2.03 KB
Binary file not shown.

src/entry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44

5-
__version__ = "1.7.0"
5+
__version__ = "1.7.1"
66
PLUGIN_ID = "tp.plugin.process_monitor"
77
PLUGIN_NAME = "Process_Monitor"
88

src/entry.tp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": 6,
3-
"version": 170,
3+
"version": 171,
44
"name": "Process Monitor",
55
"id": "tp.plugin.process_monitor",
66
"configuration": {

src/process_checker.py

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
PLUGIN_ID = "tp.plugin.process_monitor"
2727
GITHUB_URL = "process-monitor-touchportal-plugin"
2828

29+
DEFAULT_STATES = ['pid', 'username', 'cpu_percent', 'memory_percent', 'cmdline', 'create_time', 'status']
2930

3031

3132
class ProcessMonitorData:
@@ -54,58 +55,90 @@ def time_completion(self, data, the_process):
5455
print("Task completed in: ", completion_time, " seconds")
5556

5657

58+
def setClosedState(self):
59+
stateList = []
60+
for x in DEFAULT_STATES:
61+
if x == 'status':
62+
# Updating Status to "Closed" since the process appears to not be running
63+
stateList.append({
64+
"id": PLUGIN_ID + f".state.{self.process_name}.process_info.status",
65+
"desc": f"PM | {self.process_name} - status",
66+
"value":"closed",
67+
'parentGroup':str(self.process_name)
68+
})
69+
else:
70+
stateList.append({
71+
"id": PLUGIN_ID + f".state.{self.process_name}.process_info.{x}",
72+
"desc": f"PM | {self.process_name} - {x}",
73+
"value":"", 'parentGroup':str(self.process_name),
74+
"parentGroup": str(self.process_name)
75+
})
76+
77+
TPC.TPClient.createStateMany(stateList)
78+
79+
80+
def setRunningState(self, process_checked):
81+
82+
print("Process Checked: ", process_checked)
83+
stateList = []
84+
for x in process_checked:
85+
if x == 'memory_percent':
86+
## Round it to the 2nd decimal place
87+
process_checked[x] = round(process_checked[x], 2)
88+
89+
if x == 'create_time':
90+
create_time = process_checked.get('create_time', "None")
91+
if create_time is not None:
92+
create_time_datetime = datetime.fromtimestamp(create_time)
93+
process_checked[x] = create_time_datetime.strftime("%m/%d/%Y [%I:%M:%S %p]")
94+
95+
stateList.append({
96+
"id": PLUGIN_ID + f".state.{self.process_name}.process_info.{x}",
97+
"desc": f"PM | {self.process_name} - {x}",
98+
"value":str(process_checked.get(x, "None")),
99+
"parentGroup": str(self.process_name)
100+
})
101+
102+
TPC.TPClient.createStateMany(stateList)
103+
104+
105+
106+
def addToChoiceList(self, the_process):
107+
PM.add_to_dict(self.process_name, the_process)
108+
choice_list = list(PM.process_monitor_dict.keys())
109+
choice_list.append("ALL")
110+
111+
# Checking to see if the Process monitor Choice List is the same, if so we dont update it
112+
if PM.process_monitor_choiceList != choice_list:
113+
## submitted a PR for this to be added to the API by default
114+
TPC.TPClient.choiceUpdate(choiceId=PLUGIN_ID + ".act.process_name.stop", values=choice_list)
115+
116+
PM.add_to_choiceList(choice_list)
117+
# process_monitor_choiceList = the_list
118+
119+
## update a state showing how many values are in the list minus the "ALL" value
120+
TPC.TPClient.stateUpdate(stateId=PLUGIN_ID + ".state.process_monitor.count", stateValue=str(len(choice_list) - 1))
121+
122+
57123
def the_task(self, process_name, the_process):
58124
process_checked = self.is_running()
125+
print("Process Checked: ", process_checked)
59126

60127
if process_checked == False:
61-
## Setting Status to blank so it will trigger every time its checked
62-
63-
TPC.TPClient.createState(stateId=PLUGIN_ID + f".state.{self.process_name}.process_info.status", description=f"PM | {self.process_name} - status", value="", parentGroup=str(self.process_name))
64-
65-
for x in ['pid', 'username', 'cpu_percent', 'memory_percent', 'cmdline', 'create_time']:
66-
TPC.TPClient.createState(stateId=PLUGIN_ID + f".state.{self.process_name}.process_info.{x}", description=f"PM | {self.process_name} - {x}", value="", parentGroup=str(self.process_name))
67-
68-
# Updating Status to "Closed" since the process appears to not be running
69-
TPC.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))
128+
self.setClosedState()
70129

71130
if process_checked:
72-
PM.add_to_dict(self.process_name, the_process)
73-
# process_monitor_dict[process_name] = the_process
74-
the_list = list(PM.process_monitor_dict.keys())
75-
the_list.append("ALL")
76-
77-
# Checking to see if the Process monitor Choice List is the same, if so we dont update it
78-
if PM.process_monitor_choiceList != the_list:
79-
## submitted a PR for this to be added to the API by default
80-
TPC.TPClient.choiceUpdate(choiceId=PLUGIN_ID + ".act.process_name.stop", values=the_list)
131+
self.addToChoiceList(the_process)
132+
self.setRunningState(process_checked)
81133

82-
PM.add_to_choiceList(the_list)
83-
# process_monitor_choiceList = the_list
84-
85-
## update a state showing how many values are in the list minus the "ALL" value
86-
TPC.TPClient.stateUpdate(stateId=PLUGIN_ID + ".state.process_monitor.count", stateValue=str(len(the_list) - 1))
87-
88134
TPC.g_log.debug(f"{the_process.process_name} is running")
89135

90-
for x in process_checked:
91-
if x == 'memory_percent':
92-
## Round it to the 2nd decimal place
93-
process_checked[x] = round(process_checked[x], 2)
94-
95-
if x == 'create_time':
96-
create_time = process_checked.get('create_time', "None")
97-
if create_time is not None:
98-
create_time_datetime = datetime.fromtimestamp(create_time)
99-
process_checked[x] = create_time_datetime.strftime("%m/%d/%Y [%I:%M:%S %p]")
100-
101-
TPC.TPClient.createState(stateId=PLUGIN_ID + f".state.{the_process.process_name}.process_info.{x}", description=f"PM | {the_process.process_name} - {x}", value=str(process_checked.get(x, "None")), parentGroup=str(the_process.process_name))
102136

103137

104138
def is_running(self):
105139
for process in psutil.process_iter():
106140
if process.name().lower() == self.process_name.lower():
107-
process_Info = process.as_dict(attrs=['pid', 'name', 'username', 'cpu_percent', 'memory_percent', 'cmdline', 'create_time', 'status'])
108-
# print("Before joiing the cmdline: ", process_checked)
141+
process_Info = process.as_dict(attrs=DEFAULT_STATES)
109142
process_Info["cmdline"] = ' '.join(process_Info["cmdline"])
110143
return process_Info
111144

@@ -256,7 +289,7 @@ def onAction(data):
256289

257290
TPC.TPClient.stateUpdate(stateId=PLUGIN_ID + ".state.process_monitor.count", stateValue=str(len(PM.process_monitor_dict.keys())))
258291
except Exception as e:
259-
TPC.g_log.error(f"Error stopping the process: {e}")
292+
TPC.g_log.error(f"Error stopping the process: {e} - Current Monitors: {PM.process_monitor_dict}")
260293

261294

262295
def handleSettings(settings, on_connect=False):

0 commit comments

Comments
 (0)