From ce82bcf5cd6f2f360d3b8f915c6df443237f7590 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 2 Mar 2021 11:22:29 +0100 Subject: [PATCH] Bug 27756: Fork background jobs to prevent memory leak Signed-off-by: Martin Renvoize --- misc/background_jobs_worker.pl | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/misc/background_jobs_worker.pl b/misc/background_jobs_worker.pl index 0520189fa5..5d409cdf1b 100755 --- a/misc/background_jobs_worker.pl +++ b/misc/background_jobs_worker.pl @@ -51,16 +51,32 @@ while (1) { # FIXME This means we need to have create the DB entry before # It could work in a first step, but then we will want to handle job that will be created from the message received my $job = Koha::BackgroundJobs->find($args->{job_id}); - my $success = $job->process( $args ); - $conn->ack( { frame => $frame } ); # FIXME depending on $success? + process_job( $job, $args ); + $conn->ack( { frame => $frame } ); # FIXME depending on success? + } else { my $jobs = Koha::BackgroundJobs->search({ status => 'new' }); while ( my $job = $jobs->next ) { my $args = decode_json($job->data); - $job->process( { job_id => $job->id, %$args } ); + process_job( $job, { job_id => $job->id, %$args } ); } sleep 10; } } $conn->disconnect; + +sub process_job { + my ( $job, $args ) = @_; + + my $pid; + if ( $pid = fork ) { + wait; + return; + } + + die "fork failed!" unless defined $pid; + + $job->process( $args ); + exit; +} -- 2.20.1