Amazon-SQS-Client

 view release on metacpan or  search on metacpan

bin/QueueDaemon.pl  view on Meta::CPAN

########################################################################
sub init_logger {
########################################################################
  my ($options) = @_;

  my ( $logfile, $loglevel ) = @{$options}{qw(logfile loglevel)};

  $loglevel //= 'info';

  $loglevel = {
    error => $ERROR,
    debug => $DEBUG,
    trace => $TRACE,
    info  => $INFO,
    warn  => $WARN,
  }->{ lc $loglevel };

  $loglevel //= $INFO;

  $logfile //= 'stderr';

  my $log4perl_config;

  if ( !$logfile || $logfile =~ /(?:stderr|stdout)/xsmi ) {
    $log4perl_config    = sprintf $SCREEN_CONFIG, $logfile eq 'stdout' ? 0 : 1;
    $options->{logfile} = lc $logfile;
  }
  else {
    $log4perl_config = sprintf $log4perl_config, $logfile;
  }

  if ( Log::Log4perl->initialized() ) {
    my $logger = Log::Log4perl->get_logger;
    $logger->level($loglevel);
    return $logger;
  }

  Log::Log4perl->init( \$log4perl_config );

  my $logger = Log::Log4perl->get_logger;
  $logger->level($loglevel);

  return $logger;
}

########################################################################
sub setup_signal_handlers {
########################################################################
  my ( $options, $handler ) = @_;

  $SIG{HUP} = sub {
    print {*STDERR} "Caught SIGHUP:  re-reading config file.\n";

    $KEEP_GOING = $TRUE;

    my $config = load_config($options);

    ${$handler} = load_handler(
      options     => $options,
      logger      => ${$handler}->get_logger,
      credentials => ${$handler}->get_credentials,
      config      => $config,
    );

    init_logger($options);  # just reset loglevel (potentially)

    $RELOAD = $TRUE;
  };

  $SIG{INT} = sub {
    print {*STDERR} ("Caught SIGINT:  exiting gracefully\n");
    $KEEP_GOING = $FALSE;
  };

  $SIG{QUIT} = sub {
    print {*STDERR} ("Caught SIGQUIT:  exiting gracefully\n");
    $KEEP_GOING = $FALSE;
  };

  $SIG{TERM} = sub {
    print {*STDERR} ("Caught SIGTERM:  exiting gracefully\n");
    $KEEP_GOING = $FALSE;
  };

  return;
}

########################################################################
sub load_config {
########################################################################
  my ($options) = @_;

  return
    if !$options->{config};

  my $config = Amazon::SQS::Config->new( file => $options->{config} );

  $options->{loglevel} //= $config->get_log_level;

  $options->{logfile} //= $config->get_log_file;
  $options->{logfile} //= 'stderr';

  $options->{'delete-when'} //= $config->get_error_delete;

  $options->{'exit-when'} //= $config->get_error_exit;

  $options->{handler} //= $config->get_handler_class;

  $options->{'max-sleep-time'} //= $config->get_queue_max_wait;

  $options->{'max-messages'} //= $config->get_queue_max_messages // 1;

  $options->{queue} //= $config->get_queue_name;

  $options->{'queue-url'} //= $config->get_queue_url;

  $options->{'queue-interval'} //= $config->get_queue_interval;

  $options->{'create-queue'} //= $config->get_queue_create_queue // $FALSE;

  $options->{'visibility-timeout'} //= $config->get_queue_visibility_timeout;

  $options->{'wait-time'} //= $config->get_queue_wait_time;

  return $config;
}

########################################################################
sub load_handler {
########################################################################
  my %args = @_;

  my ( $config, $options, $logger, $credentials ) = @args{qw(config options logger credentials)};

  if ( Class::Inspector->loaded( $options->{handler} ) ) {
    Class::Unload->unload( $options->{handler} );
  }

  autoload $options->{handler};

  my $handler = $options->{handler}->new(
    config             => $config,
    logger             => $logger,
    endpoint_url       => $options->{endpoint_url},
    name               => $options->{queue},
    url                => $options->{'queue-url'},
    message_type       => $options->{'message-type'},
    create_queue       => $options->{'create-queue'},
    wait_time          => $options->{'wait-time'},
    visibility_timeout => $options->{'visibility-timeout'},
    credentials        => $credentials,
  );

  die "not an Amazon::SQS::QueueHandler\n"
    if !$handler->isa('Amazon::SQS::QueueHandler');

  return $handler;
}

########################################################################
sub sleep_time {
########################################################################
  my ( $sleep, $options ) = @_;

  $sleep //= 0;

  return $sleep + $options->{'queue-interval'};
}

exit main();

1;

__END__

=pod

=head1 NAME 

QueueDaemon.pl - wrapper for queue handler daemons

=head1 SYNOPSIS

 QueueDaemon.pl options

Read and process SQS messages.

=head1 DESCRIPTION

Implements a daemon that reads from Amazon's Simple Queue Service
(SQS).

=head1 OPTIONS

 -h, --help               help
 -c, --config             config file name
 -C, --create-queue       create the queue if it does not exist
 -d, --daemonize          daemonize the script (default)
     --no-daemonize       
 -D, --delete-when        never, always, error
 -E, --exit-when          never, always, error, false
 -e, --endpoint-url       default: https://sqs.amazonaws.com
 -L, --logfile            name of logfile
 -l, --loglevel           log level (trace, debug, info, warn, error)
 -H, --handler            name of the handler class, default: Amazon::SQS::QueueHandler
 -m, --max-children       not implemented (default: 1)
 -s, --max-sleep-time     default: 5 seconds
     --max-messages       fixed at 1 currently
 -M, --message-type       mime type of messages (text/plain, application/json, 
                          application/x-www-form-encoded), default: text/plain
 -q, --queue              queue name (not url)



( run in 0.778 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )