diff options
author | Matthew Somerville <matthew@mysociety.org> | 2020-03-11 13:41:19 +0000 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2020-04-02 17:14:21 +0100 |
commit | 592d52838f3d1cd1824f6a37e273705112a7011a (patch) | |
tree | 26090fd07dd9177a26135fa2875d66567059fbd3 /perllib/Open311/GetServiceRequestUpdates.pm | |
parent | 46300feaf7481ef4dac67ee312becf856c184135 (diff) |
Allow slow processes to spin up new ones.
Diffstat (limited to 'perllib/Open311/GetServiceRequestUpdates.pm')
-rw-r--r-- | perllib/Open311/GetServiceRequestUpdates.pm | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm index e27a08068..09b1f6b26 100644 --- a/perllib/Open311/GetServiceRequestUpdates.pm +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -38,8 +38,26 @@ sub fetch { $bodies = $bodies->search( { name => $self->body } ); } - my $procs = FixMyStreet->config('FETCH_COMMENTS_PROCESSES') || 0; - my $pm = Parallel::ForkManager->new(FixMyStreet->test_mode ? 0 : $procs); + my $procs_min = FixMyStreet->config('FETCH_COMMENTS_PROCESSES_MIN') || 0; + my $procs_max = FixMyStreet->config('FETCH_COMMENTS_PROCESSES_MAX'); + my $procs_timeout = FixMyStreet->config('FETCH_COMMENTS_PROCESS_TIMEOUT'); + + my $pm = Parallel::ForkManager->new(FixMyStreet->test_mode ? 0 : $procs_min); + + if ($procs_max && $procs_timeout) { + my %workers; + $pm->run_on_wait(sub { + while (my ($pid, $started_at) = each %workers) { + next unless time() - $started_at > $procs_timeout; + next if $pm->max_procs == $procs_max; + $pm->set_max_procs($pm->max_procs + 1); + delete $workers{$pid}; # Only want to increase once per long-running thing + } + }, 1); + $pm->run_on_start(sub { my $pid = shift; $workers{$pid} = time(); }); + $pm->run_on_finish(sub { my $pid = shift; delete $workers{$pid}; }); + } + while ( my $body = $bodies->next ) { $pm->start and next; |