Amazon-SQS-Client

 view release on metacpan or  search on metacpan

bin/QueueDaemon.pl  view on Meta::CPAN

      $logger->error( sprintf "message error...\n%s", ref $err ? Dumper($err) : $err );

      # exit immediately if BAD REQUEST or INTERNAL SERVER ERROR
      if ( $err && ref $err && $err->getStatusCode =~ /(?:400|500)/xsm ) {
        exit 1;
      }

      next
        if !$handler->get_message();

      # handle message disposition
      if ($err) {
        if ( $options{'delete-when'} =~ /(?:error|always)/xsm ) {
          $logger->info('deleting message');
          $handler->delete_message();
        }

        if ( $options{'exit-when'} =~ /(?:error|always)/xsm ) {
          $KEEP_GOING = $FALSE;
        }
      }
      else {
        if ( $options{'delete-when'} =~ /(?:false|always)/xsm ) {
          $logger->info('deleting message');
          $handler->delete_message();
        }

        if ( $options{'exit-when'} =~ /(?:always|false)/xsm ) {
          $KEEP_GOING = $FALSE;
        }
      }
    }
    else {
      $logger->info( sprintf 'message (%s) handled successfully', $handler->get_message_id );

      if ( $options{'delete-when'} =~ /(?:always|true)/xsm ) {
        $logger->info('deleting message');
        $handler->delete_message();
      }
    }

    if ( $options{'exit-when'} eq 'always' ) {
      $KEEP_GOING = $FALSE;
    }
  }

  return 0;
}

########################################################################
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;

bin/QueueDaemon.pl  view on Meta::CPAN

    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)
     --queue-interval     amount of time to sleep
 -p, --pidfile            fully qualified path of pid file, default: /var/run/QueueDaemon.pl.in
 -v, --visibility-timeout visibility timeout in seconds, default: 30
 -w, --wait-time          long polling wait time in seconds, default: 0

=head2 LICENSE

(c) Copyright 2024 TBC Development Group, LLC. All rights reserved.
This is free software and may be used or distributed under the same terms as Perl itself.

=head1 FEATURES

=over 5

=item * easy configuration using the command line options or a configuration file

=item * automatically create a queue if it doesn't exist

=item * long or short polling. Set --wait-time for long polling, --queue-interval for short polling

=item * configurable message disposition options for successful handling of messages and exceptions

=item * can be run as a daemon or in a terminal

=back

=head1 HINTS & TIPS

=head2 Quick Start

 QueueDaemon.pl --create-queue -q fooManQueue

=over 5 

=item 1. If the queue does not exist it will be created if you use the --create-queue option.

=item 2. If no logfile is given, log output will be sent to STDERR

=item 3. See L<Amazon::SQS::Config> regarding the available options in a config file.

=item 4. The default is to daemonize the script. Use --no-daemonize to run in a terminal.

=item 5. If you do not provide a handler on the command line or in
your .ini file the default handler will be used. The default hanlder will dump the
message to the log and delete the message.

=item 6. By default messages will only be deleted from the queue if your
handler returns a true value. If you want to delete messages which cannot be
decoded or when you handler returns a non-true value, set the
--delete-when or set 'delete' option in the [error] section of your .ini file.

=item 7. To exit the daemon when your handler returns a non-true value
set the --exit-when option to 'false' or in the [error] section of your .ini



( run in 1.141 second using v1.01-cache-2.11-cpan-5511b514fd6 )