source: bookmarks/trunk/bin/bkmkd @ 108

Last change on this file since 108 was 104, checked in by peter, 9 years ago
  • removed the separate stop script (function now handled by bkmkd stop)
  • added a restart command to bkmkd
  • Property svn:executable set to *
File size: 1.8 KB
RevLine 
[93]1#!/usr/bin/perl -w
2use strict;
[54]3
[93]4use FindBin;
[103]5use lib "$FindBin::RealBin/../lib";
[65]6
[94]7use Getopt::Long;
8use YAML;
[93]9use Plack::Runner;
[103]10use File::Pid;
[102]11use File::Spec::Functions qw{catfile};
[93]12
[94]13GetOptions(
14    'file=s' => \my $CONFIG_FILE,
15);
[93]16
[96]17$CONFIG_FILE ||= 'server.yml';
[94]18-e $CONFIG_FILE or die "Config file $CONFIG_FILE not found\n";
19my $config = YAML::LoadFile($CONFIG_FILE);
[103]20my $command = shift or die "Usage: $0 <start|stop>\n";
[94]21
[103]22my %run = (
[104]23    start   => \&_start_server,
24    stop    => \&_stop_server,
25    restart => sub {
[103]26        my $config = shift;
[104]27        _stop_server($config);
28        _start_server($config);
29    },
30);
[94]31
[104]32exists $run{$command} or die "Unrecognized command $command\n";
33$run{$command}->($config);
[103]34
[104]35sub _start_server {
36    my $config = shift;
37    require BookmarksApp;
38    my $app = BookmarksApp->new({ config => $config })->to_app;
[103]39
[104]40    my $server_root = $config->{server_root} || '.';
41    my $listen      = ':' . ($config->{port} || 5000);
42    my $access_log  = catfile($server_root, 'access');
43    my $error_log   = catfile($server_root, 'errors');
44    my $pid_file    = catfile($server_root, 'pid');
[103]45
[104]46    my $runner = Plack::Runner->new(server => 'Starman');
47    $runner->parse_options(
48        '--daemonize',
49        '--listen',     $listen,
50        '--pid',        $pid_file,
51        '--error-log',  $error_log,
52        '--access-log', $access_log,
53    );
54    $runner->run($app);
55}
[103]56
[104]57sub _stop_server {
58    my $config = shift;
59    my $server_root = $config->{server_root} || '.';
[103]60
[104]61    my $pid_path = catfile($server_root, 'pid');
62    unless (-e $pid_path) {
63        warn "$pid_path does not exist\n";
64        return;
65    }
66
67    my $pid_file = File::Pid->new({
68        file => $pid_path,
69    });
70
71    if (my $pid = $pid_file->running) {
72        kill 'TERM', $pid;
73    }
74}
Note: See TracBrowser for help on using the repository browser.