App-EventStreamr

 view release on metacpan or  search on metacpan

bin/eventstreamr  view on Meta::CPAN

my $DEBUG  = 0;
my $DAEMON = 1;

my $getopts_rc = GetOptions(
  "configure"     => \&configure,
  "version"       => \&version,
  "devices"       => \&devices,
  "debug!"        => \$DEBUG,
  "daemon!"       => \$DAEMON,

  "help|?"        => \&print_usage,
);

# daemonize
# TODO: Fix File Descripter issue when initialising logs before daemonizing
my $daemon = Proc::Daemon->new(
  work_dir => "$ENV{HOME}/.eventstreamr/",
);

if ( $DAEMON ) {
  $daemon->Init();
}

my $eventstreamr = App::EventStreamr->new(
  debug => $DEBUG,
);

# setup signal handlers and daemon stuff
# POSIX unmasks the sigprocmask properly
$SIG{CHLD} = 'IGNORE';
my $sigset = POSIX::SigSet->new();
my $update = POSIX::SigAction->new( 'self_update',
                                    $sigset,
                                    &POSIX::SA_NODEFER);
my $exit = POSIX::SigAction->new(   'sig_exit',
                                    $sigset,
                                    &POSIX::SA_NODEFER);
my $pipe = POSIX::SigAction->new(   'sig_pipe',
                                    $sigset,
                                    &POSIX::SA_NODEFER);
my $get = POSIX::SigAction->new(   'get_config',
                                    $sigset,
                                    &POSIX::SA_NODEFER);
my $post = POSIX::SigAction->new(   'post_config',
                                    $sigset,
                                    &POSIX::SA_NODEFER);
# Handle INT/Term
POSIX::sigaction(&POSIX::SIGTERM, $exit);
POSIX::sigaction(&POSIX::SIGINT, $exit);

# SIGHUP updates and execs
POSIX::sigaction(&POSIX::SIGHUP, $update);
POSIX::sigaction(&POSIX::SIGPIPE, $pipe);

# USR1/2 for get/post config
POSIX::sigaction(&POSIX::SIGUSR1, $get);
POSIX::sigaction(&POSIX::SIGUSR2, $post);


sub print_usage {
  say q{
  Usage:

  evenstreamr --configure     : Run through station configuration

  Debugging commands:
    
  eventstreamr --version      : Show version information
  eventstreamr --debug        : Run with debugging enabled.
  eventstreamr --nodaemon     : Run in the foreground, log to console
  eventstreamr --devices      : Print available devices

  For more documentation, use `perldoc eventstreamr`.
  };

  exit 1;
}

sub devices {
  my $eventstreamr = App::EventStreamr->new();
  $eventstreamr->config->update_devices();

  say "ID (Type) - Name";
  foreach my $device (@{$eventstreamr->config->available_devices->{array}}) {
    say "$device->{id} ($device->{type}) - $device->{name}";
  }
  exit 1;
}

sub version {
  $::VERSION ||= "Unreleased";
  say "eventstreamr version          : $::VERSION";
  say "App::EventStreamr version     : ", $eventstreamr->VERSION;
  exit 1;
}

sub configure {
  my $eventstreamr = App::EventStreamr->new();
  $eventstreamr->config->configure;

  exit 1;
}

# Signal Handler Subs
sub self_update {
  # TODO: Implement
}

sub sig_exit {
  $eventstreamr->stop();
}

sub get_config {
  $eventstreamr->config->get_config();
}

sub post_config {
  $eventstreamr->config->post_config();
}

sub sig_pipe {
  print "Caught Pipe\n";
}

# set umask
umask 0027;

chdir($eventstreamr->config->config_path) or $eventstreamr->error("Couldn't change working dir to ".$eventstreamr->config->config_path);

$eventstreamr->run();

__END__

=pod

=encoding UTF-8

=head1 NAME

eventstreamr  - eventstreamr - Daemon that runs the Orchestrator

=head1 VERSION

version 0.5

=head1 SYNOPSIS

Usage:

  evenstreamr --configure     : Run through station configuration

  Debugging commands:
    



( run in 0.348 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )