| 1 |
package Test2::Harness::Runner::Resource::SharedJobSlots::Config; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use YAML::Tiny; |
| 8 |
use Sys::Hostname qw/hostname/; |
| 9 |
use App::Yath::Util qw/find_in_updir/; |
| 10 |
|
| 11 |
use Test2::Harness::Util::HashBase qw{ |
| 12 |
<config_file |
| 13 |
<config_raw |
| 14 |
|
| 15 |
<host |
| 16 |
|
| 17 |
<common_conf |
| 18 |
<host_conf |
| 19 |
|
| 20 |
+state_file |
| 21 |
+state_umask |
| 22 |
+algorithm |
| 23 |
+max_slots |
| 24 |
+max_slots_per_job |
| 25 |
+max_slots_per_run |
| 26 |
+min_slots_per_run |
| 27 |
+default_slots_per_job |
| 28 |
+default_slots_per_run |
| 29 |
+disabled |
| 30 |
}; |
| 31 |
|
| 32 |
sub find { |
| 33 |
my $class = shift; |
| 34 |
my (%opts) = @_; |
| 35 |
|
| 36 |
my $base_name = delete $opts{base_name}; |
| 37 |
my $settings = delete $opts{settings}; |
| 38 |
my $config_file = delete $opts{config_file}; |
| 39 |
|
| 40 |
unless ($config_file) { |
| 41 |
$base_name //= ($settings && $settings->check_prefix('runner')) ? $settings->runner->shared_jobs_config : '.sharedjobslots.yml'; |
| 42 |
$config_file = ($base_name =~ m{(/|\\)} || -e $base_name) ? $base_name : find_in_updir($base_name); |
| 43 |
} |
| 44 |
|
| 45 |
return unless $config_file && -e $config_file; |
| 46 |
|
| 47 |
return $class->new(%opts, config_file => $config_file); |
| 48 |
} |
| 49 |
|
| 50 |
sub init { |
| 51 |
my $self = shift; |
| 52 |
|
| 53 |
my $config_file = $self->{+CONFIG_FILE}; |
| 54 |
|
| 55 |
my $config = YAML::Tiny->read($config_file) or die "Could not read '$config_file'"; |
| 56 |
$config = $self->{+CONFIG_RAW} = $config->[0]; # First doc only |
| 57 |
|
| 58 |
my $host = $self->{+HOST} //= hostname(); |
| 59 |
|
| 60 |
# Normalize an empty host config section to a hashref |
| 61 |
$config->{$host} ||= {} if exists $config->{$host}; |
| 62 |
|
| 63 |
unless ($self->{+HOST_CONF} = $config->{$host}) { |
| 64 |
if ($self->{+HOST_CONF} = $config->{DEFAULT}) { |
| 65 |
$self->{+HOST} = 'DEFAULT'; |
| 66 |
} |
| 67 |
else { |
| 68 |
die "Could not find '$host' or 'DEFAULT' settings in '$config_file'.\n"; |
| 69 |
} |
| 70 |
|
| 71 |
warn <<" EOT" unless $self->{+HOST_CONF}->{no_warning}; |
| 72 |
Using the 'DEFAULT' shared-slots host config. |
| 73 |
You may want to add the current host to the config file. |
| 74 |
To silence this warning, set the 'no_warning' key to true in the DEFAULT host config. |
| 75 |
Config File: $config_file |
| 76 |
Current Host: $host |
| 77 |
EOT |
| 78 |
} |
| 79 |
|
| 80 |
if ($self->{+HOST_CONF}->{use_common} //= 1) { |
| 81 |
$self->{+COMMON_CONF} = $config->{'COMMON'} // {}; |
| 82 |
} |
| 83 |
|
| 84 |
$self->{+COMMON_CONF} //= {}; |
| 85 |
|
| 86 |
#sanity check |
| 87 |
$self->max_slots; |
| 88 |
|
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
sub state_umask { $_[0]->{+STATE_UMASK} //= $_[0]->_get_config_option(+STATE_UMASK, default => 0007) } |
| 93 |
sub state_file { $_[0]->{+STATE_FILE} //= $_[0]->_get_config_option(+STATE_FILE, require => 1) } |
| 94 |
sub max_slots { $_[0]->{+MAX_SLOTS} //= $_[0]->_get_config_option(+MAX_SLOTS, required => 1) } |
| 95 |
sub min_slots_per_run { $_[0]->{+MIN_SLOTS_PER_RUN} //= $_[0]->_get_config_option(+MIN_SLOTS_PER_RUN, default => 0) } |
| 96 |
sub max_slots_per_job { $_[0]->{+MAX_SLOTS_PER_JOB} //= $_[0]->_get_config_option(+MAX_SLOTS_PER_JOB, default => $_[0]->max_slots) } |
| 97 |
sub max_slots_per_run { $_[0]->{+MAX_SLOTS_PER_RUN} //= $_[0]->_get_config_option(+MAX_SLOTS_PER_RUN, default => $_[0]->max_slots) } |
| 98 |
sub default_slots_per_job { $_[0]->{+DEFAULT_SLOTS_PER_JOB} //= $_[0]->_get_config_option(+DEFAULT_SLOTS_PER_JOB, default => $_[0]->max_slots_per_job) } |
| 99 |
sub default_slots_per_run { $_[0]->{+DEFAULT_SLOTS_PER_RUN} //= $_[0]->_get_config_option(+DEFAULT_SLOTS_PER_RUN, default => $_[0]->max_slots_per_run) } |
| 100 |
sub disabled { $_[0]->{+DISABLED} //= $_[0]->_get_config_option(+DISABLED, default => 0) } |
| 101 |
|
| 102 |
sub _get_config_option { |
| 103 |
my $self = shift; |
| 104 |
my ($field, %opts) = @_; |
| 105 |
|
| 106 |
my $val = $self->{+HOST_CONF}->{$field} // $self->{+COMMON_CONF}->{$field} // $opts{default}; |
| 107 |
|
| 108 |
die "'$field' not set in '$self->{+CONFIG_FILE}' for host '$self->{+HOST}' or under 'COMMON' config.\n" |
| 109 |
if $opts{required} && !defined($val); |
| 110 |
|
| 111 |
return $val; |
| 112 |
} |
| 113 |
|
| 114 |
sub algorithm { |
| 115 |
my $self = shift; |
| 116 |
|
| 117 |
return $self->{+ALGORITHM} if $self->{+ALGORITHM}; |
| 118 |
|
| 119 |
my $algorithm = $self->_get_config_option(+ALGORITHM, default => 'fair'); |
| 120 |
|
| 121 |
if ($algorithm =~ m/^(.*)::([^:]+)$/) { |
| 122 |
my ($mod, $sub) = ($1, $2); |
| 123 |
require(mod2file($mod)); |
| 124 |
} |
| 125 |
else { |
| 126 |
require Test2::Harness::Runner::Resource::SharedJobSlots::State; |
| 127 |
|
| 128 |
my $short = $algorithm; |
| 129 |
$algorithm = "_redistribute_$algorithm"; |
| 130 |
|
| 131 |
die "'$short' is not a valid algorithm (in file '$self->{+CONFIG_FILE}' under host '$self->{+HOST}' key 'algorithm'). Must be 'fair', 'first', or a Fully::Qualified::Module::function_name." |
| 132 |
unless Test2::Harness::Runner::Resource::SharedJobSlots::State->can($algorithm); |
| 133 |
} |
| 134 |
|
| 135 |
return $self->{+ALGORITHM} = $algorithm; |
| 136 |
} |
| 137 |
|
| 138 |
1; |
| 139 |
|
| 140 |
__END__ |
| 141 |
|
| 142 |
=pod |
| 143 |
|
| 144 |
=encoding UTF-8 |
| 145 |
|
| 146 |
=head1 NAME |
| 147 |
|
| 148 |
Test2::Harness::Runner::Resource::SharedJobSlots::Config - Config for shared job slots |
| 149 |
|
| 150 |
=head1 SOURCE |
| 151 |
|
| 152 |
The source code repository for Test2-Harness can be found at |
| 153 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 154 |
|
| 155 |
=head1 MAINTAINERS |
| 156 |
|
| 157 |
=over 4 |
| 158 |
|
| 159 |
=item Chad Granum E<lt>[email protected]<gt> |
| 160 |
|
| 161 |
=back |
| 162 |
|
| 163 |
=head1 AUTHORS |
| 164 |
|
| 165 |
=over 4 |
| 166 |
|
| 167 |
=item Chad Granum E<lt>[email protected]<gt> |
| 168 |
|
| 169 |
=back |
| 170 |
|
| 171 |
=head1 COPYRIGHT |
| 172 |
|
| 173 |
Copyright 2022 Chad Granum E<lt>[email protected]<gt>. |
| 174 |
|
| 175 |
This program is free software; you can redistribute it and/or |
| 176 |
modify it under the same terms as Perl itself. |
| 177 |
|
| 178 |
See F<http://dev.perl.org/licenses/> |
| 179 |
|
| 180 |
=cut |