4 use vars qw( $DEBUG $kids $max_kids $sleep_time %kids );
5 use POSIX qw(:sys_wait_h);
8 use MIME::Base64 qw(decode_base64);
10 use FS::UID qw(adminsuidsetup forksuidsetup driver_name dbh myconnect);
11 use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm);
13 use FS::Record qw(qsearch);
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 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 my %hash = $job->hash;
141 $hash{'status'} = 'locked';
142 my $ljob = new FS::queue ( \%hash );
143 my $error = $ljob->replace($job);
145 warn "WARNING: database error locking job, closing connection: ".
152 warn "WARNING: database error, closing connection: ". dbh->errstr;
157 $FS::UID::AutoCommit = 1;
159 my @args = eval { $ljob->args; };
161 warn "WARNING: error retrieving job arguments, closing connection: $@";
165 splice @args, 0, 1, $ljob if $args[0] eq '_JOB';
167 defined( my $pid = fork ) or do {
168 warn "WARNING: can't fork: $!\n";
169 my %hash = $job->hash;
170 $hash{'status'} = 'failed';
171 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
172 my $ljob = new FS::queue ( \%hash );
173 my $error = $ljob->replace($job);
174 die $error if $error; #XXX still dying if we can't fork AND we can't connect to the db
175 next; #don't increment the kid counter
184 $FS::UID::dbh->{InactiveDestroy} = 1;
186 forksuidsetup($user);
188 dbh->{'private_profile'} = {} if UNIVERSAL::can(dbh, 'sprintProfile');
191 if ( $ljob->job =~ /(FS::(part_export|cust_main)::\w+)::/
192 || $ljob->job =~ /(FS::\w+)::/
198 warn "job use $class failed";
199 my %hash = $ljob->hash;
200 $hash{'status'} = 'failed';
201 $hash{'statustext'} = $@;
202 my $fjob = new FS::queue( \%hash );
203 my $error = $fjob->replace($ljob);
204 die $error if $error;
209 my $eval = "&". $ljob->job. '(@args);';
210 warn 'running "&'. $ljob->job. '('. join(', ', @args). ")\n" if $DEBUG;
211 eval $eval; #throw away return value? suppose so
213 my %hash = $ljob->hash;
214 $hash{'statustext'} = $@;
215 if ( $hash{'statustext'} =~ /\/misc\/queued_report/ ) { #use return?
216 $hash{'status'} = 'done';
218 $hash{'status'} = 'failed';
219 warn "job $eval failed";
221 my $fjob = new FS::queue( \%hash );
222 my $error = $fjob->replace($ljob);
223 die $error if $error;
228 if ( UNIVERSAL::can(dbh, 'sprintProfile') ) {
229 open(PROFILE,">%%%FREESIDE_LOG%%%/queueprofile.$$.".time)
230 or die "can't open profile file: $!";
231 print PROFILE dbh->sprintProfile();
232 close PROFILE or die "can't close profile file: $!";
243 warn "received TERM signal; exiting\n";
247 warn "received INT signal; exiting\n";
253 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
254 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
256 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
262 die "Usage:\n\n freeside-queued user\n";
266 foreach my $pid ( keys %kids ) {
267 my $kid = waitpid($pid, WNOHANG);
277 freeside-queued - Job queue daemon
281 freeside-queued [ -s | -n ] user
285 Job queue daemon. Should be running at all times.
287 -s: "secure" jobs only (queued billing jobs)
289 -n: non-"secure" jobs only (other jobs)
291 user: from the mapsecrets file - see config.html from the base documentation