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'} = '';
112 'hashref' => $hashref,
113 'extra_sql' => $nodepend,
114 'order_by' => $order_by,
119 warn "WARNING: database error, closing connection: ". dbh->errstr;
127 foreach my $job ( @jobs ) {
129 my %hash = $job->hash;
130 $hash{'status'} = 'locked';
131 my $ljob = new FS::queue ( \%hash );
132 my $error = $ljob->replace($job);
134 warn "WARNING: database error locking job, closing connection: ".
141 warn "WARNING: database error, closing connection: ". dbh->errstr;
146 $FS::UID::AutoCommit = 1;
148 my @args = $ljob->args;
149 splice @args, 0, 1, $ljob if $args[0] eq '_JOB';
151 defined( my $pid = fork ) or do {
152 warn "WARNING: can't fork: $!\n";
153 my %hash = $job->hash;
154 $hash{'status'} = 'failed';
155 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
156 my $ljob = new FS::queue ( \%hash );
157 my $error = $ljob->replace($job);
158 die $error if $error;
159 next; #don't increment the kid counter
168 $FS::UID::dbh->{InactiveDestroy} = 1;
170 forksuidsetup($user);
172 dbh->{'private_profile'} = {} if UNIVERSAL::can(dbh, 'sprintProfile');
175 if ( $ljob->job =~ /(FS::(part_export|cust_main)::\w+)::/
176 || $ljob->job =~ /(FS::\w+)::/
182 warn "job use $class failed";
183 my %hash = $ljob->hash;
184 $hash{'status'} = 'failed';
185 $hash{'statustext'} = $@;
186 my $fjob = new FS::queue( \%hash );
187 my $error = $fjob->replace($ljob);
188 die $error if $error;
193 my $eval = "&". $ljob->job. '(@args);';
194 warn 'running "&'. $ljob->job. '('. join(', ', @args). ")\n" if $DEBUG;
195 eval $eval; #throw away return value? suppose so
197 warn "job $eval failed";
198 my %hash = $ljob->hash;
199 $hash{'status'} = 'failed';
200 $hash{'statustext'} = $@;
201 my $fjob = new FS::queue( \%hash );
202 my $error = $fjob->replace($ljob);
203 die $error if $error;
208 if ( UNIVERSAL::can(dbh, 'sprintProfile') ) {
209 open(PROFILE,">%%%FREESIDE_LOG%%%/queueprofile.$$.".time)
210 or die "can't open profile file: $!";
211 print PROFILE dbh->sprintProfile();
212 close PROFILE or die "can't close profile file: $!";
223 warn "received TERM signal; exiting\n";
227 warn "received INT signal; exiting\n";
233 foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
234 #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
236 $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
242 die "Usage:\n\n freeside-queued user\n";
246 foreach my $pid ( keys %kids ) {
247 my $kid = waitpid($pid, WNOHANG);
257 freeside-queued - Job queue daemon
261 freeside-queued [ -s | -n ] user
265 Job queue daemon. Should be running at all times.
267 -s: "secure" jobs only (queued billing jobs)
269 -n: non-"secure" jobs only (other jobs)
271 user: from the mapsecrets file - see config.html from the base documentation