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 use FS::Cron::expire_user_pref qw( expire_user_pref );
18 # no autoloading for non-FS classes...
25 &untaint_argv; #what it sounds like (eww)
27 getopts('sn', \%opt );
29 my $user = shift or die &usage;
31 warn "starting daemonization (forking)\n" if $DEBUG;
32 #daemonize1('freeside-queued',$user); #to keep pid files unique w/multi installs
33 daemonize1('freeside-queued');
35 warn "dropping privledges\n" if $DEBUG;
38 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
40 warn "connecting to database\n" if $DEBUG;
43 eval { adminsuidsetup $user; };
46 warn "sleeping for reconnect...\n";
51 my $log = FS::Log->new('queue');
52 logfile( "%%%FREESIDE_LOG%%%/queuelog.". $FS::UID::datasrc );
54 warn "completing daemonization (detaching))\n" if $DEBUG;
59 my $conf = new FS::Conf;
60 $max_kids = $conf->config('queued-max_kids') || 10;
61 $sleep_time = $conf->config('queued-sleep_time') || 10;
67 #prevent runaway forking
68 if ( $kids >= $max_kids ) {
69 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
71 expire_user_pref() unless $warnkids % 10;
72 sleep 1; #waiting for signals is cheap
77 unless ( dbh && dbh->ping ) {
78 warn "WARNING: connection to database lost, reconnecting...\n";
80 eval { $FS::UID::dbh = myconnect; };
82 unless ( !$@ && dbh && dbh->ping ) {
83 warn "WARNING: still no connection to database, sleeping for retry...\n";
87 warn "WARNING: reconnected to database\n";
93 # my $oldAutoCommit = $FS::UID::AutoCommit;
94 # local $FS::UID::AutoCommit = 0;
95 $FS::UID::AutoCommit = 0;
97 my $nodepend = 'AND NOT EXISTS( SELECT 1 FROM queue_depend'.
98 ' WHERE queue_depend.jobnum = queue.jobnum )';
100 #anything with a priority goes after stuff without one
101 my $order_by = ' ORDER BY COALESCE(priority,0) ASC, jobnum ASC ';
103 my $limit = $max_kids - $kids;
105 $order_by .= ( driver_name eq 'mysql'
106 ? " LIMIT $limit FOR UPDATE "
107 : " FOR UPDATE LIMIT $limit " );
109 my $hashref = { 'status' => 'new' };
111 $hashref->{'secure'} = 'Y';
112 } elsif ( $opt{'n'} ) {
113 $hashref->{'secure'} = '';
116 #qsearch dies when the db goes away
120 'hashref' => $hashref,
121 'extra_sql' => $nodepend,
122 'order_by' => $order_by,
126 warn "WARNING: error searching for jobs, closing connection: $@";
133 warn "WARNING: database error, closing connection: ". dbh->errstr;
142 foreach my $job ( @jobs ) {
144 my $start_date = time;
146 $log->debug('locking queue job', object => $job);
148 my %hash = $job->hash;
149 $hash{'status'} = 'locked';
150 my $ljob = new FS::queue ( \%hash );
151 my $error = $ljob->replace($job);
153 warn "WARNING: database error locking job, closing connection: ".
160 warn "WARNING: database error, closing connection: ". dbh->errstr;
165 $FS::UID::AutoCommit = 1;
167 my @args = eval { $ljob->args; };
169 warn "WARNING: error retrieving job arguments, closing connection: $@";
173 splice @args, 0, 1, $ljob if $args[0] eq '_JOB';
175 defined( my $pid = fork ) or do {
176 warn "WARNING: can't fork: $!\n";
177 my %hash = $job->hash;
178 $hash{'status'} = 'failed';
179 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
180 my $ljob = new FS::queue ( \%hash );
181 my $error = $ljob->replace($job);
182 die $error if $error; #XXX still dying if we can't fork AND we can't connect to the db
183 next; #don't increment the kid counter
192 $FS::UID::dbh->{InactiveDestroy} = 1;
194 forksuidsetup($user);
196 dbh->{'private_profile'} = {} if UNIVERSAL::can(dbh, 'sprintProfile');
199 if ( $ljob->job =~ /(FS::(part_export|cust_main|cust_pkg|part_pkg|Cron)::\w+)::/
200 || $ljob->job =~ /(FS::\w+)::/
206 warn "job use $class failed";
207 my %hash = $ljob->hash;
208 $hash{'status'} = 'failed';
209 $hash{'statustext'} = $@;
210 my $fjob = new FS::queue( \%hash );
211 my $error = $fjob->replace($ljob);
212 die $error if $error;
217 my $eval = "&". $ljob->job. '(@args);';
218 # don't put @args in the log, may expose passwords
219 $log->info('starting job ('.$ljob->job.')');
220 warn 'running "&'. $ljob->job. '('. join(', ', @args). ")\n" if $DEBUG;
221 local $FS::UID::AutoCommit = 0; # so that we can clean up failures
222 eval $eval; #throw away return value? suppose so
225 my %hash = $ljob->hash;
226 $hash{'statustext'} = $@;
227 if ( $hash{'statustext'} =~ /\/misc\/queued_report/ ) { #use return?
228 $hash{'status'} = 'done';
230 $hash{'status'} = 'failed';
231 warn "job $eval failed";
233 my $fjob = new FS::queue( \%hash );
234 my $error = $fjob->replace($ljob);
235 die $error if $error;
236 dbh->commit; # for the status change only
239 dbh->commit; # for the job itself
242 if ( $ljob->job eq 'FS::cust_main::queued_bill' ) {
243 my $queue_stat = new FS::queue_stat {
244 'jobnum' => $ljob->jobnum,
246 'custnum' => $ljob->custnum,
247 'insert_date' => $ljob->_date,
248 'start_date' => $start_date,
251 my $error = $queue_stat->insert;
252 die $error if $error;
253 dbh->commit; #for the stat
256 if ( UNIVERSAL::can(dbh, 'sprintProfile') ) {
257 open(PROFILE,">%%%FREESIDE_LOG%%%/queueprofile.$$.".time)
258 or die "can't open profile file: $!";
259 print PROFILE dbh->sprintProfile();
260 close PROFILE or die "can't close profile file: $!";
271 warn "received TERM signal; exiting\n";
275 warn "received INT signal; exiting\n";
281 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
282 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
284 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
290 die "Usage:\n\n freeside-queued user\n";
294 foreach my $pid ( keys %kids ) {
295 my $kid = waitpid($pid, WNOHANG);
305 freeside-queued - Job queue daemon
309 freeside-queued [ -s | -n ] user
313 Job queue daemon. Should be running at all times.
315 -s: "secure" jobs only (queued billing jobs)
317 -n: non-"secure" jobs only (other jobs)
319 user: Typically "fs_queue"