36
36
from hashlib import md5
37
37
from re import sub
38
38
from json import loads , dump , dumps
39
+ import signal
40
+ import traceback
39
41
40
42
from qiita_db .util import create_nested_path , retrieve_resource_data
41
43
from qiita_db .util import resource_allocation_plot
@@ -555,7 +557,8 @@ def generate_plugin_releases():
555
557
f (redis_key , v )
556
558
557
559
558
- def update_resource_allocation_redis (active = True , verbose = False ):
560
+ def update_resource_allocation_redis (active = True , verbose = False ,
561
+ time_limit = 300 ):
559
562
"""Updates redis with plots and information about current software.
560
563
561
564
Parameters
@@ -564,7 +567,11 @@ def update_resource_allocation_redis(active=True, verbose=False):
564
567
Defaults to True. Should only be False when testing.
565
568
566
569
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.
568
575
569
576
"""
570
577
time = datetime .now ().strftime ('%m-%d-%y' )
@@ -592,24 +599,24 @@ def update_resource_allocation_redis(active=True, verbose=False):
592
599
cmd_name = command .name
593
600
scommands [sname ][sversion ][cmd_name ] = col_names
594
601
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 = {}
598
605
for sname , versions in scommands .items ():
599
606
for version , commands in versions .items ():
600
607
for cname , col_names in commands .items ():
601
608
df = retrieve_resource_data (cname , sname , version , COLUMNS )
602
609
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 } " ))
607
614
if len (df ) == 0 :
608
615
if verbose :
609
- print (("No allocation resources available for" +
616
+ print (("\n No allocation resources available for" +
610
617
f" software: { sname } " +
611
618
f" version: { version } " +
612
- f" command: { cname } " ))
619
+ f" command: { cname } \n " ))
613
620
continue
614
621
# column_name_str looks like col1*col2*col3, etc
615
622
for col_name in col_names :
@@ -624,16 +631,38 @@ def update_resource_allocation_redis(active=True, verbose=False):
624
631
else :
625
632
new_column *= df_copy [curr_column ]
626
633
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
632
665
633
- fig , axs = resource_allocation_plot (df_copy ,
634
- col_name ,
635
- new_column ,
636
- verbose = verbose )
637
666
titles = [0 , 0 ]
638
667
images = [0 , 0 ]
639
668
@@ -685,14 +714,28 @@ def update_resource_allocation_redis(active=True, verbose=False):
685
714
("title_time" , titles [1 ], r_client .set )
686
715
]
687
716
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' )} " ))
693
724
694
725
for k , v , f in values :
695
726
redis_key = 'resources$#%s$#%s$#%s$#%s:%s' % (
696
727
cname , sname , version , col_name , k )
697
728
r_client .delete (redis_key )
698
729
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