55
66import numpy as np
77import pyvista
8+ import sphinx
9+ from sphinx .util import logging
810from ansys .dpf .core import __version__ , server , server_factory
911from ansys .dpf .core .examples import get_example_required_minimum_dpf_version
1012from ansys_sphinx_theme import ansys_favicon , get_version_match , pyansys_logo_light_mode , pyansys_logo_dark_mode
@@ -158,7 +160,7 @@ def reset_servers(gallery_conf, fname, when):
158160 try :
159161 # check whether the process name matches
160162 if proc_name in proc .name ():
161- # proc.kill()
163+ proc .kill ()
162164 nb_procs += 1
163165 except psutil .NoSuchProcess :
164166 pass
@@ -326,3 +328,89 @@ def reset_servers(gallery_conf, fname, when):
326328
327329# A list of files that should not be packed into the epub file.
328330epub_exclude_files = ["search.html" ]
331+
332+
333+ def check_global_servers_and_close (app : sphinx .application .Sphinx , exception : Exception ) -> None :
334+ from ansys .dpf .core import server , _server_instances
335+ import copy
336+ import psutil
337+ import gc
338+
339+ gc .collect
340+
341+ # Server instances are different from processes.
342+ # Ideally just closing the servers => no running processes
343+ # Server instances should be closed first
344+ if _server_instances :
345+ print (f"{ len (_server_instances )} dpf server instances found running" )
346+ print ("Closing server instances" )
347+
348+ copy_instances = copy .deepcopy (_server_instances )
349+ for instance in copy_instances :
350+ try :
351+ if hasattr (instance (), "shutdown" ):
352+ instance ().shutdown ()
353+ except Exception as e :
354+ print (e .args )
355+ pass
356+ server .shutdown_global_server ()
357+ print (f"{ len (_server_instances )} dpf server instances found running after closing" )
358+ else :
359+ print ("no server instances found running" )
360+
361+ # Subsequently check running processes and close
362+ proc_name = "Ans.Dpf.Grpc"
363+ nb_procs = 0
364+ for proc in psutil .process_iter ():
365+ try :
366+ # check whether the process name matches
367+ if proc_name in proc .name ():
368+ proc .kill ()
369+ nb_procs += 1
370+ except psutil .NoSuchProcess :
371+ pass
372+
373+ # Check if processes were actually killed
374+ if nb_procs :
375+ print (f"Killed { nb_procs } { proc_name } processes." )
376+ else :
377+ print (f"No processes were found running" )
378+
379+ # def reset_server_after_sphinx_gallery(app: sphinx.application.Sphinx, exception: Exception) -> None:
380+ # """
381+ # Stop running dpf servers before regardless of documentation build sucess.
382+
383+ # Parameters
384+ # ----------
385+ # app : sphinx.application.Sphinx
386+ # Sphinx application instance containing the all the doc build configuration.
387+
388+ # """
389+ # from ansys.dpf.core import server
390+
391+ # logger = logging.getLogger(__name__)
392+
393+ # try:
394+ # server.check_global_servers_and_close()
395+ # except exception:
396+ # if "Extension error" in str(exception):
397+ # server.check_global_servers_and_close()
398+ # logger.warning("An error occurred during sphinx gallery execution")
399+ # raise exception
400+ # else:
401+ # logger.warning("An error outside sphinx gallery execution occurred")
402+ # raise exception
403+
404+
405+ def setup (app : sphinx .application .Sphinx ) -> None :
406+ """
407+ Run hook function(s) during the documentation build.
408+
409+ Parameters
410+ ----------
411+ app : sphinx.application.Sphinx
412+ Sphinx application instance containing the all the doc build configuration.
413+ """
414+
415+ app .connect ("builder-inited" , check_global_servers_and_close )
416+ app .connect ("build-finished" , check_global_servers_and_close )
0 commit comments