Skip to content

Commit

Permalink
do not cache task objects
Browse files Browse the repository at this point in the history
Caching the task object means it will survive long enough for Minion to
clean it up entirely using `POSIX::_exit(0)`. Since this is an abrupt,
immediate exit, things are not able to be cleaned up (like database
connections, resulting in spurious log messages).

Committed from Perl Toolchain Summit 2018. Thanks @toddr and @atoomic
for helping to debug this issue!
  • Loading branch information
preaction committed Apr 19, 2018
1 parent 549defa commit ce8f83e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/Beam/Minion/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ sub build_mojo_app {
$minion->add_task( "$container_name:$service_name" => sub {
my ( $job, @args ) = @_;

my $obj = eval { $wire->get( $service_name ) };
my $obj = eval { $wire->get( $service_name, lifecycle => 'factory' ) };
if ( $@ ) {
return $job->fail( { error => $@ } );
}

my $exit = eval { $obj->run( @args ) };
if ( $@ ) {
return $job->fail( { error => $@ } );
if ( my $err = $@ ) {
return $job->fail( { error => $err } );
}

my $method = $exit ? 'fail' : 'finish';
Expand Down
7 changes: 7 additions & 0 deletions t/command/worker.t
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,11 @@ subtest 'BEAM_MINION must be set' => sub {
'BEAM_MINION missing raises exception';
};

subtest 'test that object is destroyed' => sub {
my $id = $minion->enqueue( 'container:success', [] );
my $job = $minion->job( $id );
$job->execute;
is $Local::Service::DESTROYED, 1, 'DESTROY was called';
};

done_testing;
6 changes: 6 additions & 0 deletions t/lib/Local/Service.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ sub run {
return $self->exit_code;
}

our $DESTROYED = 0;
sub DESTROY {
$DESTROYED++;
}

1;

0 comments on commit ce8f83e

Please sign in to comment.