4 use vars qw( $DEBUG $kids $max_kids $sleep_time %kids );
5 use POSIX qw(:sys_wait_h);
8 use FS::UID qw(adminsuidsetup forksuidsetup driver_name dbh myconnect);
9 use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm);
11 use FS::Record qw(qsearch);
15 use FS::Cron::expire_user_pref qw( expire_user_pref );
17 # no autoloading for non-FS classes...
24 &untaint_argv; #what it sounds like (eww)
26 getopts('sn', \%opt );
28 my $user = shift or die &usage;
30 warn "starting daemonization (forking)\n" if $DEBUG;
31 #daemonize1('freeside-queued',$user); #to keep pid files unique w/multi installs
32 daemonize1('freeside-queued');
34 warn "dropping privledges\n" if $DEBUG;
37 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
39 warn "connecting to database\n" if $DEBUG;
42 eval { adminsuidsetup $user; };
45 warn "sleeping for reconnect...\n";
50 my $log = FS::Log->new('queue');
51 logfile( "%%%FREESIDE_LOG%%%/queuelog.". $FS::UID::datasrc );
53 warn "completing daemonization (detaching))\n" if $DEBUG;
58 my $conf = new FS::Conf;
59 $max_kids = $conf->config('queued-max_kids') || 10;
60 $sleep_time = $conf->config('queued-sleep_time') || 10;
66 #prevent runaway forking
67 if ( $kids >= $max_kids ) {
68 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
70 expire_user_pref() unless $warnkids % 10;
71 sleep 1; #waiting for signals is cheap
76 unless ( dbh && dbh->ping ) {
77 warn "WARNING: connection to database lost, reconnecting...\n";
79 eval { $FS::UID::dbh = myconnect; };
81 unless ( !$@ && dbh && dbh->ping ) {
82 warn "WARNING: still no connection to database, sleeping for retry...\n";
86 warn "WARNING: reconnected to database\n";
92 # my $oldAutoCommit = $FS::UID::AutoCommit;
93 # local $FS::UID::AutoCommit = 0;
94 $FS::UID::AutoCommit = 0;
96 my $nodepend = 'AND NOT EXISTS( SELECT 1 FROM queue_depend'.
97 ' WHERE queue_depend.jobnum = queue.jobnum )';
99 #anything with a priority goes after stuff without one
100 my $order_by = ' ORDER BY COALESCE(priority,0) ASC, jobnum ASC ';
102 my $limit = $max_kids - $kids;
104 $order_by .= ( driver_name eq 'mysql'
105 ? " LIMIT $limit FOR UPDATE "
106 : " FOR UPDATE LIMIT $limit " );
108 my $hashref = { 'status' => 'new' };
110 $hashref->{'secure'} = 'Y';
111 } elsif ( $opt{'n'} ) {
112 $hashref->{'secure'} = '';
115 #qsearch dies when the db goes away
119 'hashref' => $hashref,
120 'extra_sql' => $nodepend,
121 'order_by' => $order_by,
125 warn "WARNING: error searching for jobs, closing connection: $@";
132 warn "WARNING: database error, closing connection: ". dbh->errstr;
141 foreach my $job ( @jobs ) {
143 $log->debug('locking queue job', object => $job);
145 my %hash = $job->hash;
146 $hash{'status'} = 'locked';
147 my $ljob = new FS::queue ( \%hash );
148 my $error = $ljob->replace($job);
150 warn "WARNING: database error locking job, closing connection: ".
157 warn "WARNING: database error, closing connection: ". dbh->errstr;
162 $FS::UID::AutoCommit = 1;
164 my @args = eval { $ljob->args; };
166 warn "WARNING: error retrieving job arguments, closing connection: $@";
170 splice @args, 0, 1, $ljob if $args[0] eq '_JOB';
172 defined( my $pid = fork ) or do {
173 warn "WARNING: can't fork: $!\n";
174 my %hash = $job->hash;
175 $hash{'status'} = 'failed';
176 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
177 my $ljob = new FS::queue ( \%hash );
178 my $error = $ljob->replace($job);
179 die $error if $error; #XXX still dying if we can't fork AND we can't connect to the db
180 next; #don't increment the kid counter
189 $FS::UID::dbh->{InactiveDestroy} = 1;
191 forksuidsetup($user);
193 dbh->{'private_profile'} = {} if UNIVERSAL::can(dbh, 'sprintProfile');
196 if ( $ljob->job =~ /(FS::(part_export|cust_main|cust_pkg|Cron)::\w+)::/
197 || $ljob->job =~ /(FS::\w+)::/
203 warn "job use $class failed";
204 my %hash = $ljob->hash;
205 $hash{'status'} = 'failed';
206 $hash{'statustext'} = $@;
207 my $fjob = new FS::queue( \%hash );
208 my $error = $fjob->replace($ljob);
209 die $error if $error;
214 my $eval = "&". $ljob->job. '(@args);';
215 # don't put @args in the log, may expose passwords
216 $log->info('starting job ('.$ljob->job.')');
217 warn 'running "&'. $ljob->job. '('. join(', ', @args). ")\n" if $DEBUG;
218 local $FS::UID::AutoCommit = 0; # so that we can clean up failures
219 eval $eval; #throw away return value? suppose so
222 my %hash = $ljob->hash;
223 $hash{'statustext'} = $@;
224 if ( $hash{'statustext'} =~ /\/misc\/queued_report/ ) { #use return?
225 $hash{'status'} = 'done';
227 $hash{'status'} = 'failed';
228 warn "job $eval failed";
230 my $fjob = new FS::queue( \%hash );
231 my $error = $fjob->replace($ljob);
232 die $error if $error;
233 dbh->commit; # for the status change only
236 dbh->commit; # for the job itself
239 if ( UNIVERSAL::can(dbh, 'sprintProfile') ) {
240 open(PROFILE,">%%%FREESIDE_LOG%%%/queueprofile.$$.".time)
241 or die "can't open profile file: $!";
242 print PROFILE dbh->sprintProfile();
243 close PROFILE or die "can't close profile file: $!";
254 warn "received TERM signal; exiting\n";
258 warn "received INT signal; exiting\n";
264 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
265 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
267 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
273 die "Usage:\n\n freeside-queued user\n";
277 foreach my $pid ( keys %kids ) {
278 my $kid = waitpid($pid, WNOHANG);
288 freeside-queued - Job queue daemon
292 freeside-queued [ -s | -n ] user
296 Job queue daemon. Should be running at all times.
298 -s: "secure" jobs only (queued billing jobs)
300 -n: non-"secure" jobs only (other jobs)
302 user: from the mapsecrets file - see config.html from the base documentation