Your IP : 18.218.47.83


Current Path : /opt/webdir/bin/
Upload File :
Current File : //opt/webdir/bin/bx-process

#!/usr/bin/perl
# manage background processes
use strict;
use warnings;

use lib "/opt/webdir/lib";
use bxDaemon;
use Output;
use Pool;
use Data::Dumper;
use Getopt::Long;
use File::Basename qw( dirname basename );


# program options
my $prog_name = basename $0;
my $prog_dir  = dirname $0;

my $o_action  = "list";            # type of action that script must do.
                                    # list    - list all task with statuses
                                    # status  - task status by its id
                                    # stop    - stop task ( kill)
                                    # clean   - clean old tasks info
my $o_format  = 'plain';            # format of stdout message
my $o_task    = undef;              # process id, used in status and stop operations
my $o_days    = 7;                  # clear old task that older than $o_days
my $o_verbose = 0;
my $o_help    = undef;
my $o_type    = undef;

# get command line options
Getopt::Long::Configure ("bundling");
my $result_option = GetOptions(
  'v'     => \$o_verbose,   'verbose'   => \$o_verbose,
  'h'     => \$o_help,      'help'      => \$o_help,
  "a:s"   => \$o_action,    'action:s'  => \$o_action,
  "o:s"   => \$o_format,    'output'    => \$o_format,
  "t:s"  => \$o_task,       'task'      => \$o_task,
  "T:s"   => \$o_type,      'type'      => \$o_type,
  "d:s"   => \$o_days,      'days'      => \$o_days,
) or unknown_arg();

# help message
if ( $o_help ) { print_help($prog_name, 0) };
# formt output
if ( $o_format !~ /^(json|plain|te?xt)$/ ) { print_help($prog_name, 1)}
if ( $o_format =~ /^te?xt$/ ) { $o_format = "plain" };

# process request
my $bx = bxDaemon->new();
my $processes = undef;
if ($o_action =~ /^list$/i){
  $processes = $bx->listProcess($o_task);
}elsif ($o_action =~ /^status$/){
  $processes = $bx->statusProcess($o_task);
}elsif ($o_action =~ /^stop$/){
  $processes = $bx->stopProcess($o_task);
}elsif ($o_action =~ /^clean$/ ){
  $processes = $bx->clearHistory($o_days,$o_type);
}else{
  $processes = Output->new( error => 1, message => "Unknown action option. PLease use -h for help message." );
}
#print Dumper( $confMonitor );
$processes->print($o_format);

# print usage
sub print_usage {
  my $prog = shift;
  print "Usage: $prog [-vh] [-a list|status|stop|clean] [-t task] [-d days] \n";
}

# help message
sub print_help {
  my $prog = shift;
  my $exit = shift;
  print_usage( $prog );
  print <<EOT;
Options:
  -h|--help    - show this message
  -v|--verbose - enable verbose mode.
  -a|--action  - task management actions: list|status|stop|clean
  -t|--task    - task id , used in status action
  -d|--days    - days number, used in clean action ( default: 7d )
  -T|--type    - task type (ex. monitor)
 Ex.
  * get list of tasks
 $prog -o json
  * get status of task
 $prog -a status -t monitor_1234567890
  * stop task
 $prog -a stop -t monitor_1234567890
  * clear info about old task ( older than days )
 $prog -a clean -d 10
EOT
;
  exit;
}