source: bookmarks/trunk/bin/bkmkd @ 110

Last change on this file since 110 was 110, checked in by peter, 8 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
Line 
1#!/usr/bin/perl -w
2use strict;
3
4use FindBin;
5use lib "$FindBin::RealBin/../lib";
6
7use Getopt::Long;
8use YAML;
9use Plack::Runner;
10use File::Pid;
11use File::Spec::Functions qw{catfile};
12
13GetOptions(
14    'file=s' => \my $CONFIG_FILE,
15);
16
17$CONFIG_FILE ||= 'server.yml';
18-e $CONFIG_FILE or die "Config file $CONFIG_FILE not found\n";
19my $config = YAML::LoadFile($CONFIG_FILE);
20
21my %run = (
22    run     => \&run_server,
23    start   => \&start_server,
24    stop    => \&stop_server,
25    restart => sub {
26        my $config = shift;
27        stop_server($config);
28        start_server($config);
29    },
30);
31
32my $command = shift or die "Usage: $0 <" . join('|', keys %run) . ">\n";
33
34exists $run{$command} or die "Unrecognized command $command\n";
35$run{$command}->($config);
36
37sub run_server {
38    my $config = shift;
39    require BookmarksApp;
40    my $app = BookmarksApp->new({ config => $config })->to_app;
41
42    my $server_root = $config->{server_root} || '.';
43    my $listen      = ':' . ($config->{port} || 5000);
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);
59    my $access_log  = catfile($server_root, 'access');
60    my $error_log   = catfile($server_root, 'errors');
61    my $pid_file    = catfile($server_root, 'pid');
62
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}
73
74sub stop_server {
75    my $config = shift;
76    my $server_root = $config->{server_root} || '.';
77
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.