From c7553996701186c3527eb3de47f6c863b408ad5b Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sun, 19 Jul 2020 00:42:18 -0400 Subject: [PATCH] prod: Fix production server daemon behavior So I was under the impression that the 'daemon' command, well, created a daemon. This is false. To truly daemonize, one needs to call the server's daemonize command (which took forever to figure out how to do, due to the class hierarchy... but I figured it out!). Anyways, with that solved, the only thing left was how to still capture logs. The solution is to use a global logger (i.e. shared settings) between all modules (tied to the $:: a.k.a. main namespace). With both of these in place, the rc.d script finally looks normal. After all, rc.d is about managing daemons, not processes that want to be daemons :) --- bin/euchre-service.sh | 6 +----- gloat.pl | 14 +++++++++++++- lib/Euchre/Host.pm | 21 +++++++++++---------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/bin/euchre-service.sh b/bin/euchre-service.sh index fe9e33f..aa15d60 100644 --- a/bin/euchre-service.sh +++ b/bin/euchre-service.sh @@ -5,13 +5,9 @@ INSTALL="/home/_euchre/prod-el" # end config -daemon="$INSTALL/gloat.pl daemon -m production" -daemon_flags=">> /var/log/gloat/prod.log 2>&1 &" +daemon="perl $INSTALL/gloat.pl daemon -m production" daemon_user="_euchre" . /etc/rc.d/rc.subr -# use pexp because the redirections in the flags won't show up in pgrep -pexp="perl $daemon" - rc_cmd "$1" diff --git a/gloat.pl b/gloat.pl index 99d52ea..752e50d 100755 --- a/gloat.pl +++ b/gloat.pl @@ -9,10 +9,11 @@ use Mojo::IOLoop; use FindBin; use lib "$FindBin::RealBin/lib"; +$::LOG = app->log; # save off for global access, befroe loading Euchre::Host use Euchre::Host; # Always log in debug, regardless of prod vs preprod -$ENV{MOJO_LOG_LEVEL} = 'debug'; +app->log->level('debug'); plugin Webpack => {process => [qw(js css sass)]}; @@ -70,4 +71,15 @@ Mojo::IOLoop->recurring($cleanup_time => sub { prune_players(); }); +# Our prod environment is a true daemon +if (app->mode() eq 'production') { + app->hook( + before_server_start => sub { + my ($server, $app) = @_; + $server->daemonize(); + app->log->path("/var/log/gloat/prod.log"); + } + ); +} + app->start; diff --git a/lib/Euchre/Host.pm b/lib/Euchre/Host.pm index f93b48e..3c5082c 100644 --- a/lib/Euchre/Host.pm +++ b/lib/Euchre/Host.pm @@ -5,7 +5,10 @@ use warnings; package Euchre::Host; -use Mojo::Log; +if (!defined $::LOG) { + use Mojo::Log; + $::LOG = Mojo::Log->new(); +} use Euchre::Errors; use Euchre::Dealer; @@ -28,8 +31,6 @@ our %PLAYERS; our %DEALERS; our %PINDEX; # Player id => Dealer id -our $LOG = Mojo::Log->new; - # Stats our $TOTAL_PLAYERS = 0; our $TOTAL_TABLES = 0; @@ -59,7 +60,7 @@ sub gloaters_never_win { my $p = $PLAYERS{$id}; try_leave_table($p); - $LOG->info("Player " . $p->name . " went inactive"); + $::LOG->info("Player " . $p->name . " went inactive"); delete $PLAYERS{$id}; } @@ -144,14 +145,14 @@ sub join_table { (exists $msg->{password} ? (password => $msg->{password}) : ()), ); $TOTAL_TABLES++; - $LOG->info("Player " . $p->name . " created table $tid"); + $::LOG->info("Player " . $p->name . " created table $tid"); } my $d = $DEALERS{$tid}; if (my $errno = $d->add_player($p, $msg->{password})) { $p->error($errno); } else { - $LOG->info("Player " . $p->name . " joined table $tid"); + $::LOG->info("Player " . $p->name . " joined table $tid"); $PINDEX{$p->id} = $tid; } } @@ -169,9 +170,9 @@ sub leave_table { # Success! Was removed properly, delete PINDEX # Also delete the Dealer itself if that was the last player # to leave and the game looks finished - $LOG->info("Player " . $p->name . " left table " . $d->id); + $::LOG->info("Player " . $p->name . " left table " . $d->id); if (!$d->is_active && $d->game->phase eq 'end') { - $LOG->info("Deleting Table " . $d->id . " that appears to have finished"); + $::LOG->info("Deleting Table " . $d->id . " that appears to have finished"); delete $DEALERS{$PINDEX{$p->id}}; } delete $PINDEX{$p->id}; @@ -246,7 +247,7 @@ sub require_keys { sub prune_tables { for my $k (keys %DEALERS) { if (!$DEALERS{$k}->is_active) { - $LOG->info("Pruning inactive table " . $DEALERS{$k}->id); + $::LOG->info("Pruning inactive table " . $DEALERS{$k}->id); delete $DEALERS{$k}; } } @@ -256,7 +257,7 @@ sub prune_tables { sub prune_players { for my $p (keys %PLAYERS) { if (!$PLAYERS{$p}->is_active) { - $LOG->info("Pruning inactive player " . $PLAYERS{$p}->name); + $::LOG->info("Pruning inactive player " . $PLAYERS{$p}->name); try_leave_table($PLAYERS{$p}); delete $PLAYERS{$p}; } -- libgit2 1.1.1