3636from hashlib import md5
3737from re import sub
3838from json import loads , dump , dumps
39+ import signal
40+ import traceback
3941
4042from qiita_db .util import create_nested_path , retrieve_resource_data
4143from qiita_db .util import resource_allocation_plot
@@ -555,7 +557,8 @@ def generate_plugin_releases():
555557 f (redis_key , v )
556558
557559
558- def update_resource_allocation_redis (active = True , verbose = False ):
560+ def update_resource_allocation_redis (active = True , verbose = False ,
561+ time_limit = 300 ):
559562 """Updates redis with plots and information about current software.
560563
561564 Parameters
@@ -564,7 +567,11 @@ def update_resource_allocation_redis(active=True, verbose=False):
564567 Defaults to True. Should only be False when testing.
565568
566569 verbose: boolean, optional
567- Defaults to False. Prints status on what function
570+ Defaults to False. Prints status on what function is running.
571+
572+ time_limit: integer, optional
573+ Defaults to 300, representing 5 minutes. This is the limit for how long
574+ resource_allocation_plot function will run.
568575
569576 """
570577 time = datetime .now ().strftime ('%m-%d-%y' )
@@ -592,24 +599,24 @@ def update_resource_allocation_redis(active=True, verbose=False):
592599 cmd_name = command .name
593600 scommands [sname ][sversion ][cmd_name ] = col_names
594601
595- redis_key = 'resources: commands'
596- r_client . set ( redis_key , str ( scommands ))
597-
602+ # software commands for which resource allocations were sucessfully
603+ # calculated
604+ scommands_allocation = {}
598605 for sname , versions in scommands .items ():
599606 for version , commands in versions .items ():
600607 for cname , col_names in commands .items ():
601608 df = retrieve_resource_data (cname , sname , version , COLUMNS )
602609 if verbose :
603- print (("Retrieving allocation resources for " +
604- f" software: { sname } " +
605- f" version: { version } " +
606- f" command: { cname } " ))
610+ print (("\n Retrieving allocation resources for: \n " +
611+ f" software: { sname } \n " +
612+ f" version: { version } \n " +
613+ f" command: { cname } " ))
607614 if len (df ) == 0 :
608615 if verbose :
609- print (("No allocation resources available for" +
616+ print (("\n No allocation resources available for" +
610617 f" software: { sname } " +
611618 f" version: { version } " +
612- f" command: { cname } " ))
619+ f" command: { cname } \n " ))
613620 continue
614621 # column_name_str looks like col1*col2*col3, etc
615622 for col_name in col_names :
@@ -624,16 +631,38 @@ def update_resource_allocation_redis(active=True, verbose=False):
624631 else :
625632 new_column *= df_copy [curr_column ]
626633 if verbose :
627- print (("Building resource allocation plot for " +
628- f" software: { sname } " +
629- f" version: { version } " +
630- f" command: { cname } " +
631- f" column name: { col_name } " ))
634+ print (
635+ ("\n Building resource allocation plot for:\n " +
636+ f" software: { sname } \n " +
637+ f" version: { version } \n " +
638+ f" command: { cname } \n " +
639+ f" column name: { col_name } \n " +
640+ f" { datetime .now ().strftime ('%b %d %H:%M:%S' )} " ))
641+
642+ def timeout_handler (signum , frame ):
643+ raise TimeoutError ((
644+ "\n resource_allocation_plot " +
645+ "execution exceeded time limit." +
646+ "For:\n "
647+ f" software: { sname } \n " +
648+ f" version: { version } \n " +
649+ f" command: { cname } \n " +
650+ f" column name: { col_name } \n " +
651+ f" { datetime .now ().strftime ('%b %d %H:%M:%S' )} " ))
652+
653+ signal .signal (signal .SIGALRM , timeout_handler )
654+ signal .alarm (time_limit )
655+ try :
656+ fig , axs = resource_allocation_plot (df_copy ,
657+ col_name ,
658+ new_column ,
659+ verbose = verbose )
660+ signal .alarm (0 )
661+ except TimeoutError :
662+ print ("Timeout reached!" )
663+ traceback .print_exc ()
664+ continue
632665
633- fig , axs = resource_allocation_plot (df_copy ,
634- col_name ,
635- new_column ,
636- verbose = verbose )
637666 titles = [0 , 0 ]
638667 images = [0 , 0 ]
639668
@@ -685,14 +714,28 @@ def update_resource_allocation_redis(active=True, verbose=False):
685714 ("title_time" , titles [1 ], r_client .set )
686715 ]
687716 if verbose :
688- print (("Saving resource allocation image for " +
689- f" software: { sname } " +
690- f" version: { version } " +
691- f" command: { cname } " +
692- f" column name: { col_name } " ))
717+ print (
718+ ("Saving resource allocation image for\n " +
719+ f" software: { sname } \n " +
720+ f" version: { version } \n " +
721+ f" command: { cname } \n " +
722+ f" column name: { col_name } \n " +
723+ f" { datetime .now ().strftime ('%b %d %H:%M:%S' )} " ))
693724
694725 for k , v , f in values :
695726 redis_key = 'resources$#%s$#%s$#%s$#%s:%s' % (
696727 cname , sname , version , col_name , k )
697728 r_client .delete (redis_key )
698729 f (redis_key , v )
730+
731+ if sname not in scommands_allocation :
732+ scommands_allocation [sname ] = {}
733+ if version not in scommands_allocation [sname ]:
734+ scommands_allocation [sname ][version ] = {}
735+ if cname not in scommands_allocation [sname ][version ]:
736+ scommands_allocation [sname ][version ][cname ] = []
737+ scommands_allocation [sname ][version ][cname ].append (
738+ col_name )
739+
740+ redis_key = 'resources:commands'
741+ r_client .set (redis_key , str (scommands_allocation ))
0 commit comments