/usr/share/perl5/FCM/Admin/Runner.pm is in fcm 2016.12.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | # ------------------------------------------------------------------------------
# (C) British Crown Copyright 2006-16 Met Office.
#
# This file is part of FCM, tools for managing and building source code.
#
# FCM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FCM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FCM. If not, see <http://www.gnu.org/licenses/>.
# ------------------------------------------------------------------------------
use strict;
use warnings;
package FCM::Admin::Runner;
use IO::Handle;
use POSIX qw{strftime};
# The default values of the attributes
my %DEFAULT = (
exceptions => [],
max_attempts => 3,
retry_interval => 5,
stderr_handle => \*STDERR,
stdout_handle => \*STDOUT,
);
my $INSTANCE;
# ------------------------------------------------------------------------------
# Returns a unique instance of this class. Creates the instance on first call.
sub instance {
my ($class) = @_;
if (!defined($INSTANCE)) {
$INSTANCE = bless({%DEFAULT}, $class);
}
return $INSTANCE;
}
# ------------------------------------------------------------------------------
# Adds a new exception to the list of exceptions.
sub _add_exception {
my ($self, $exception) = @_;
push(@{$self->get_exceptions()}, $exception);
}
# ------------------------------------------------------------------------------
# Returns the list of exceptions (or a reference to the list in scalar context).
sub get_exceptions {
my ($self) = @_;
return (wantarray() ? @{$self->{exceptions}} : $self->{exceptions});
}
# ------------------------------------------------------------------------------
# Returns the latest exception in the exception list.
sub get_latest_exception {
my ($self) = @_;
if (exists($self->get_exceptions()->[-1])) {
return $self->get_exceptions()->[-1];
}
else {
return;
}
}
# ------------------------------------------------------------------------------
# Returns the maximum number of attempts for the "run_with_retries" method.
sub get_max_attempts {
my ($self) = @_;
return $self->{max_attempts};
}
# ------------------------------------------------------------------------------
# Returns the retry interval for the "run_with_retries" method.
sub get_retry_interval {
my ($self) = @_;
return $self->{retry_interval};
}
# ------------------------------------------------------------------------------
# Returns the file handle for STDERR.
sub get_stderr_handle {
my ($self) = @_;
if (!IO::Handle::opened($self->{stderr_handle})) {
$self->{stderr_handle} = $DEFAULT{stderr_handle};
}
return $self->{stderr_handle};
}
# ------------------------------------------------------------------------------
# Returns the file handle for STDOUT.
sub get_stdout_handle {
my ($self) = @_;
if (!IO::Handle::opened($self->{stdout_handle})) {
$self->{stdout_handle} = $DEFAULT{stdout_handle};
}
return $self->{stdout_handle};
}
# ------------------------------------------------------------------------------
# Runs $sub_ref->(@arguments) with a diagnostic $message. Dies on error.
sub run {
my ($self, $message, $sub_ref, @arguments) = @_;
printf(
{$self->get_stdout_handle()}
qq{%s: %s\n}, strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()), $message,
);
eval {
if (!$sub_ref->(@arguments)) {
die(qq{\n});
}
};
if ($@) {
my $e = $@;
chomp($e);
my $exception
= sprintf(qq{ERROR %s%s\n}, $message, ($e ? qq{ - $e} : qq{}));
$self->_add_exception($exception);
die($exception);
}
return 1;
}
# ------------------------------------------------------------------------------
# Runs $sub_ref->(@arguments) with a diagnostic $message. Warns on error.
sub run_continue {
my ($self, $message, $sub_ref, @arguments) = @_;
my $rc;
eval {
$rc = $self->run($message, $sub_ref, @arguments);
};
if ($@) {
print({$self->get_stderr_handle()} $@);
return;
}
return $rc;
}
# ------------------------------------------------------------------------------
# Runs $sub_ref->(@arguments) with a diagnostic $message. Retries on error.
sub run_with_retries {
my ($self, $message, $sub_ref, @arguments) = @_;
for my $i_attempt (1 .. $self->get_max_attempts()) {
my $attempt_message = sprintf(
qq{%s, attempt %d of %d},
$message, $i_attempt, $self->get_max_attempts(),
);
if ($i_attempt == $self->get_max_attempts()) {
return $self->run($attempt_message, $sub_ref, @arguments);
}
else {
if ($self->run_continue($attempt_message, $sub_ref, @arguments)) {
return 1;
}
sleep($self->get_retry_interval());
}
}
}
# ------------------------------------------------------------------------------
# Sets the maximum number of attempts for the "run_with_retries" method.
sub set_max_attempts {
my ($self, $value) = @_;
$self->{max_attempts} = $value;
}
# ------------------------------------------------------------------------------
# Sets the retry interval for the "run_with_retries" method.
sub set_retry_interval {
my ($self, $value) = @_;
$self->{retry_interval} = $value;
}
# ------------------------------------------------------------------------------
# Sets the file handle for STDERR.
sub set_stderr_handle {
my ($self, $value) = @_;
if (defined($value) && IO::Handle::opened($value)) {
$self->{stderr_handle} = $value;
}
}
# ------------------------------------------------------------------------------
# Sets the file handle for STDOUT.
sub set_stdout_handle {
my ($self, $value) = @_;
if (defined($value) && IO::Handle::opened($value)) {
$self->{stdout_handle} = $value;
}
}
1;
__END__
=head1 NAME
FCM::Admin::Runner
=head1 SYNOPSIS
$runner = FCM::Admin::Runner->instance();
$runner->run($message, sub { ... });
=head1 DESCRIPTION
Provides a simple way to run a piece of code with a time-stamped diagnostic
message.
=head1 METHODS
=over 4
=item FCM::Admin::Runner->instance()
Returns a unique instance of FCM::Admin::Runner.
=item $runner->get_exceptions()
Returns a list containing all the exceptions captured by the previous
invocations of the $runner->run() method. In SCALAR context, returns a reference
to the list.
=item $runner->get_latest_exception()
Returns the latest exception captured by the $runner->run() method. Returns
undef if there is no captured exception in the list.
=item $runner->get_max_attempts()
Returns the number of maximum retries for the
$runner->run_with_retries($message,$sub_ref,@arguments) method. (Default: 3)
=item $runner->get_retry_interval()
Returns the interval (in seconds) between retries for the
$runner->run_with_retries($message,$sub_ref,@arguments) method. (Default: 5)
=item $runner->get_stderr_handle()
Returns the file handle for standard error output. (Default: \*STDERR)
=item $runner->get_stdout_handle()
Returns the file handle for standard output. (Default: \*STDOUT)
=item $runner->run($message,$sub_ref,@arguments)
Prints the diagnostic $message and runs $sub_ref (with extra @arguments).
Returns true if $sub_ref returns true. die() with a message that looks like
"ERROR $message\n" if $sub_ref returns false or die().
=item $runner->run_continue($message,$sub_ref,@arguments)
Same as $runner->run($message,$sub_ref,@arguments), but only issue a warning
(and returns false) if $sub_ref returns false or die().
=item $runner->run_with_retries($message,$sub_ref,@arguments)
Attempts $runner->run($message,$sub_ref,@arguments) for a number of times up to
$runner->get_max_attempts(), with a delay of $runner->get_retry_interval()
between each attempt. die() if $sub_ref still returns false in the final
attempt. Returns true on success.
=item $runner->set_max_attempts($value)
Sets the maximum number of attempts in the
$runner->run_with_retries($message,$sub_ref,@arguments) method.
=item $runner->set_retry_interval($value)
Sets the interval (in seconds) between retries for the
$runner->run_with_retries($message,$sub_ref,@arguments) method.
=item $runner->set_stderr_handle($value)
Sets the file handle for standard error output to an alternate file handle. The
$value must be a valid file descriptor.
=item $runner->set_stdout_handle($value)
Sets the file handle for standard output to an alternate file handle. The $value
must be a valid file descriptor.
=back
=head1 COPYRIGHT
E<169> Crown copyright Met Office. All rights reserved.
=cut
|