Changeset 115 in bookmarks


Ignore:
Timestamp:
02/18/16 22:27:07 (9 years ago)
Author:
peter
Message:

Improved the bkmkd control script:

  • bkmkd restart sends a graceful restart (HUP) signal
  • use the -D command line switch to override the config file options
  • use the --verbose command line switch to dump the calculated config settings before starting the server
  • calculate all relative paths in the config file as relative to server_root, whcih defaults to the current working directory
  • bkmkd stop sends a graceful stop (QUIT) signal instead of TERM
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/bkmkd

    r110 r115  
    99use Plack::Runner; 
    1010use File::Pid; 
    11 use File::Spec::Functions qw{catfile}; 
     11use File::Spec::Functions qw{catfile rel2abs}; 
     12use Cwd; 
    1213 
    1314GetOptions( 
    14     'file=s' => \my $CONFIG_FILE, 
     15    'file=s'    => \my $CONFIG_FILE, 
     16    'verbose|v' => \my $VERBOSE, 
     17    'D=s'       => \my %DEFINES, 
    1518); 
    16  
    17 $CONFIG_FILE ||= 'server.yml'; 
    18 -e $CONFIG_FILE or die "Config file $CONFIG_FILE not found\n"; 
    19 my $config = YAML::LoadFile($CONFIG_FILE); 
    2019 
    2120my %run = ( 
    2221    run     => \&run_server, 
    2322    start   => \&start_server, 
    24     stop    => \&stop_server, 
    25     restart => sub { 
    26         my $config = shift; 
    27         stop_server($config); 
    28         start_server($config); 
    29     }, 
     23    stop    => sub { signal_server('QUIT') }, 
     24    restart => sub { signal_server('HUP') }, 
     25); 
     26 
     27my %default_config = ( 
     28    server_root => cwd, 
     29    port        => 5000, 
     30    access_log  => 'access', 
     31    error_log   => 'error', 
     32    pid_file    => 'pid', 
    3033); 
    3134 
     
    3336 
    3437exists $run{$command} or die "Unrecognized command $command\n"; 
    35 $run{$command}->($config); 
     38$run{$command}->(); 
     39 
     40sub get_config { 
     41    $CONFIG_FILE ||= 'server.yml'; 
     42    my $config_from_file = -e $CONFIG_FILE ? YAML::LoadFile($CONFIG_FILE) : {}; 
     43 
     44    my $config = { 
     45        %default_config, 
     46        %{ $config_from_file }, 
     47        %DEFINES, 
     48    }; 
     49 
     50    # make config paths absolute before handing off to the app 
     51    for my $file_key (qw{dbname htdigest access_log error_log pid_file}) { 
     52        if ($config->{$file_key}) { 
     53            $config->{$file_key} = rel2abs($config->{$file_key}, $config->{server_root}); 
     54        } 
     55    } 
     56 
     57    warn Dump($config) if $VERBOSE; 
     58 
     59    return $config; 
     60} 
    3661 
    3762sub run_server { 
    38     my $config = shift; 
     63    my $config = get_config(); 
     64 
    3965    require BookmarksApp; 
    4066    my $app = BookmarksApp->new({ config => $config })->to_app; 
    4167 
    42     my $server_root = $config->{server_root} || '.'; 
    43     my $listen      = ':' . ($config->{port} || 5000); 
    44  
    4568    my $runner = Plack::Runner->new(server => 'Starman'); 
    4669    $runner->parse_options( 
    47         '--listen',     $listen, 
     70        '--listen', ':' . $config->{port}, 
    4871    ); 
    4972    $runner->run($app); 
     
    5174 
    5275sub start_server { 
    53     my $config = shift; 
     76    my $config = get_config(); 
     77 
    5478    require BookmarksApp; 
    5579    my $app = BookmarksApp->new({ config => $config })->to_app; 
    56  
    57     my $server_root = $config->{server_root} || '.'; 
    58     my $listen      = ':' . ($config->{port} || 5000); 
    59     my $access_log  = catfile($server_root, 'access'); 
    60     my $error_log   = catfile($server_root, 'errors'); 
    61     my $pid_file    = catfile($server_root, 'pid'); 
    6280 
    6381    my $runner = Plack::Runner->new(server => 'Starman'); 
    6482    $runner->parse_options( 
    6583        '--daemonize', 
    66         '--listen',     $listen, 
    67         '--pid',        $pid_file, 
    68         '--error-log',  $error_log, 
    69         '--access-log', $access_log, 
     84        '--listen',     ':' . $config->{port}, 
     85        '--pid',        $config->{pid_file}, 
     86        '--error-log',  $config->{error_log}, 
     87        '--access-log', $config->{access_log}, 
    7088    ); 
    7189    $runner->run($app); 
    7290} 
    7391 
    74 sub stop_server { 
    75     my $config = shift; 
    76     my $server_root = $config->{server_root} || '.'; 
     92sub signal_server { 
     93    my $config = get_config(); 
     94    my $signal = shift; 
    7795 
    78     my $pid_path = catfile($server_root, 'pid'); 
     96    my $pid_path = $config->{pid_file}; 
    7997    unless (-e $pid_path) { 
    8098        warn "$pid_path does not exist\n"; 
     
    87105 
    88106    if (my $pid = $pid_file->running) { 
    89         kill 'TERM', $pid; 
     107        kill $signal, $pid; 
    90108    } 
    91109} 
Note: See TracChangeset for help on using the changeset viewer.