Skip to content

Commit

Permalink
Daemonize with MX::Daemonize
Browse files Browse the repository at this point in the history
  • Loading branch information
Caldrin authored and renormalist committed Mar 15, 2014
1 parent f89e39e commit 5ee71fe
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
23 changes: 13 additions & 10 deletions bin/tapper-notify-daemon
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
use strict;
use warnings;

use File::Spec::Functions 'tmpdir';
use Tapper::Notification;
use App::Daemon 'daemonize';
use Tapper::Notification::Daemon;

my ($tmpfh, $tmpfile) = tempfile("tapper-notification-daemon.log-XXXXX", UNLINK => 1);
$App::Daemon::as_user = "root";
$App::Daemon::logfile = $ENV{HARNESS_ACTIVE} ? $tmpfile : '/var/log/tapper-notification-daemon.log';
$App::Daemon::pidfile = tmpdir()."/tapper-notification-daemon.pid";
daemonize();
my $daemon = Tapper::Notification->new;
$daemon->loop;
$ENV{MX_DAEMON_STDOUT} ||= '/var/log/tapper_notification_daemon_stdout.log';
$ENV{MX_DAEMON_STDERR} ||= '/var/log/tapper_notification_daemon_stderr.log';

$ENV{MX_DAEMON_STDOUT} = '/tmp/tapper_notification_daemon_stdout.log' if not -w $ENV{MX_DAEMON_STDOUT};
$ENV{MX_DAEMON_STDERR} = '/tmp/tapper_notification_daemon_stderr.log' if not -w $ENV{MX_DAEMON_STDERR};


my $notify = Tapper::Notification::Daemon->new
(
pidfile => '/tmp/tapper-reports-api-daemon.pid'
);
$notify->run;

62 changes: 62 additions & 0 deletions lib/Tapper/Notification/Daemon.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package Tapper::Notification::Daemon;

use 5.010;

use strict;
use warnings;

use Tapper::Notification;
use Moose;
use Tapper::Config;
use Log::Log4perl;

with 'MooseX::Daemonize';


after start => sub {
my $self = shift;

return unless $self->is_daemon;

my $logconf = Tapper::Config->subconfig->{files}{log4perl_cfg};
Log::Log4perl->init($logconf);

$self->initialize_server;
$self->server->server_loop;
}
;

=head2 initialize_server
Initialize and start daemon according to config.
=cut

sub initialize_server
{
my $self = shift;

my $daemon = Tapper::Notification->new;
$daemon->loop;
}
;

=head2 run
Frontend to subcommands: start, status, restart, stop.
=cut

sub run
{
my $self = shift;

my ($command) = @ARGV ? @ARGV : @_;
return unless $command && grep /^$command$/, qw(start status restart stop);
$self->$command;
say $self->status_message;
}
;


1;

0 comments on commit 5ee71fe

Please sign in to comment.