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
20 my $pid_file = '/var/run/freeside-queued.pid';
22 my $user = shift or die &usage;
26 sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; }
27 $SIG{CHLD} = \&REAPER;
31 $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $sigint++; };
32 $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $sigterm++; };
34 $> = $FS::UID::freeside_uid unless $>;
36 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
39 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
41 $SIG{__DIE__} = \&_die;
42 $SIG{__WARN__} = \&_logmsg;
44 warn "freeside-queued starting\n";
50 { 'status' => 'new' },
52 driver_name =~ /^mysql$/i
53 ? 'ORDER BY jobnum LIMIT 1 FOR UPDATE'
54 : 'ORDER BY jobnum FOR UPDATE LIMIT 1'
60 my %hash = $job->hash;
61 $hash{'status'} = 'locked';
62 my $ljob = new FS::queue ( \%hash );
63 my $error = $ljob->replace($job);
66 my @args = $ljob->args;
68 # number of children limit?
69 defined( my $pid = fork ) or do {
70 warn "WARNING: can't fork: $!\n";
71 my %hash = $job->hash;
72 $hash{'status'} = 'failed';
73 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
74 my $ljob = new FS::queue ( \%hash );
75 my $error = $ljob->replace($job);
79 unless ( $pid ) { #kid time
82 $FS::UID::dbh->{InactiveDestroy} = 1;
83 $FS::svc_acct::icradius_dbh->{InactiveDestroy} = 1
84 if $FS::svc_acct::icradius_dbh;
87 my $eval = "&". $ljob->job. '(@args);';
89 eval $eval; #throw away return value? suppose so
91 warn "job $eval failed";
92 my %hash = $ljob->hash;
93 $hash{'status'} = 'failed';
94 $hash{'statustext'} = $@;
95 my $fjob = new FS::queue( \%hash );
96 my $error = $fjob->replace($ljob);
108 warn "received TERM signal; exiting\n";
112 warn "received INT signal; exiting\n";
118 die "Usage:\n\n freeside-queued user\n";
123 unlink $pid_file if -e $pid_file;
128 chomp( my $msg = shift );
129 my $log = new IO::File ">>$log_file";
130 flock($log, LOCK_EX);
132 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
133 flock($log, LOCK_UN);
139 chdir "/" or die "Can't chdir to /: $!";
140 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
141 defined(my $pid = fork) or die "Can't fork: $!";
143 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
144 exit unless $pid_file;
145 my $pidfh = new IO::File ">$pid_file" or exit;
146 print $pidfh "$pid\n";
149 open STDOUT, '>/dev/null'
150 or die "Can't write to /dev/null: $!";
151 setsid or die "Can't start a new session: $!";
152 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
158 freeside-queued - Job queue daemon
166 Job queue daemon. Should be running at all times.
168 user: from the mapsecrets file - see config.html from the base documentation