use File::Tree::Snapshot;
use System::Introspector::Gatherer;
use Object::Remote::Future qw( await_all );
-
use JSON::Diffable qw( encode_json );
+use System::Introspector::Logger qw( :log );
has config => (is => 'ro', required => 1);
sub sudo_user { $_[0]->config->sudo_user }
-sub _log { shift; printf "[%s] %s\n", scalar(localtime), join '', @_ }
-
sub gather {
my ($self, @groups) = @_;
- $self->_log('Start');
+ log_debug { "Starting to gather results" };
for my $group (@groups) {
my @waiting;
for my $host ($self->config->hosts) {
- $self->_log("Beginning to fetch group '$group' on '$host'");
+ log_trace { "Adding group '$group' on '$host' to waiting list" };
push @waiting, [$host, $self->fetch($host, $group)];
}
- $self->_log("Now waiting for results");
+ log_debug { sprintf("Waiting for results to become available for %i groups", scalar(@waiting)) };
for my $wait (@waiting) {
my ($host, @futures) = @$wait;
+
+ log_trace { "Waiting for futures on host '$host'" };
+
my @data = await_all @futures;
- $self->_log("Received all from group '$group' on '$host'");
+ log_trace { "Received all from group '$group' on '$host'" };
$self->_store($host, $group, +{ map %$_, @data });
}
- }
- $self->_log('Done');
+ }
+ log_debug { "Completed gathering results" };
return 1;
}
my (@sudo, @nosudo);
push(@{ $spec->{$_}{sudo} ? \@sudo : \@nosudo}, [$_, $spec->{$_}])
for sort keys %$spec;
- my @futures;
+ my @futures;
+ log_debug { sprintf("Fetching results for '%s': sudo:%i nosudo:%i", $host, scalar(@sudo), scalar(@nosudo)) };
if (@nosudo) {
- $self->_log("Without sudo: ", join ", ", map $_->[0], @nosudo);
+ log_trace { "Preparing to fetch without sudo: " . join ", ", map $_->[0], @nosudo };
my $proxy = $self->_create_gatherer(
host => $host,
introspectors => [@nosudo],
push @futures, $proxy->start::gather_all;
}
if (@sudo) {
- $self->_log("With sudo: ", join ", ", map $_->[0], @nosudo);
+ log_trace { "Preparing to fetch with sudo: ", join ", ", map $_->[0], @nosudo };
my $proxy = $self->_create_gatherer(
sudo => 1,
host => $host,
);
push @futures, $proxy->start::gather_all;
}
+
+ log_trace { sprintf("Fetching resulted in %i futures being created", scalar(@futures)) };
return @futures;
}
sub _store {
my ($self, $host, $group, $gathered) = @_;
- $self->_log("Storing data for group '$group' on '$host'");
+ log_debug { "Storing data for group '$group' on '$host'" };
my $storage = $self->storage($host, $group);
my $ok = eval {
my @files;
} split m{::}, $class;
my $fh = $storage->open('>:utf8', $file, mkpath => 1);
my $full_path = $storage->file($file);
- $self->_log("Writing $full_path");
+ log_trace { "Writing state to '$full_path'" };
print $fh encode_json($gathered->{$class});
push @files, $full_path;
}
$self->_cleanup($storage, [@files]);
- $self->_log("Committing");
+ log_trace { "Comitting stored data" };
$storage->commit;
};
unless ($ok) {
- $self->_log("Rolling back snapshot because of: ", $@ || 'unknown error');
+ log_error { "Rolling back snapshot because of: " . $@ || 'unknown error' };
$storage->rollback;
die $@;
}
my ($self, $storage, $known_files) = @_;
my %known = map { ($_ => 1) } @$known_files;
my @files = $storage->find_files('json');
+ log_debug { "Cleaning up" };
for my $file (@files) {
next if $known{$file};
- $self->_log("Removing $file");
+ log_trace { "Removing $file" };
unlink($file)
or die "Unable to remove '$file': $!\n";
}