$loop->loop_once until $_[1];
}
+sub await_all {
+ my ($class, @requests) = @_;
+ @requests = grep !$_->is_done, @requests;
+ return unless @requests;
+ my %req = map +("$_" => "$_"), @requests;
+ my $done;
+ my %orig;
+ my %on_r = map {
+ my $orig = $orig{$_} = $_->{on_result};
+ my $tag = $req{$_};
+ ($_ => sub { delete $req{$tag}; $orig->(@_); $done = 1 unless keys %req; })
+ } @requests;
+ $_->{on_result} = $on_r{$_} for @requests;
+ $class->loop_until($done);
+ $_->{on_result} = $orig{$_} for @requests;
+ return;
+}
+
1;
has on_result => (is => 'ro', required => 1);
+has is_done => (is => 'rw', default => sub { 0 });
+
sub progress {
my ($self, @report) = @_;
if (my $cb = $self->on_progress) {
sub result {
my ($self, $type, @data) = @_;
+ $self->is_done(1);
$self->on_result->(Tak::Result->new(type => $type, data => \@data));
}
return $self->_run($cmd, $code, @argv);
} elsif ($code = $self->can("each_$cmd")) {
return $self->_run_each($cmd, $code, @argv);
+ } elsif ($code = $self->can("every_$cmd")) {
+ return $self->_run_every($cmd, $code, @argv);
}
$self->stderr->print("No such command: ${cmd}\n");
return $self->local_help;
return;
}
my $opt = $self->_maybe_parse_options($code, \@argv);
- if (my $prepare = $self->can("prepare_$cmd")) {
- $self->$prepare($opt, @argv);
- }
$self->local_client->ensure(connector => 'Tak::ConnectorService');
foreach my $target (@targets) {
my $remote = $self->_connection_to($target);
}
}
+sub _run_every {
+ my ($self, $cmd, $code, @argv) = @_;
+ my @targets = $self->_host_list_for($cmd);
+ unless (@targets) {
+ $self->stderr->print("No targets for ${cmd}\n");
+ return;
+ }
+ my $opt = $self->_maybe_parse_options($code, \@argv);
+ $self->local_client->ensure(connector => 'Tak::ConnectorService');
+ my @remotes = map $self->_connection_to($_), @targets;
+ $self->$code(\@remotes, $opt, @argv);
+}
+
sub _host_list_for {
my ($self, $command) = @_;
my @host_spec = map split(' ', $_), @{$self->options->{host}};