| 1 | package Smolder::Server::PreFork; | ||
| 2 | use Smolder::Conf qw(PidFile LogFile DataDir); | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base qw(Net::Server::PreFork); | ||
| 6 | |||
| 7 | sub configure_hook { | ||
| 8 | my $self = shift; | ||
| 9 | my $prop = $self->{server}; | ||
| 10 | |||
| 11 | # This all runs in the parent, before forking | ||
| 12 | # | ||
| 13 | |||
| 14 | # Use our pid file | ||
| 15 | $prop->{pid_file} = PidFile; | ||
| 16 | |||
| 17 | # Ensure that we fork | ||
| 18 | $prop->{background} = 1; | ||
| 19 | |||
| 20 | # Create data dir if needed | ||
| 21 | if (not -e DataDir) { | ||
| 22 | mkpath(DataDir) or die sprintf("Could not create %s: $!", DataDir); | ||
| 23 | } | ||
| 24 | |||
| 25 | unless (-e Smolder::DB->db_file) { | ||
| 26 | |||
| 27 | # do we have a database? If not then create one | ||
| 28 | Smolder::DB->create_database; | ||
| 29 | } else { | ||
| 30 | |||
| 31 | # upgrade if we need to | ||
| 32 | require Smolder::Upgrade; | ||
| 33 | Smolder::Upgrade->new->upgrade(); | ||
| 34 | } | ||
| 35 | |||
| 36 | # preload our perl modules | ||
| 37 | require Smolder::Dispatch; | ||
| 38 | require Smolder::Control; | ||
| 39 | require Smolder::Control::Admin; | ||
| 40 | require Smolder::Control::Admin::Developers; | ||
| 41 | require Smolder::Control::Admin::Projects; | ||
| 42 | require Smolder::Control::Developer; | ||
| 43 | require Smolder::Control::Graphs; | ||
| 44 | require Smolder::Control::Projects; | ||
| 45 | require Smolder::Control::Developer::Prefs; | ||
| 46 | require Smolder::Control::Public; | ||
| 47 | require Smolder::Control::Public::Auth; | ||
| 48 | require Smolder::Redirect; | ||
| 49 | |||
| 50 | $self->SUPER::configure_hook(); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub post_configure_hook { | ||
| 54 | my $self = shift; | ||
| 55 | my $prop = $self->{server}; | ||
| 56 | |||
| 57 | # This all runs in the child, after forking | ||
| 58 | # | ||
| 59 | |||
| 60 | # Send warnings to our logs | ||
| 61 | my $log_file = LogFile || devnull(); | ||
| 62 | my $ok = open(STDERR, '>>', $log_file); | ||
| 63 | if (!$ok) { | ||
| 64 | warn "Could not open logfile $log_file for appending: $!"; | ||
| 65 | exit(1); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; |