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);
16 # no autoloading for non-FS classes...
23 &untaint_argv; #what it sounds like (eww)
25 getopts('sn', \%opt );
27 my $user = shift or die &usage;
29 warn "starting daemonization (forking)\n" if $DEBUG;
30 #daemonize1('freeside-queued',$user); #to keep pid files unique w/multi installs
31 daemonize1('freeside-queued');
33 warn "dropping privledges\n" if $DEBUG;
36 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
38 warn "connecting to database\n" if $DEBUG;
41 eval { adminsuidsetup $user; };
44 warn "sleeping for reconnect...\n";
49 my $log = FS::Log->new('queue');
50 logfile( "%%%FREESIDE_LOG%%%/queuelog.". $FS::UID::datasrc );
52 warn "completing daemonization (detaching))\n" if $DEBUG;
57 my $conf = new FS::Conf;
58 $max_kids = $conf->config('queued-max_kids') || 10;
59 $sleep_time = $conf->config('queued-sleep_time') || 10;
65 #prevent runaway forking
66 if ( $kids >= $max_kids ) {
67 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
69 sleep 1; #waiting for signals is cheap
74 unless ( dbh && dbh->ping ) {
75 warn "WARNING: connection to database lost, reconnecting...\n";
77 eval { $FS::UID::dbh = myconnect; };
79 unless ( !$@ && dbh && dbh->ping ) {
80 warn "WARNING: still no connection to database, sleeping for retry...\n";
84 warn "WARNING: reconnected to database\n";
90 # my $oldAutoCommit = $FS::UID::AutoCommit;
91 # local $FS::UID::AutoCommit = 0;
92 $FS::UID::AutoCommit = 0;
94 my $nodepend = 'AND NOT EXISTS( SELECT 1 FROM queue_depend'.
95 ' WHERE queue_depend.jobnum = queue.jobnum )';
97 #anything with a priority goes after stuff without one
98 my $order_by = ' ORDER BY COALESCE(priority,0) ASC, jobnum ASC ';
100 my $limit = $max_kids - $kids;
102 $order_by .= ( driver_name eq 'mysql'
103 ? " LIMIT $limit FOR UPDATE "
104 : " FOR UPDATE LIMIT $limit " );
106 my $hashref = { 'status' => 'new' };
108 $hashref->{'secure'} = 'Y';
109 } elsif ( $opt{'n'} ) {
110 $hashref->{'secure'} = '';
113 #qsearch dies when the db goes away
117 'hashref' => $hashref,
118 'extra_sql' => $nodepend,
119 'order_by' => $order_by,
123 warn "WARNING: error searching for jobs, closing connection: $@";
130 warn "WARNING: database error, closing connection: ". dbh->errstr;
138 foreach my $job ( @jobs ) {
140 $log->debug('locking queue job', object => $job);
142 my %hash = $job->hash;
143 $hash{'status'} = 'locked';
144 my $ljob = new FS::queue ( \%hash );
145 my $error = $ljob->replace($job);
147 warn "WARNING: database error locking job, closing connection: ".
154 warn "WARNING: database error, closing connection: ". dbh->errstr;
159 $FS::UID::AutoCommit = 1;
161 my @args = eval { $ljob->args; };
163 warn "WARNING: error retrieving job arguments, closing connection: $@";
167 splice @args, 0, 1, $ljob if $args[0] eq '_JOB';
169 defined( my $pid = fork ) or do {
170 warn "WARNING: can't fork: $!\n";
171 my %hash = $job->hash;
172 $hash{'status'} = 'failed';
173 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
174 my $ljob = new FS::queue ( \%hash );
175 my $error = $ljob->replace($job);
176 die $error if $error; #XXX still dying if we can't fork AND we can't connect to the db
177 next; #don't increment the kid counter
186 $FS::UID::dbh->{InactiveDestroy} = 1;
188 forksuidsetup($user);
190 dbh->{'private_profile'} = {} if UNIVERSAL::can(dbh, 'sprintProfile');
193 if ( $ljob->job =~ /(FS::(part_export|cust_main|cust_pkg|Cron)::\w+)::/
194 || $ljob->job =~ /(FS::\w+)::/
200 warn "job use $class failed";
201 my %hash = $ljob->hash;
202 $hash{'status'} = 'failed';
203 $hash{'statustext'} = $@;
204 my $fjob = new FS::queue( \%hash );
205 my $error = $fjob->replace($ljob);
206 die $error if $error;
211 my $eval = "&". $ljob->job. '(@args);';
212 # don't put @args in the log, may expose passwords
213 $log->info('starting job ('.$ljob->job.')');
214 warn 'running "&'. $ljob->job. '('. join(', ', @args). ")\n" if $DEBUG;
215 local $FS::UID::AutoCommit = 0; # so that we can clean up failures
216 eval $eval; #throw away return value? suppose so
219 my %hash = $ljob->hash;
220 $hash{'statustext'} = $@;
221 if ( $hash{'statustext'} =~ /\/misc\/queued_report/ ) { #use return?
222 $hash{'status'} = 'done';
224 $hash{'status'} = 'failed';
225 warn "job $eval failed";
227 my $fjob = new FS::queue( \%hash );
228 my $error = $fjob->replace($ljob);
229 die $error if $error;
230 dbh->commit; # for the status change only
233 dbh->commit; # for the job itself
236 if ( UNIVERSAL::can(dbh, 'sprintProfile') ) {
237 open(PROFILE,">%%%FREESIDE_LOG%%%/queueprofile.$$.".time)
238 or die "can't open profile file: $!";
239 print PROFILE dbh->sprintProfile();
240 close PROFILE or die "can't close profile file: $!";
251 warn "received TERM signal; exiting\n";
255 warn "received INT signal; exiting\n";
261 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
262 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
264 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
270 die "Usage:\n\n freeside-queued user\n";
274 foreach my $pid ( keys %kids ) {
275 my $kid = waitpid($pid, WNOHANG);
285 freeside-queued - Job queue daemon
289 freeside-queued [ -s | -n ] user
293 Job queue daemon. Should be running at all times.
295 -s: "secure" jobs only (queued billing jobs)
297 -n: non-"secure" jobs only (other jobs)
299 user: Typically "fs_queue"