4 use vars qw( $log_file $sigterm $sigint $kids $max_kids %kids );
5 use subs qw( _die _logmsg );
7 use POSIX qw(:sys_wait_h setsid);
10 use FS::UID qw(adminsuidsetup forksuidsetup driver_name dbh myconnect);
11 use FS::Record qw(qsearch qsearchs);
15 # no autoloading just yet
21 $max_kids = '10'; #guess it should be a config file...
24 my $user = shift or die &usage;
26 #my $pid_file = "/var/run/freeside-queued.$user.pid";
27 my $pid_file = "/var/run/freeside-queued.pid";
31 #sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; $kids--; }
32 #$SIG{CHLD} = \&REAPER;
36 $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $sigint++; };
37 $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $sigterm++; };
39 my $freeside_gid = scalar(getgrnam('freeside'))
40 or die "can't setgid to freeside group\n";
43 #if freebsd can't setuid(), presumably it can't setgid() either. grr fleabsd
47 $> = $FS::UID::freeside_uid;
48 $< = $FS::UID::freeside_uid;
49 #freebsd is sofa king broken, won't setuid()
51 $> = $FS::UID::freeside_uid;
53 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
56 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
60 $SIG{__DIE__} = \&_die;
61 $SIG{__WARN__} = \&_logmsg;
63 warn "freeside-queued starting\n";
69 #prevent runaway forking
70 if ( $kids >= $max_kids ) {
71 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
73 sleep 1; #waiting for signals is cheap
79 unless ( $dbh->ping ) {
80 warn "WARNING: connection to database lost, reconnecting...\n";
82 unless ( $dbh->ping ) {
83 warn "WARNING: still no connection to database, sleeping for retry...\n";
91 # my $oldAutoCommit = $FS::UID::AutoCommit;
92 # local $FS::UID::AutoCommit = 0;
93 $FS::UID::AutoCommit = 0;
95 my $nodepend = driver_name eq 'mysql'
97 : 'AND 0 = ( SELECT COUNT(*) FROM queue_depend'.
98 ' WHERE queue_depend.jobnum = queue.jobnum ) ';
101 { 'status' => 'new' },
103 driver_name eq 'mysql'
104 ? "$nodepend ORDER BY jobnum LIMIT 1 FOR UPDATE"
105 : "$nodepend ORDER BY jobnum FOR UPDATE LIMIT 1"
107 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
108 sleep 5; #connecting to db is expensive
112 if ( driver_name eq 'mysql'
113 && qsearch('queue_depend', { 'jobnum' => $job->jobnum } ) ) {
114 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
115 sleep 5; #would be better if mysql could do everything in query above
119 my %hash = $job->hash;
120 $hash{'status'} = 'locked';
121 my $ljob = new FS::queue ( \%hash );
122 my $error = $ljob->replace($job);
123 die $error if $error;
125 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
127 $FS::UID::AutoCommit = 1;
130 my @args = $ljob->args;
132 defined( my $pid = fork ) or do {
133 warn "WARNING: can't fork: $!\n";
134 my %hash = $job->hash;
135 $hash{'status'} = 'failed';
136 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
137 my $ljob = new FS::queue ( \%hash );
138 my $error = $ljob->replace($job);
139 die $error if $error;
140 next; #don't increment the kid counter
149 $FS::UID::dbh->{InactiveDestroy} = 1;
151 forksuidsetup($user);
153 #auto-use export classes...
154 if ( $ljob->job =~ /(FS::part_export::\w+)::/ ) {
158 warn "job use $class failed";
159 my %hash = $ljob->hash;
160 $hash{'status'} = 'failed';
161 $hash{'statustext'} = $@;
162 my $fjob = new FS::queue( \%hash );
163 my $error = $fjob->replace($ljob);
164 die $error if $error;
169 my $eval = "&". $ljob->job. '(@args);';
170 warn "running $eval";
171 eval $eval; #throw away return value? suppose so
173 warn "job $eval failed";
174 my %hash = $ljob->hash;
175 $hash{'status'} = 'failed';
176 $hash{'statustext'} = $@;
177 my $fjob = new FS::queue( \%hash );
178 my $error = $fjob->replace($ljob);
179 die $error if $error;
190 warn "received TERM signal; exiting\n";
194 warn "received INT signal; exiting\n";
200 die "Usage:\n\n freeside-queued user\n";
205 unlink $pid_file if -e $pid_file;
210 chomp( my $msg = shift );
211 my $log = new IO::File ">>$log_file";
212 flock($log, LOCK_EX);
214 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
215 flock($log, LOCK_UN);
221 chdir "/" or die "Can't chdir to /: $!";
222 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
223 defined(my $pid = fork) or die "Can't fork: $!";
225 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
226 exit unless $pid_file;
227 my $pidfh = new IO::File ">$pid_file" or exit;
228 print $pidfh "$pid\n";
231 #open STDOUT, '>/dev/null'
232 # or die "Can't write to /dev/null: $!";
233 #setsid or die "Can't start a new session: $!";
234 #open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
239 open STDOUT, '>/dev/null'
240 or die "Can't write to /dev/null: $!";
241 setsid or die "Can't start a new session: $!";
242 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
246 foreach my $pid ( keys %kids ) {
247 my $kid = waitpid($pid, WNOHANG);
257 freeside-queued - Job queue daemon
265 Job queue daemon. Should be running at all times.
267 user: from the mapsecrets file - see config.html from the base documentation