| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2010 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2010-Jan-22 13:00 (EST) | ||
| 6 | # Function: m/r daemon | ||
| 7 | # | ||
| 8 | # $Id: D.pm,v 1.1 2010/11/01 18:41:41 jaw Exp $ | ||
| 9 | |||
| 10 | package AC::MrGamoo::D; | ||
| 11 | use AC::DC::IO { monitor => 'AC::MrGamoo::Stats' }; | ||
| 12 | use AC::MrGamoo::Stats; | ||
| 13 | use AC::MrGamoo::Debug; | ||
| 14 | use AC::MrGamoo::Config; | ||
| 15 | use AC::MrGamoo::Server; | ||
| 16 | use AC::MrGamoo::API::Client; | ||
| 17 | use AC::MrGamoo::Retry; | ||
| 18 | use AC::MrGamoo::Xfer; | ||
| 19 | use AC::MrGamoo::Job; | ||
| 20 | use AC::MrGamoo::Task; | ||
| 21 | use AC::MrGamoo::MySelf; | ||
| 22 | use AC::MrGamoo::Kibitz; | ||
| 23 | use AC::MrGamoo::Submit::Compile; | ||
| 24 | use AC::MrGamoo::Submit::Request; | ||
| 25 | |||
| 26 | use AC::Daemon; | ||
| 27 | use AC::Misc; | ||
| 28 | |||
| 29 | require 'AC/protobuf/mrgamoo.pl'; | ||
| 30 | require 'AC/protobuf/std_reply.pl'; | ||
| 31 | use strict; | ||
| 32 | |||
| 33 | |||
| 34 | sub new { | ||
| 35 | my $class = shift; | ||
| 36 | my %p = @_; | ||
| 37 | |||
| 38 | AC::MrGamoo::MySelf->customize( $p{class_myself} ); | ||
| 39 | AC::MrGamoo::FileList->customize( $p{class_filelist} ); | ||
| 40 | AC::MrGamoo::ReadInput->customize( $p{class_readinput} ); | ||
| 41 | # ... | ||
| 42 | |||
| 43 | return bless \$class, $class; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub daemon { | ||
| 47 | my $me = shift; | ||
| 48 | my $cfile = shift; | ||
| 49 | my $opt = shift; # foreground, debugall, persistent_id, argv | ||
| 50 | |||
| 51 | die "no config file specified\n" unless $cfile; | ||
| 52 | |||
| 53 | # configure | ||
| 54 | |||
| 55 | $AC::MrGamoo::CONF = AC::MrGamoo::Config->new( | ||
| 56 | $cfile, onreload => sub {}, | ||
| 57 | ); | ||
| 58 | |||
| 59 | initlog( 'mrgamoo', (conf_value('syslog') || 'local4'), $opt->{debugall} ); | ||
| 60 | AC::MrGamoo::Debug->init( $opt->{debugall}, $AC::MrGamoo::CONF ); | ||
| 61 | |||
| 62 | daemonize(5, 'mrgamood', $opt->{argv}) unless $opt->{foreground}; | ||
| 63 | verbose("starting."); | ||
| 64 | |||
| 65 | $SIG{CHLD} = $SIG{PIPE} = sub {}; # ignore | ||
| 66 | $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = \&AC::DC::IO::request_exit; # abort | ||
| 67 | |||
| 68 | # initialize subsystems | ||
| 69 | |||
| 70 | my $port = $opt->{port} || conf_value('port'); | ||
| 71 | |||
| 72 | AC::MrGamoo::About->init( $port ); | ||
| 73 | AC::MrGamoo::MySelf->init( $port, $opt->{persistent_id} ); | ||
| 74 | AC::DC::IO::TCP::Server->new( $port, 'AC::MrGamoo::Server' ); | ||
| 75 | |||
| 76 | # start "cronjobs" | ||
| 77 | AC::DC::Sched->new( | ||
| 78 | info => 'check config files', | ||
| 79 | freq => 30, | ||
| 80 | func => sub { $AC::MrGamoo::CONF->check() }, | ||
| 81 | ); | ||
| 82 | |||
| 83 | run_and_watch( | ||
| 84 | ($opt->{foreground} || $opt->{debugall}), | ||
| 85 | \&AC::DC::IO::mainloop, | ||
| 86 | ); | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | 1; |