source: bookmarks/trunk/bin/bkmkd @ 113

Last change on this file since 113 was 110, checked in by peter, 9 years ago

#13: Add a run command to the bkmkd script.

  • bkmkd run runs the application in the foreground, making it suitable to be the ENTRYPOINT in a Docker container.
  • removed the leading underscore from the function names in bkmkd
  • Property svn:executable set to *
File size: 2.2 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);
20
[103]21my %run = (
[110]22    run     => \&run_server,
23    start   => \&start_server,
24    stop    => \&stop_server,
[104]25    restart => sub {
[103]26        my $config = shift;
[110]27        stop_server($config);
28        start_server($config);
[104]29    },
30);
[94]31
[110]32my $command = shift or die "Usage: $0 <" . join('|', keys %run) . ">\n";
33
[104]34exists $run{$command} or die "Unrecognized command $command\n";
35$run{$command}->($config);
[103]36
[110]37sub run_server {
[104]38    my $config = shift;
39    require BookmarksApp;
40    my $app = BookmarksApp->new({ config => $config })->to_app;
[103]41
[104]42    my $server_root = $config->{server_root} || '.';
43    my $listen      = ':' . ($config->{port} || 5000);
[110]44
45    my $runner = Plack::Runner->new(server => 'Starman');
46    $runner->parse_options(
47        '--listen',     $listen,
48    );
49    $runner->run($app);
50}
51
52sub start_server {
53    my $config = shift;
54    require BookmarksApp;
55    my $app = BookmarksApp->new({ config => $config })->to_app;
56
57    my $server_root = $config->{server_root} || '.';
58    my $listen      = ':' . ($config->{port} || 5000);
[104]59    my $access_log  = catfile($server_root, 'access');
60    my $error_log   = catfile($server_root, 'errors');
61    my $pid_file    = catfile($server_root, 'pid');
[103]62
[104]63    my $runner = Plack::Runner->new(server => 'Starman');
64    $runner->parse_options(
65        '--daemonize',
66        '--listen',     $listen,
67        '--pid',        $pid_file,
68        '--error-log',  $error_log,
69        '--access-log', $access_log,
70    );
71    $runner->run($app);
72}
[103]73
[110]74sub stop_server {
[104]75    my $config = shift;
76    my $server_root = $config->{server_root} || '.';
[103]77
[104]78    my $pid_path = catfile($server_root, 'pid');
79    unless (-e $pid_path) {
80        warn "$pid_path does not exist\n";
81        return;
82    }
83
84    my $pid_file = File::Pid->new({
85        file => $pid_path,
86    });
87
88    if (my $pid = $pid_file->running) {
89        kill 'TERM', $pid;
90    }
91}
Note: See TracBrowser for help on using the repository browser.