App-Actions

 view release on metacpan or  search on metacpan

lib/App/Actions/Runner/Parallel.pm  view on Meta::CPAN

# Entry:       $entry.
# Exit status: Returns the child's exit status: 0 when it wrote a result.
# Side effects: Redirects STDOUT and STDERR; never returns to the parent's
#              code (the caller exits with the status).
sub _child {
	my ($self, $entry) = @_;

	# autodie would die here, and a die in the child would carry on running
	# the parent's code; a child that cannot write its log just exits.
	{
		no autodie qw(open);
		open(STDOUT, '>', $entry->{log}) or POSIX::_exit($EX_SOFTWARE);
		open(STDERR, '>&', \*STDOUT) or POSIX::_exit($EX_SOFTWARE);
	}

	# Buffered writes would sit in this child until it exited, and the
	# parent would have nothing to show while the job works.
	STDOUT->autoflush(1);
	STDERR->autoflush(1);

	# A plain print goes to the selected handle, which the caller may have
	# pointed elsewhere; the job's output belongs in its log.
	select STDOUT;

	return $self->_run_job($entry);
}

# _run_job
# Purpose:     Run the job and write its result where the parent will read it.
# Entry:       $entry.
# Exit status: Returns 0 when a result was written, 1 otherwise.
# Side effects: Runs the job; writes the result file; prints an error.
sub _run_job {
	my ($self, $entry) = @_;

	my $ok = eval {
		$self->_write_result($entry->{result_path}, $self->{run_job}->($entry));
		1;
	};
	if (!$ok) {
		my $error = chomp_error($@ || $self->i18n('runner.parallel.did_not_run', { name => $entry->{plan}{display_name} }));
		$self->{logger}->error($self->i18n('runner.error', { error => $error }));
	}

	STDOUT->flush;
	STDERR->flush;

	return $ok ? 0 : 1;
}

# _run_here
# Purpose:     Run a job in this process, for a host where fork() failed,
#              with its output redirected to its log as a child's would be.
# Entry:       $entry.
# Exit status: Returns nothing.
# Side effects: Redirects and restores STDOUT and STDERR; runs the job;
#              collects its result.
sub _run_here {
	my ($self, $entry) = @_;

	my ($saved_stdout, $saved_stderr);
	{
		no autodie qw(open);
		open($saved_stdout, '>&', \*STDOUT) or $self->throw('runner.parallel.dup_failed', { handle => 'STDOUT' });
		open($saved_stderr, '>&', \*STDERR) or $self->throw('runner.parallel.dup_failed', { handle => 'STDERR' });
		open(STDOUT, '>', $entry->{log}) or $self->throw('runner.file_write_failed', { file => $entry->{log}, error => "$!" });
		open(STDERR, '>&', \*STDOUT) or $self->throw('runner.parallel.dup_failed', { handle => 'STDOUT' });
	}
	STDOUT->autoflush(1);
	STDERR->autoflush(1);

	# As in a child, the job's plain prints go to its log whatever handle
	# the caller had selected; the caller's choice is put back afterwards.
	my $selected = select STDOUT;
	$self->_run_job($entry);
	select $selected;

	{
		no autodie qw(open);
		open(STDOUT, '>&', $saved_stdout) or $self->throw('runner.parallel.restore_failed', { handle => 'STDOUT' });
		open(STDERR, '>&', $saved_stderr) or $self->throw('runner.parallel.restore_failed', { handle => 'STDERR' });
	}

	$self->_collect($entry);

	return;
}

# _stream
# Purpose:     Show the part of a job's log not shown yet, a line at a time,
#              with the job named on each line.  A partial last line waits
#              for the rest of it.
# Entry:       $entry.
# Exit status: Returns nothing.
# Side effects: Prints; advances the entry's shown offset and pending text.
sub _stream {
	my ($self, $entry) = @_;

	return if $self->{quiet};

	my $chunk = _read_from($entry->{log}, $entry->{shown});
	return unless defined $chunk && length $chunk;

	$entry->{shown} += length $chunk;
	$entry->{pending} .= $chunk;

	# The complete lines are split off in one pass; the unfinished last one
	# waits for the rest of it.  Cutting one line at a time off the front of
	# the buffer, while (s/\A([^\n]*)\n//), copies what is left every time,
	# which is quadratic: a 200,000-line chunk took sixteen seconds.
	my @lines = split /\n/, $entry->{pending}, -1;
	my $rest = pop @lines;
	$entry->{pending} = defined $rest ? $rest : q{};

	my $prefix = _prefix($entry);
	for my $line (@lines) {
		$line =~ s/\r\z//;

		# A blank line stays blank: a prefix on it would be noise rather
		# than a name for something the job said.
		print length $line ? "$prefix$line\n" : "\n";
	}
	STDOUT->flush;

	return;
}

# _collect
# Purpose:     Read the result a finished job left, after showing the rest of
#              its log, including a last line without a newline.
# Entry:       $entry.
# Exit status: Returns nothing.
# Side effects: Prints; sets the entry's result; calls on_result.
sub _collect {
	my ($self, $entry) = @_;

	$self->_stream($entry);
	if (!$self->{quiet} && length $entry->{pending}) {
		(my $last = $entry->{pending}) =~ s/\r\z//;
		$entry->{pending} = '';
		print _prefix($entry), $last, "\n";



( run in 1.230 second using v1.01-cache-2.11-cpan-60884a8df76 )