33import sys
44import threading
55import time
6+ from collections import Counter
67
78from daqpytools .logging import LogHandlerConf , exceptions , setup_daq_ers_logger
89from druncschema .authoriser_pb2 import ActionType , SystemType
@@ -170,35 +171,48 @@ def find_by_uuid(pi_list, target_uuid: str):
170171 return pi
171172 return None
172173
174+ tech_name = self .configuration .pm_type .name
175+
176+ #! Evaluate code for these with the dead check down below
173177 n_dead_prev = 0
174178 dead_processes_prev = set ()
179+
175180 while not self .stop_event .is_set ():
176181 results = self ._ps_impl (q )
177182
178- n_running = sum (
179- 1
180- for process in results .values
181- if process .status_code == ProcessInstance .StatusCode .RUNNING
182- )
183- dead_processes = {
184- process .uuid .uuid
185- for process in results .values
186- if process .status_code == ProcessInstance .StatusCode .DEAD
187- }
183+ session_running = Counter ()
184+ session_dead = Counter ()
185+ dead_processes = set ()
186+
187+ for process in results .values :
188+ session = f"{ tech_name } _{ process .process_description .metadata .session } "
189+ status = process .status_code
190+
191+ if status == ProcessInstance .StatusCode .RUNNING :
192+ session_running [session ] += 1
193+
194+ elif status == ProcessInstance .StatusCode .DEAD :
195+ session_dead [session ] += 1
196+ dead_processes .add (process .uuid .uuid )
197+
198+ # merge all known sessions from both counters
199+ all_sessions = session_running .keys () | session_dead .keys ()
200+
201+ # For future developers, if this is still slow consider using async.
202+ for sesh in all_sessions :
203+ self .opmon_publisher .publish (
204+ message = ProcessStatus (
205+ n_running = session_running [sesh ],
206+ n_dead = session_dead [sesh ],
207+ n_session = 1 , #! Can we change the schema? will it affect the influxdb?
208+ ),
209+ custom_origin = {"drunc_session" : sesh },
210+ )
211+
188212 n_dead = len (dead_processes )
189- n_session = len (
190- {
191- process .process_description .metadata .session
192- for process in results .values
193- }
194- )
195- self .opmon_publisher .publish (
196- message = ProcessStatus (
197- n_running = n_running , n_dead = n_dead , n_session = n_session
198- ),
199- )
200213 if n_dead_prev < n_dead :
201214 n_dead_prev = n_dead
215+ #! Ask pawel whats going on with the dead process check
202216 diff_set = dead_processes - dead_processes_prev
203217 for diff in diff_set :
204218 if diff in self .expected_dead_applications :
@@ -218,6 +232,7 @@ def find_by_uuid(pi_list, target_uuid: str):
218232 "drunc.process_manager" ,
219233 )
220234 self .log .critical (err_msg , extra = self .handlerconf .ERS )
235+ self .log .warning (err_msg )
221236
222237 time .sleep (interval_s )
223238
0 commit comments