4 use vars qw( $DEBUG $kids $max_kids $sleep_time %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;
52 $sleep_time = $conf->config('queued-sleep_time') || 10;
58 #prevent runaway forking
59 if ( $kids >= $max_kids ) {
60 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
62 sleep 1; #waiting for signals is cheap
67 unless ( dbh && dbh->ping ) {
68 warn "WARNING: connection to database lost, reconnecting...\n";
70 eval { $FS::UID::dbh = myconnect; };
72 unless ( !$@ && dbh && dbh->ping ) {
73 warn "WARNING: still no connection to database, sleeping for retry...\n";
77 warn "WARNING: reconnected to database\n";
83 # my $oldAutoCommit = $FS::UID::AutoCommit;
84 # local $FS::UID::AutoCommit = 0;
85 $FS::UID::AutoCommit = 0;
87 my $nodepend = 'AND NOT EXISTS( SELECT 1 FROM queue_depend'.
88 ' WHERE queue_depend.jobnum = queue.jobnum )';
90 #anything with a priority goes after stuff without one
91 my $order_by = ' ORDER BY COALESCE(priority,0) ASC, jobnum ASC ';
93 my $limit = $max_kids - $kids;
95 $order_by .= ( driver_name eq 'mysql'
96 ? " LIMIT $limit FOR UPDATE "
97 : " FOR UPDATE LIMIT $limit " );
101 'hashref' => { 'status' => 'new' },
102 'extra_sql' => $nodepend,
103 'order_by' => $order_by,
108 warn "WARNING: database error, closing connection: ". dbh->errstr;
116 foreach my $job ( @jobs ) {
118 my %hash = $job->hash;
119 $hash{'status'} = 'locked';
120 my $ljob = new FS::queue ( \%hash );
121 my $error = $ljob->replace($job);
123 warn "WARNING: database error locking job, closing connection: ".
130 warn "WARNING: database error, closing connection: ". dbh->errstr;
135 $FS::UID::AutoCommit = 1;
137 my @args = $ljob->args;
138 splice @args, 0, 1, $ljob if $args[0] eq '_JOB';
140 defined( my $pid = fork ) or do {
141 warn "WARNING: can't fork: $!\n";
142 my %hash = $job->hash;
143 $hash{'status'} = 'failed';
144 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
145 my $ljob = new FS::queue ( \%hash );
146 my $error = $ljob->replace($job);
147 die $error if $error;
148 next; #don't increment the kid counter
157 $FS::UID::dbh->{InactiveDestroy} = 1;
159 forksuidsetup($user);
162 if ( $ljob->job =~ /(FS::(part_export|cust_main)::\w+)::/
163 || $ljob->job =~ /(FS::\w+)::/
169 warn "job use $class failed";
170 my %hash = $ljob->hash;
171 $hash{'status'} = 'failed';
172 $hash{'statustext'} = $@;
173 my $fjob = new FS::queue( \%hash );
174 my $error = $fjob->replace($ljob);
175 die $error if $error;
180 my $eval = "&". $ljob->job. '(@args);';
181 warn 'running "&'. $ljob->job. '('. join(', ', @args). ")\n" if $DEBUG;
182 eval $eval; #throw away return value? suppose so
184 warn "job $eval failed";
185 my %hash = $ljob->hash;
186 $hash{'status'} = 'failed';
187 $hash{'statustext'} = $@;
188 my $fjob = new FS::queue( \%hash );
189 my $error = $fjob->replace($ljob);
190 die $error if $error;
203 warn "received TERM signal; exiting\n";
207 warn "received INT signal; exiting\n";
213 die "Usage:\n\n freeside-queued user\n";
217 foreach my $pid ( keys %kids ) {
218 my $kid = waitpid($pid, WNOHANG);
228 freeside-queued - Job queue daemon
236 Job queue daemon. Should be running at all times.
238 user: from the mapsecrets file - see config.html from the base documentation