Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions lib/Rex/Fork/Manager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@ sub new {
}

sub add {
my ( $self, $coderef, $task, $server ) = @_;
my $f = Rex::Fork::Task->new(
coderef => $coderef,
task => $task,
server => $server,
);
my ( $self, $coderef ) = @_;

my $f = Rex::Fork::Task->new( coderef => $coderef );

push( @{ $self->{'forks'} }, $f );

Expand Down
21 changes: 4 additions & 17 deletions lib/Rex/Fork/Task.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ use POSIX ":sys_wait_h";

# VERSION

BEGIN {
use Rex::Shared::Var;
share qw(@SUMMARY);
}

sub new {
my $that = shift;
my $proto = ref($that) || $that;
Expand All @@ -31,20 +26,12 @@ sub new {

sub start {
my ($self) = @_;

$self->{'running'} = 1;
if ( $self->{pid} = fork ) { return $self->{pid}; }
else {
$self->{chld} = 1;

eval { $self->{coderef}->($self) };
my $exit_code = $@ ? ( ( $? >> 8 ) || 1 ) : 0;
push @SUMMARY,
{
task => $self->{task}->name,
server => $self->{server},
exit_code => $exit_code,
};
$self->{pid} = fork;

if ( !$self->{pid} ) {
$self->{coderef}->($self);
$self->{'running'} = 0;
exit();
}
Expand Down
70 changes: 47 additions & 23 deletions lib/Rex/TaskList/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ use warnings;

# VERSION

BEGIN {
use Rex::Shared::Var;
share qw(@SUMMARY);
}

use Data::Dumper;
use Rex::Logger;
use Rex::Task;
Expand Down Expand Up @@ -290,39 +295,21 @@ sub run {
my ( $self, $task, %options ) = @_;

my $fm = Rex::Fork::Manager->new( max => $self->get_thread_count($task) );
my $task_name = $task->name;
my $all_servers = $task->server;

for my $server (@$all_servers) {
my $forked_sub = sub {
Rex::Logger::init();
Rex::Logger::info("Running task $task_name on $server");

my $run_task = $task->clone;
my $return_value = $run_task->run(
$server,
in_transaction => $self->{IN_TRANSACTION},
params => $options{params},
args => $options{args},
);

Rex::Logger::debug("Destroying all cached os information");
Rex::Logger::shutdown();

return $return_value;
};
my $child_coderef = $self->build_child_coderef($task, $server, %options);

if ( $self->{IN_TRANSACTION} ) {

# Inside a transaction -- no forking and no chance to get zombies.
# This only happens if someone calls do_task() from inside a transaction.
# Note the result is not appended to @SUMMARY.
$forked_sub->();
$child_coderef->();
}
else {
# Not inside a transaction, so lets fork
# Add $forked_sub to the fork queue
$fm->add( $forked_sub, $task, $server->to_s );
$fm->add($child_coderef);
}
}

Expand All @@ -333,6 +320,43 @@ sub run {
return $ret;
}

sub build_child_coderef {
my ($self, $task, $server, %options) = @_;

return sub {
Rex::Logger::init();
Rex::Logger::info("Running task " . $task->name . " on $server");

my $return_value = eval {
$task->clone->run(
$server,
in_transaction => $self->{IN_TRANSACTION},
params => $options{params},
args => $options{args},
);
};

if ($self->{IN_TRANSACTION}) {
die $@ if $@;
}
else {
my $exit_code = $@ ? ( ( $? >> 8 ) || 1 ) : 0;

push @SUMMARY, {
task => $task->name,
server => $server->to_s,
exit_code => $exit_code,
};
}

Rex::Logger::debug("Destroying all cached os information");
Rex::Logger::shutdown();


return $return_value;
};
}

sub modify {
my ( $self, $type, $task, $code, $package, $file, $line ) = @_;

Expand Down Expand Up @@ -389,7 +413,7 @@ sub is_transaction {

sub get_exit_codes {
my ($self) = @_;
return map { $_->{exit_code} } @Rex::Fork::Task::SUMMARY;
return map { $_->{exit_code} } @SUMMARY;
}

sub get_thread_count {
Expand All @@ -409,6 +433,6 @@ sub get_thread_count {
return 1;
}

sub get_summary { @Rex::Fork::Task::SUMMARY }
sub get_summary { @SUMMARY }

1;
65 changes: 4 additions & 61 deletions lib/Rex/TaskList/Parallel_ForkManager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,16 @@ use Rex::Report;
use Time::HiRes qw(time);

BEGIN {
use Rex::Shared::Var;
share qw(@SUMMARY);

use Rex::Require;
Parallel::ForkManager->require;
}

use base qw(Rex::TaskList::Base);

sub new {
my $that = shift;
my $proto = ref($that) || $that;
my $self = $proto->SUPER::new(@_);

bless( $self, $proto );

return $self;
}

sub run {
my ( $self, $task, %options ) = @_;

my $fm = Parallel::ForkManager->new( $self->get_thread_count($task) );
my $task_name = $task->name;
my $fm = Parallel::ForkManager->new( $self->get_thread_count($task) );
my $all_servers = $task->server;

$fm->run_on_finish(
Expand All @@ -55,44 +41,18 @@ sub run {
);

for my $server (@$all_servers) {
my $forked_sub = sub {
Rex::Logger::init();
Rex::Logger::info("Running task $task_name on $server");

my $run_task = $task->clone;
my $return_value = $run_task->run(
$server,
in_transaction => $self->{IN_TRANSACTION},
params => $options{params},
args => $options{args},
);

Rex::Logger::debug("Destroying all cached os information");
Rex::Logger::shutdown();

return $return_value;
};
my $child_coderef = $self->build_child_coderef($task, $server, %options);

if ( $self->{IN_TRANSACTION} ) {

# Inside a transaction -- no forking and no chance to get zombies.
# This only happens if someone calls do_task() from inside a transaction.
# Note the result is not appended to @SUMMARY.
$forked_sub->();
$child_coderef->();
}
else {
# Not inside a transaction, so lets fork
$fm->start and next;

eval { $forked_sub->() };
my $exit_code = $@ ? ( ( $? >> 8 ) || 1 ) : 0;
push @SUMMARY,
{
task => $task_name,
server => $server->to_s,
exit_code => $exit_code,
};

$child_coderef->();
$fm->finish;
}
}
Expand All @@ -104,21 +64,4 @@ sub run {
return $ret;
}

sub get_exit_codes {
my ($self) = @_;
return map { $_->{exit_code} } @SUMMARY;
}

sub get_summary { @SUMMARY }

sub set_in_transaction {
my ( $self, $val ) = @_;
$self->{IN_TRANSACTION} = $val;
}

sub is_transaction {
my ($self) = @_;
return $self->{IN_TRANSACTION};
}

1;
2 changes: 2 additions & 0 deletions lib/Rex/Transaction.pm
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ sub transaction(&) {
Rex::pop_connection();
}

Rex::TaskList->create()->set_in_transaction(0);

die("Transaction failed. Rollback done.");
}

Expand Down
15 changes: 4 additions & 11 deletions t/summary.t
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,19 @@ sub create_tasks {

sub test_summary {
my (%expected) = @_;
my @expected_summary;

$Rex::TaskList::task_list = undef;

create_tasks();

my @summary;
my @expected_summary;
my $test_description;

for my $task_name ( Rex::TaskList->create->get_tasks ) {
Rex::TaskList->run($task_name);
@summary = Rex::TaskList->create->get_summary;
my @summary = Rex::TaskList->create->get_summary;

push @expected_summary, $expected{$task_name};

$test_description =
my $test_description =
$expected{$task_name}->{exit_code} == 0
? "$task_name succeeded"
: "$task_name failed";
Expand All @@ -119,11 +116,7 @@ sub test_summary {
my $distributor = Rex::Config->get_distributor;
no warnings;

@Rex::Fork::Task::SUMMARY = ()
if $distributor eq 'Base';

@Rex::TaskList::Parallel_ForkManager::SUMMARY = ()
if $distributor eq 'Parallel_ForkManager';
@Rex::TaskList::Base::SUMMARY = ();
}

sub parallel_forkmanager_not_installed {
Expand Down