4 use vars qw( $log_file $sigterm $sigint );
5 use subs qw( _die _logmsg );
10 use FS::UID qw(adminsuidsetup forksuidsetup driver_name);
11 use FS::Record qw(qsearchs);
14 # no autoloading just yet
19 my $pid_file = '/var/run/freeside-queued.pid';
21 my $user = shift or die &usage;
25 sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; }
26 $SIG{CHLD} = \&REAPER;
30 $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $sigint++; };
31 $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $sigterm++; };
33 $> = $FS::UID::freeside_uid unless $>;
35 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
38 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
40 $SIG{__DIE__} = \&_die;
41 $SIG{__WARN__} = \&_logmsg;
43 warn "freeside-queued starting\n";
49 { 'status' => 'new' },
51 driver_name =~ /^mysql$/i
52 ? 'ORDER BY jobnum LIMIT 1 FOR UPDATE'
53 : 'ORDER BY jobnum FOR UPDATE LIMIT 1'
59 my %hash = $job->hash;
60 $hash{'status'} = 'locked';
61 my $ljob = new FS::queue ( \%hash );
62 my $error = $ljob->replace($job);
65 my @args = $ljob->args;
67 # number of children limit?
68 defined( my $pid = fork ) or do {
69 warn "WARNING: can't fork: $!\n";
70 my %hash = $job->hash;
71 $hash{'status'} = 'failed';
72 my $ljob = new FS::queue ( \%hash );
73 my $error = $ljob->replace($job);
77 unless ( $pid ) { #kid time
80 $FS::UID::dbh->{InactiveDestroy} = 1;
81 $FS::svc_acct::icradius_dbh->{InactiveDestroy} = 1
82 if $FS::svc_acct::icradius_dbh;
85 my $eval = "&". $ljob->job. '(@args);';
89 warn "job $eval failed";
90 my %hash = $ljob->hash;
91 $hash{'status'} = 'failed';
92 my $fjob = new FS::queue( \%hash );
93 my $error = $fjob->replace($ljob);
105 warn "received TERM signal; exiting\n";
109 warn "received INT signal; exiting\n";
115 die "Usage:\n\n freeside-queued user\n";
120 unlink $pid_file if -e $pid_file;
125 chomp( my $msg = shift );
126 my $log = new IO::File ">>$log_file";
127 flock($log, LOCK_EX);
129 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
130 flock($log, LOCK_UN);
136 chdir "/" or die "Can't chdir to /: $!";
137 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
138 defined(my $pid = fork) or die "Can't fork: $!";
140 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
141 exit unless $pid_file;
142 my $pidfh = new IO::File ">$pid_file" or exit;
143 print $pidfh "$pid\n";
146 open STDOUT, '>/dev/null'
147 or die "Can't write to /dev/null: $!";
148 setsid or die "Can't start a new session: $!";
149 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
155 freeside-queued - Job queue daemon
163 Job queue daemon. Should be running at all times.
165 user: from the mapsecrets file - see config.html from the base documentation