11import getpass
2+ from time import sleep
23
34import click
45from druncschema .process_manager_pb2 import LogRequest , ProcessQuery
1112)
1213from drunc .process_manager .interface .context import ProcessManagerContext
1314from drunc .process_manager .utils import tabulate_process_instance_list
14- from drunc .utils .shell_utils import InterruptedCommand
15+ from drunc .utils .shell_utils import InterruptedCommand , log_pm_cmd
1516from drunc .utils .utils import get_logger
1617
1718
@@ -43,6 +44,7 @@ def boot(
4344 override_logs : bool ,
4445) -> None :
4546 log = get_logger ("process_manager.shell" )
47+ log_pm_cmd (obj )
4648 processes = obj .get_driver ("process_manager" ).ps (ProcessQuery (user = user ))
4749
4850 if len (processes .values ) > 0 :
@@ -129,6 +131,7 @@ def dummy_boot(
129131 session_name : str ,
130132) -> None :
131133 log = get_logger ("process_manager.shell" )
134+ log_pm_cmd (obj )
132135 log .debug (
133136 f"Running dummy_boot with { n_processes } processes for { sleep } seconds { n_sleeps } times, requested by user { user } "
134137 )
@@ -150,6 +153,16 @@ def dummy_boot(
150153 return
151154
152155
156+ @click .command ("wait" )
157+ @click .argument ("sleep_time" , type = int , default = 1 )
158+ @click .pass_obj
159+ def wait (obj : ProcessManagerContext , sleep_time : int ) -> None :
160+ log = get_logger ("process_manager.wait" )
161+ log .info (f"Command [green]wait[/green] running for { sleep_time } seconds." )
162+ sleep (sleep_time ) # seconds
163+ log .info (f"Command [green]wait[/green] ran for { sleep_time } seconds." )
164+
165+
153166@click .command ("terminate" )
154167@click .option (
155168 "-w" ,
@@ -161,6 +174,7 @@ def dummy_boot(
161174@click .pass_obj
162175def terminate (obj : ProcessManagerContext , width : int | None ) -> None :
163176 log = get_logger ("process_manager.shell" )
177+ log_pm_cmd (obj )
164178 log .debug ("Terminating" )
165179 result = obj .get_driver ("process_manager" ).terminate ()
166180 if not result :
@@ -171,23 +185,35 @@ def terminate(obj: ProcessManagerContext, width: int | None) -> None:
171185 obj .delete_driver ("controller" )
172186
173187
188+ def kill_decorators (f ):
189+ f = click .pass_obj (f )
190+ f = click .option (
191+ "-w" ,
192+ "--width" ,
193+ type = int ,
194+ default = None ,
195+ help = "Table width. Default is automatically calculated" ,
196+ )(f )
197+ f = click .option (
198+ "--crash" ,
199+ is_flag = True ,
200+ default = False ,
201+ help = "Simulate a crash: send SIGKILL without any cleanup, leaving the process manager in an unexpected-death state." ,
202+ )(f )
203+ return f
204+
205+
174206@click .command ("kill" )
175- @click .option (
176- "-w" ,
177- "--width" ,
178- type = int ,
179- default = None ,
180- help = "Table width. Default is automatically calculated" ,
181- )
182207@add_query_options (at_least_one = True )
183- @click .option (
184- "--crash" ,
185- is_flag = True ,
186- default = False ,
187- help = "Simulate a crash: send SIGKILL without any cleanup, leaving the process manager in an unexpected-death state." ,
188- )
189- @click .pass_obj
190- def kill (obj : ProcessManagerContext , query : ProcessQuery , width : int | None ) -> None :
208+ @kill_decorators
209+ def kill (obj , query , width ):
210+ log_pm_cmd (obj )
211+ return kill_impl (obj , query , width )
212+
213+
214+ def kill_impl (
215+ obj : ProcessManagerContext , query : ProcessQuery , width : int | None
216+ ) -> None :
191217 log = get_logger ("process_manager.shell" )
192218 log .debug (f"Killing with query { query } " )
193219 result = obj .get_driver ("process_manager" ).kill (query )
@@ -198,17 +224,27 @@ def kill(obj: ProcessManagerContext, query: ProcessQuery, width: int | None) ->
198224 ) # rich tables require console printing
199225
200226
227+ def flush_decorators (f ):
228+ f = click .pass_obj (f )
229+ f = click .option (
230+ "-w" ,
231+ "--width" ,
232+ type = int ,
233+ default = None ,
234+ help = "Table width. Default is automatically calculated" ,
235+ )(f )
236+ return f
237+
238+
201239@click .command ("flush" )
202- @click .option (
203- "-w" ,
204- "--width" ,
205- type = int ,
206- default = None ,
207- help = "Table width. Default is automatically calculated" ,
208- )
209240@add_query_options (at_least_one = False , all_processes_by_default = True )
210- @click .pass_obj
211- def flush (
241+ @flush_decorators
242+ def flush (obj , query , width ):
243+ log_pm_cmd (obj )
244+ return flush_impl (obj , query , width )
245+
246+
247+ def flush_impl (
212248 obj : ProcessManagerContext ,
213249 query : ProcessQuery ,
214250 width : int | None ,
@@ -223,22 +259,34 @@ def flush(
223259 ) # rich tables require console printing
224260
225261
262+ def logs_decorators (f ):
263+ f = click .pass_obj (f )
264+ f = click .option ("--grep" , type = str , default = None )(f )
265+ f = click .option (
266+ "--how-far" ,
267+ type = int ,
268+ show_default = True ,
269+ default = 100 ,
270+ help = "How many lines one wants" ,
271+ )(f )
272+ return f
273+
274+
226275@click .command ("logs" )
227276@add_query_options (at_least_one = True )
228- @click .option (
229- "--how-far" ,
230- type = int ,
231- show_default = True ,
232- default = 100 ,
233- help = "How many lines one wants" ,
234- )
235- @click .option ("--grep" , type = str , default = None )
236- @click .pass_obj
237- def logs (
277+ @logs_decorators
278+ def logs (obj , how_far , grep , query ):
279+ log_pm_cmd (obj )
280+ return logs_impl (obj , how_far , grep , query )
281+
282+
283+ def logs_impl (
238284 obj : ProcessManagerContext , how_far : int , grep : str , query : ProcessQuery
239285) -> None :
240286 log = get_logger ("process_manager.shell" )
241- log .debug (f"Running logs with query { query } " )
287+ # TODO: MOVE BACK TO DEBUG BEFORE MERGE
288+ # THIS IS USEFUL FOR TESTING THOUGH
289+ log .error (f"Running logs with query { query } " )
242290 log_req = LogRequest (
243291 how_far = how_far ,
244292 query = query ,
@@ -276,6 +324,11 @@ def logs(
276324@add_query_options (at_least_one = True )
277325@click .pass_obj
278326def restart (obj : ProcessManagerContext , query : ProcessQuery ) -> None :
327+ log_pm_cmd (obj )
328+ return restart_impl (obj , query )
329+
330+
331+ def restart_impl (obj : ProcessManagerContext , query : ProcessQuery ) -> None :
279332 log = get_logger ("process_manager.shell" )
280333 log .debug (f"Restarting with query { query } " )
281334 obj .get_driver ("process_manager" ).restart (query )
@@ -306,6 +359,7 @@ def ps(
306359 width : int | None ,
307360) -> None :
308361 log = get_logger ("process_manager.shell" )
362+ log_pm_cmd (obj )
309363 log .debug (f"Running ps with query { query } " )
310364 results = obj .get_driver ("process_manager" ).ps (query )
311365 if not results :
0 commit comments