4 use vars qw( $DEBUG $kids $max_kids %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;
62 #prevent runaway forking
63 if ( $kids >= $max_kids ) {
64 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
66 sleep 1; #waiting for signals is cheap
71 unless ( dbh && dbh->ping ) {
72 warn "WARNING: connection to database lost, reconnecting...\n";
74 eval { $FS::UID::dbh = myconnect; };
76 unless ( !$@ && dbh && dbh->ping ) {
77 warn "WARNING: still no connection to database, sleeping for retry...\n";
81 warn "WARNING: reconnected to database\n";
87 # my $oldAutoCommit = $FS::UID::AutoCommit;
88 # local $FS::UID::AutoCommit = 0;
89 $FS::UID::AutoCommit = 0;
91 my $nodepend = 'AND NOT EXISTS( SELECT 1 FROM queue_depend'.
92 ' WHERE queue_depend.jobnum = queue.jobnum )';
94 #anything with a priority goes after stuff without one
95 my $order_by = ' ORDER BY COALESCE(priority,0) ASC, jobnum ASC ';
97 my $limit = $max_kids - $kids;
99 $order_by .= ( driver_name eq 'mysql'
100 ? " LIMIT $limit FOR UPDATE "
101 : " FOR UPDATE LIMIT $limit " );
103 my $hashref = { 'status' => 'new' };
105 $hashref->{'secure'} = 'Y';
106 } elsif ( $opt{'n'} ) {
107 $hashref->{'secure'} = '';
110 #qsearch dies when the db goes away
114 'hashref' => $hashref,
115 'extra_sql' => $nodepend,
116 'order_by' => $order_by,
120 warn "WARNING: error searching for jobs, closing connection: $@";
127 warn "WARNING: database error, closing connection: ". dbh->errstr;
135 foreach my $job ( @jobs ) {
137 my %hash = $job->hash;
138 $hash{'status'} = 'locked';
139 my $ljob = new FS::queue ( \%hash );
140 my $error = $ljob->replace($job);
142 warn "WARNING: database error locking job, closing connection: ".
149 warn "WARNING: database error, closing connection: ". dbh->errstr;
154 $FS::UID::AutoCommit = 1;
156 my @args = eval { $ljob->args; };
158 warn "WARNING: error retrieving job arguments, closing connection: $@";
162 splice @args, 0, 1, $ljob if $args[0] eq '_JOB';
164 defined( my $pid = fork ) or do {
165 warn "WARNING: can't fork: $!\n";
166 my %hash = $job->hash;
167 $hash{'status'} = 'failed';
168 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
169 my $ljob = new FS::queue ( \%hash );
170 my $error = $ljob->replace($job);
171 die $error if $error; #XXX still dying if we can't fork AND we can't connect to the db
172 next; #don't increment the kid counter
181 $FS::UID::dbh->{InactiveDestroy} = 1;
183 forksuidsetup($user);
185 dbh->{'private_profile'} = {} if UNIVERSAL::can(dbh, 'sprintProfile');
188 if ( $ljob->job =~ /(FS::(part_export|cust_main)::\w+)::/
189 || $ljob->job =~ /(FS::\w+)::/
195 warn "job use $class failed";
196 my %hash = $ljob->hash;
197 $hash{'status'} = 'failed';
198 $hash{'statustext'} = $@;
199 my $fjob = new FS::queue( \%hash );
200 my $error = $fjob->replace($ljob);
201 die $error if $error;
206 my $eval = "&". $ljob->job. '(@args);';
207 warn 'running "&'. $ljob->job. '('. join(', ', @args). ")\n" if $DEBUG;
208 eval $eval; #throw away return value? suppose so
210 warn "job $eval failed";
211 my %hash = $ljob->hash;
212 $hash{'status'} = 'failed';
213 $hash{'statustext'} = $@;
214 my $fjob = new FS::queue( \%hash );
215 my $error = $fjob->replace($ljob);
216 die $error if $error;
221 if ( UNIVERSAL::can(dbh, 'sprintProfile') ) {
222 open(PROFILE,">%%%FREESIDE_LOG%%%/queueprofile.$$.".time)
223 or die "can't open profile file: $!";
224 print PROFILE dbh->sprintProfile();
225 close PROFILE or die "can't close profile file: $!";
236 warn "received TERM signal; exiting\n";
240 warn "received INT signal; exiting\n";
246 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
247 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
249 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
255 die "Usage:\n\n freeside-queued user\n";
259 foreach my $pid ( keys %kids ) {
260 my $kid = waitpid($pid, WNOHANG);
270 freeside-queued - Job queue daemon
274 freeside-queued [ -s | -n ] user
278 Job queue daemon. Should be running at all times.
280 -s: "secure" jobs only (queued billing jobs)
282 -n: non-"secure" jobs only (other jobs)
284 user: from the mapsecrets file - see config.html from the base documentation