From 2c7f73aecb9c04a11025f9946444f752e8efc5c7 Mon Sep 17 00:00:00 2001
From: Leo Gordon <lg4@ebi.ac.uk>
Date: Wed, 8 Jan 2014 17:11:19 +0000
Subject: [PATCH] If greedy disoriented Workers overshoot
 analysis_capacity|hive_capacity, make sure they respecialize after the first
 batch

---
 modules/Bio/EnsEMBL/Hive/Worker.pm | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/modules/Bio/EnsEMBL/Hive/Worker.pm b/modules/Bio/EnsEMBL/Hive/Worker.pm
index be4646259..a52bc1ee4 100644
--- a/modules/Bio/EnsEMBL/Hive/Worker.pm
+++ b/modules/Bio/EnsEMBL/Hive/Worker.pm
@@ -559,12 +559,10 @@ sub run {
             );
         }
 
-        # A mechanism whereby workers can be caused to exit even if they were doing fine:
-        #
-        # FIXME: The following check is not *completely* correct, as it assumes hive_capacity is "local" to the analysis:
+            # A mechanism whereby workers can be caused to exit even if they were doing fine:
         if (!$self->cause_of_death) {
-            my $stats = $self->analysis->stats;
-            if( defined($stats->hive_capacity) && (0 <= $stats->hive_capacity) && ($stats->hive_capacity < $stats->num_running_workers)
+            my $stats = $self->analysis->stats;     # make sure it is fresh from the DB
+            if( defined($stats->hive_capacity) && (0 <= $stats->hive_capacity) && ($self->adaptor->get_hive_current_load >= 1.1)
              or defined($self->analysis->analysis_capacity) && (0 <= $self->analysis->analysis_capacity) && ($self->analysis->analysis_capacity < $stats->num_running_workers)
             ) {
                 $self->cause_of_death('HIVE_OVERLOAD');
@@ -572,10 +570,13 @@ sub run {
         }
 
         if( $self->cause_of_death() =~ /^(NO_WORK|HIVE_OVERLOAD)$/ ) {
-            $self->adaptor->db->get_AnalysisStatsAdaptor->update_status($self->analysis_id, 'ALL_CLAIMED');
+            if( $self->cause_of_death() eq 'NO_WORK') {
+                $self->adaptor->db->get_AnalysisStatsAdaptor->update_status($self->analysis_id, 'ALL_CLAIMED');
+            }
             
             if( $self->can_respecialize and !$specialization_arglist ) {
                 $self->cause_of_death(undef);
+                $self->adaptor->db->get_AnalysisStatsAdaptor->decrease_running_workers($self->analysis->dbID);  # FIXME: tidy up this counting of active roles
                 $self->specialize_and_compile_wrapper();
             }
         }
-- 
GitLab