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 # no autoloading for non-FS classes...
22 &untaint_argv; #what it sounds like (eww)
24 getopts('sn', \%opt );
26 my $user = shift or die &usage;
28 warn "starting daemonization (forking)\n" if $DEBUG;
29 #daemonize1('freeside-queued',$user); #to keep pid files unique w/multi installs
30 daemonize1('freeside-queued');
32 warn "dropping privledges\n" if $DEBUG;
35 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
37 warn "connecting to database\n" if $DEBUG;
40 eval { adminsuidsetup $user; };
43 warn "sleeping for reconnect...\n";
48 logfile( "%%%FREESIDE_LOG%%%/queuelog.". $FS::UID::datasrc );
50 warn "completing daemonization (detaching))\n" if $DEBUG;
55 my $conf = new FS::Conf;
56 $max_kids = $conf->config('queued-max_kids') || 10;
57 $sleep_time = $conf->config('queued-sleep_time') || 10;
63 #prevent runaway forking
64 if ( $kids >= $max_kids ) {
65 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
67 sleep 1; #waiting for signals is cheap
72 unless ( dbh && dbh->ping ) {
73 warn "WARNING: connection to database lost, reconnecting...\n";
75 eval { $FS::UID::dbh = myconnect; };
77 unless ( !$@ && dbh && dbh->ping ) {
78 warn "WARNING: still no connection to database, sleeping for retry...\n";
82 warn "WARNING: reconnected to database\n";
88 # my $oldAutoCommit = $FS::UID::AutoCommit;
89 # local $FS::UID::AutoCommit = 0;
90 $FS::UID::AutoCommit = 0;
92 my $nodepend = 'AND NOT EXISTS( SELECT 1 FROM queue_depend'.
93 ' WHERE queue_depend.jobnum = queue.jobnum )';
95 #anything with a priority goes after stuff without one
96 my $order_by = ' ORDER BY COALESCE(priority,0) ASC, jobnum ASC ';
98 my $limit = $max_kids - $kids;
100 $order_by .= ( driver_name eq 'mysql'
101 ? " LIMIT $limit FOR UPDATE "
102 : " FOR UPDATE LIMIT $limit " );
104 my $hashref = { 'status' => 'new' };
106 $hashref->{'secure'} = 'Y';
107 } elsif ( $opt{'n'} ) {
108 $hashref->{'secure'} = '';
111 #qsearch dies when the db goes away
115 'hashref' => $hashref,
116 'extra_sql' => $nodepend,
117 'order_by' => $order_by,
121 warn "WARNING: error searching for jobs, closing connection: $@";
128 warn "WARNING: database error, closing connection: ". dbh->errstr;
136 foreach my $job ( @jobs ) {
138 my %hash = $job->hash;
139 $hash{'status'} = 'locked';
140 my $ljob = new FS::queue ( \%hash );
141 my $error = $ljob->replace($job);
143 warn "WARNING: database error locking job, closing connection: ".
150 warn "WARNING: database error, closing connection: ". dbh->errstr;
155 $FS::UID::AutoCommit = 1;
157 my @args = eval { $ljob->args; };
159 warn "WARNING: error retrieving job arguments, closing connection: $@";
163 splice @args, 0, 1, $ljob if $args[0] eq '_JOB';
165 defined( my $pid = fork ) or do {
166 warn "WARNING: can't fork: $!\n";
167 my %hash = $job->hash;
168 $hash{'status'} = 'failed';
169 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
170 my $ljob = new FS::queue ( \%hash );
171 my $error = $ljob->replace($job);
172 die $error if $error; #XXX still dying if we can't fork AND we can't connect to the db
173 next; #don't increment the kid counter
182 $FS::UID::dbh->{InactiveDestroy} = 1;
184 forksuidsetup($user);
186 dbh->{'private_profile'} = {} if UNIVERSAL::can(dbh, 'sprintProfile');
189 if ( $ljob->job =~ /(FS::(part_export|cust_main|cust_pkg)::\w+)::/
190 || $ljob->job =~ /(FS::\w+)::/
196 warn "job use $class failed";
197 my %hash = $ljob->hash;
198 $hash{'status'} = 'failed';
199 $hash{'statustext'} = $@;
200 my $fjob = new FS::queue( \%hash );
201 my $error = $fjob->replace($ljob);
202 die $error if $error;
207 my $eval = "&". $ljob->job. '(@args);';
208 warn 'running "&'. $ljob->job. '('. join(', ', @args). ")\n" if $DEBUG;
209 eval $eval; #throw away return value? suppose so
211 my %hash = $ljob->hash;
212 $hash{'statustext'} = $@;
213 if ( $hash{'statustext'} =~ /\/misc\/queued_report/ ) { #use return?
214 $hash{'status'} = 'done';
216 $hash{'status'} = 'failed';
217 warn "job $eval failed";
219 my $fjob = new FS::queue( \%hash );
220 my $error = $fjob->replace($ljob);
221 die $error if $error;
226 if ( UNIVERSAL::can(dbh, 'sprintProfile') ) {
227 open(PROFILE,">%%%FREESIDE_LOG%%%/queueprofile.$$.".time)
228 or die "can't open profile file: $!";
229 print PROFILE dbh->sprintProfile();
230 close PROFILE or die "can't close profile file: $!";
241 warn "received TERM signal; exiting\n";
245 warn "received INT signal; exiting\n";
251 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
252 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
254 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
260 die "Usage:\n\n freeside-queued user\n";
264 foreach my $pid ( keys %kids ) {
265 my $kid = waitpid($pid, WNOHANG);
275 freeside-queued - Job queue daemon
279 freeside-queued [ -s | -n ] user
283 Job queue daemon. Should be running at all times.
285 -s: "secure" jobs only (queued billing jobs)
287 -n: non-"secure" jobs only (other jobs)
289 user: from the mapsecrets file - see config.html from the base documentation