| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2010 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2010-May-12 15:57 (EDT) | ||
| 6 | # Function: yenta daemon | ||
| 7 | # | ||
| 8 | # $Id: D.pm,v 1.6 2010/12/27 19:03:10 jaw Exp $ | ||
| 9 | |||
| 10 | package AC::Yenta::D; | ||
| 11 | |||
| 12 | use AC::Daemon; | ||
| 13 | use AC::DC::IO { monitor => 'AC::Yenta::Stats' }; | ||
| 14 | use AC::Yenta::Config; | ||
| 15 | use AC::Yenta::Debug; | ||
| 16 | use AC::Yenta::Stats; | ||
| 17 | use AC::Yenta::Monitor; | ||
| 18 | use AC::Yenta::Server; | ||
| 19 | use AC::Yenta::Status; | ||
| 20 | use AC::Yenta::Store; | ||
| 21 | use AC::Yenta::MySelf; | ||
| 22 | use AC::Yenta::Store::BDBI; | ||
| 23 | use AC::Yenta::Store::SQLite; | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | |||
| 27 | sub new { | ||
| 28 | my $class = shift; | ||
| 29 | my %p = @_; | ||
| 30 | |||
| 31 | AC::Yenta::MySelf->customize( $p{class_myself} ); | ||
| 32 | # ... | ||
| 33 | |||
| 34 | return bless \$class, $class; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub daemon { | ||
| 38 | my $me = shift; | ||
| 39 | my $cfile = shift; | ||
| 40 | my $opt = shift; # foreground, debugall, persistent_id, argv | ||
| 41 | |||
| 42 | die "no config file specified\n" unless $cfile; | ||
| 43 | |||
| 44 | # configure | ||
| 45 | $AC::Yenta::CONF = AC::Yenta::Config->new( | ||
| 46 | $cfile, onreload => sub { | ||
| 47 | AC::Yenta::Store::configure(); | ||
| 48 | }); | ||
| 49 | |||
| 50 | |||
| 51 | initlog( 'yenta', (conf_value('syslog') || 'local5'), $opt->{debugall} ); | ||
| 52 | |||
| 53 | AC::Yenta::Debug->init( $opt->{debugall}, $AC::Yenta::CONF); | ||
| 54 | daemonize(5, 'yentad', $opt->{argv}) unless $opt->{foreground}; | ||
| 55 | verbose("starting."); | ||
| 56 | |||
| 57 | |||
| 58 | $SIG{CHLD} = $SIG{PIPE} = sub{}; # ignore | ||
| 59 | $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = \&AC::DC::IO::request_exit; # abort | ||
| 60 | |||
| 61 | # initialize subsystems | ||
| 62 | my $port = $opt->{port} || conf_value('port'); | ||
| 63 | |||
| 64 | AC::Yenta::MySelf->init( $port, $opt->{persistent_id} ); | ||
| 65 | AC::Yenta::Store::configure(); | ||
| 66 | AC::Yenta::Status::init( $port ); | ||
| 67 | AC::Yenta::Monitor::init(); | ||
| 68 | AC::DC::IO::TCP::Server->new( $port, 'AC::Yenta::Server' ); | ||
| 69 | |||
| 70 | |||
| 71 | # start "cronjobs" | ||
| 72 | AC::DC::Sched->new( | ||
| 73 | info => 'check config files', | ||
| 74 | freq => 30, | ||
| 75 | func => sub { $AC::Yenta::CONF->check() }, | ||
| 76 | ); | ||
| 77 | |||
| 78 | run_and_watch( | ||
| 79 | ($opt->{foreground} || $opt->{debugall}), | ||
| 80 | \&AC::DC::IO::mainloop, | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | 1; |