App-FargateStack

 view release on metacpan or  search on metacpan

lib/App/FargateStack/AutoscalingConfig.pm  view on Meta::CPAN

package App::FargateStack::AutoscalingConfig;

use strict;
use warnings;

use App::FargateStack::Constants;
use App::FargateStack::Builder::Utils qw(dmp choose);

use Data::Dumper;
use English qw(no_match_vars);
use List::Util qw(none uniq);

__PACKAGE__->follow_best_practice;
__PACKAGE__->mk_accessors(
  qw(
    config
    cpu
    max_capacity
    metric
    min_capacity
    policy_arn
    policy_name
    requests
    scale_in_cooldown
    scale_out_cooldown
    scheduled_actions
    task_name
  )
);

use parent qw(Class::Accessor::Fast);

########################################################################
sub new {
########################################################################
  my ( $class, @args ) = @_;

  my $options = ref $args[0] ? $args[0] : {@args};

  die 'usage: App::FargateStack::AutoscalingConfig->new(config => autoscaling-config)'
    if !$options->{config};

  my $self = $class->SUPER::new($options);

  foreach (qw(cpu requests scale_in_cooldown scale_out_cooldown policy_name)) {
    next if !defined $self->get($_);

    $self->update( $_ => $self->get($_) );
  }

  $self->parse_metric();

  if ( $self->get_cpu || $self->get_requests ) {
    $self->parse_cooldown();

    $self->parse_scaling_capacity();
  }

  $self->parse_scheduled_actions();

  $self->sanity_check();

  return $self;
}

########################################################################
sub sanity_check {
########################################################################
  my ($self) = @_;

  die 'autoscaling: cpu & requests are mutually exclusive'
    if $self->get_cpu && $self->get_requests;

  return;
}

########################################################################
sub update {
########################################################################
  my ( $self, $key, $value ) = @_;

  $self->get_config->{$key} = $value;

  return $self->set( $key, $value );
}

########################################################################
sub has_scheduled_action {
########################################################################
  my ($self) = @_;

  return exists $self->get_config->{scheduled};
}

########################################################################
sub parse_scheduled_actions {
########################################################################
  my ($self) = @_;

  return
    if !$self->has_scheduled_action;

  my $scheduled = $self->get_config->{scheduled};



( run in 0.626 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )