App-FargateStack
view release on metacpan or search on metacpan
lib/App/FargateStack/AutoscalingConfig.pm view on Meta::CPAN
my @metric = choose {
return ( 'cpu', int $config->{cpu} )
if defined $config->{cpu};
return ( 'requests', int $config->{requests} )
if defined $config->{requests};
# scheduled only?
return ( $config->{scheduled}, 0 )
if defined $config->{scheduled};
return;
};
die 'autoscaling: autoscaling type must be one of "cpu", "requests" or "scheduled"'
if !@metric;
return # scheduled
if !$metric[1];
$self->set_metric( $metric[0] );
$self->update( $metric[0] => $metric[1] );
return $self;
}
=pod
=head2 parse_scheduled_action
my $action_hash = $self->parse_scheduled_action( $scheduled_action_hash );
This is a private helper subroutine that acts as the primary parser and validator
for the user-defined schedule configuration. It takes a single, user-configured
schedule hash (e.g., the business_hours block) and translates it into a
complex data structure containing the two distinct, API-ready "ScaleOut" and
"ScaleIn" actions required by AWS.
The subroutine's main responsibilities are:
=over 4
=item *
Parsing: It parses the user-friendly start_time, end_time, days,
min_capacity, and max_capacity values. It supports flexible delimiters
for capacity pairs and both textual and numeric day representations.
=item *
Validation: It performs a series of critical, upfront validation checks.
If any part of the configuration is missing, malformed, or logically
inconsistent (e.g., a scale-in capacity is larger than a scale-out capacity),
the subroutine will abort the entire process with a clear, descriptive error
message.
=item *
Transformation: It transforms the validated user input into two separate,
syntactically correct AWS cron expressions (cron(...)) for the scale-out and
scale-in events.
=item *
Structuring: It builds and returns a nested hash that precisely mirrors
the data structure needed for the put-scheduled-action API calls, separating
the ScaleOut and ScaleIn actions, each with their own Schedule string and
Action hash containing the corresponding capacity limits.
=back
=head3 Arguments
=over 4
=item * C<$scheduled_action>
A hash reference corresponding to a single named schedule from the user's YAML
configuration. It is expected to contain the following keys:
=over 8
=item * C<start_time> (e.g., "09:00")
=item * C<end_time> (e.g., "17:00")
=item * C<days> (e.g., "MON-FRI", "2-6")
=item * C<min_capacity> (e.g., "2/1", "2-1")
=item * C<max_capacity> (e.g., "4/1", "4-1")
=back
=back
=head3 Returns
A hash reference containing two keys, C<ScaleOut> and C<ScaleIn>. Each of these
keys contains another hash with the fully-formed C<Action> and C<Schedule>
sub-hashes required for the final API calls.
=cut
########################################################################
sub _parse_scheduled_action {
########################################################################
my ( $self, $scheduled_action ) = @_;
my ( $start_time, $end_time, $days ) = @{$scheduled_action}{qw(start_time end_time days)};
die 'autoscaling: you must include start_time, end_time, and days for a scheduled action'
if !$start_time || !$end_time || !$days;
my (@start_times) = split /:/xsm, $start_time;
my (@end_times) = split /:/xsm, $end_time;
die sprintf 'autoscaling: invalid start time: [%s]', $start_time
( run in 0.572 second using v1.01-cache-2.11-cpan-39bf76dae61 )