4 use vars qw( $DEBUG $kids $max_kids %kids );
5 use POSIX qw(:sys_wait_h);
7 use FS::UID qw(adminsuidsetup forksuidsetup driver_name dbh myconnect);
8 use FS::Daemon qw(daemonize1 drop_root logfile daemonize2 sigint sigterm);
10 use FS::Record qw(qsearch);
14 # no autoloading for non-FS classes...
21 my $user = shift or die &usage;
23 warn "starting daemonization (forking)\n" if $DEBUG;
24 #daemonize1('freeside-queued',$user); #to keep pid files unique w/multi installs
25 daemonize1('freeside-queued');
27 warn "dropping privledges\n" if $DEBUG;
30 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
32 warn "connecting to database\n" if $DEBUG;
35 eval { adminsuidsetup $user; };
38 warn "sleeping for reconnect...\n";
43 logfile( "%%%FREESIDE_LOG%%%/queuelog.". $FS::UID::datasrc );
45 warn "completing daemonization (detaching))\n" if $DEBUG;
50 my $conf = new FS::Conf;
51 $max_kids = $conf->config('queued-max_kids') || 10;
57 #prevent runaway forking
58 if ( $kids >= $max_kids ) {
59 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
61 sleep 1; #waiting for signals is cheap
66 unless ( dbh && dbh->ping ) {
67 warn "WARNING: connection to database lost, reconnecting...\n";
69 eval { $FS::UID::dbh = myconnect; };
71 unless ( !$@ && dbh && dbh->ping ) {
72 warn "WARNING: still no connection to database, sleeping for retry...\n";
76 warn "WARNING: reconnected to database\n";
82 # my $oldAutoCommit = $FS::UID::AutoCommit;
83 # local $FS::UID::AutoCommit = 0;
84 $FS::UID::AutoCommit = 0;
86 my $nodepend = 'AND NOT EXISTS( SELECT 1 FROM queue_depend'.
87 ' WHERE queue_depend.jobnum = queue.jobnum )';
89 #anything with a priority goes after stuff without one
90 my $order_by = ' ORDER BY COALESCE(priority,0) ASC, jobnum ASC ';
92 my $limit = $max_kids - $kids;
94 $order_by .= ( driver_name eq 'mysql'
95 ? " LIMIT $limit FOR UPDATE "
96 : " FOR UPDATE LIMIT $limit " );
100 'hashref' => { 'status' => 'new' },
101 'extra_sql' => $nodepend,
102 'order_by' => $order_by,
107 warn "WARNING: database error, closing connection: ". dbh->errstr;
115 foreach my $job ( @jobs ) {
117 my %hash = $job->hash;
118 $hash{'status'} = 'locked';
119 my $ljob = new FS::queue ( \%hash );
120 my $error = $ljob->replace($job);
122 warn "WARNING: database error locking job, closing connection: ".
129 warn "WARNING: database error, closing connection: ". dbh->errstr;
134 $FS::UID::AutoCommit = 1;
136 my @args = $ljob->args;
137 splice @args, 0, 1, $ljob if $args[0] eq '_JOB';
139 defined( my $pid = fork ) or do {
140 warn "WARNING: can't fork: $!\n";
141 my %hash = $job->hash;
142 $hash{'status'} = 'failed';
143 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
144 my $ljob = new FS::queue ( \%hash );
145 my $error = $ljob->replace($job);
146 die $error if $error;
147 next; #don't increment the kid counter
156 $FS::UID::dbh->{InactiveDestroy} = 1;
158 forksuidsetup($user);
161 if ( $ljob->job =~ /(FS::(part_export|cust_main)::\w+)::/
162 || $ljob->job =~ /(FS::\w+)::/
168 warn "job use $class failed";
169 my %hash = $ljob->hash;
170 $hash{'status'} = 'failed';
171 $hash{'statustext'} = $@;
172 my $fjob = new FS::queue( \%hash );
173 my $error = $fjob->replace($ljob);
174 die $error if $error;
179 my $eval = "&". $ljob->job. '(@args);';
180 warn 'running "&'. $ljob->job. '('. join(', ', @args). ")\n" if $DEBUG;
181 eval $eval; #throw away return value? suppose so
183 warn "job $eval failed";
184 my %hash = $ljob->hash;
185 $hash{'status'} = 'failed';
186 $hash{'statustext'} = $@;
187 my $fjob = new FS::queue( \%hash );
188 my $error = $fjob->replace($ljob);
189 die $error if $error;
202 warn "received TERM signal; exiting\n";
206 warn "received INT signal; exiting\n";
212 die "Usage:\n\n freeside-queued user\n";
216 foreach my $pid ( keys %kids ) {
217 my $kid = waitpid($pid, WNOHANG);
227 freeside-queued - Job queue daemon
235 Job queue daemon. Should be running at all times.
237 user: from the mapsecrets file - see config.html from the base documentation