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)::\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 warn "job $eval failed";
212 my %hash = $ljob->hash;
213 $hash{'status'} = 'failed';
214 $hash{'statustext'} = $@;
215 my $fjob = new FS::queue( \%hash );
216 my $error = $fjob->replace($ljob);
217 die $error if $error;
222 if ( UNIVERSAL::can(dbh, 'sprintProfile') ) {
223 open(PROFILE,">%%%FREESIDE_LOG%%%/queueprofile.$$.".time)
224 or die "can't open profile file: $!";
225 print PROFILE dbh->sprintProfile();
226 close PROFILE or die "can't close profile file: $!";
237 warn "received TERM signal; exiting\n";
241 warn "received INT signal; exiting\n";
247 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
248 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
250 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
256 die "Usage:\n\n freeside-queued user\n";
260 foreach my $pid ( keys %kids ) {
261 my $kid = waitpid($pid, WNOHANG);
271 freeside-queued - Job queue daemon
275 freeside-queued [ -s | -n ] user
279 Job queue daemon. Should be running at all times.
281 -s: "secure" jobs only (queued billing jobs)
283 -n: non-"secure" jobs only (other jobs)
285 user: from the mapsecrets file - see config.html from the base documentation