4 use vars qw( $log_file $sigterm $sigint $kids $max_kids );
5 use subs qw( _die _logmsg );
10 use FS::UID qw(adminsuidsetup forksuidsetup driver_name dbh);
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";
68 #prevent runaway forking
69 if ( $kids >= $max_kids ) {
70 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
71 sleep 1; #waiting for signals is cheap
76 my $nodepend = driver_name eq 'mysql'
78 : 'AND 0 = ( SELECT COUNT(*) FROM queue_depend'.
79 ' WHERE queue_depend.jobnum = queue.jobnum ) ';
83 # my $oldAutoCommit = $FS::UID::AutoCommit;
84 # local $FS::UID::AutoCommit = 0;
85 $FS::UID::AutoCommit = 0;
90 { 'status' => 'new' },
92 driver_name eq 'mysql'
93 ? "$nodepend ORDER BY jobnum LIMIT 1 FOR UPDATE"
94 : "$nodepend ORDER BY jobnum FOR UPDATE LIMIT 1"
96 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
97 sleep 5; #connecting to db is expensive
101 if ( driver_name eq 'mysql'
102 && qsearch('queue_depend', { 'jobnum' => $job->jobnum } ) ) {
103 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
104 sleep 5; #would be better if mysql could do everything in query above
108 my %hash = $job->hash;
109 $hash{'status'} = 'locked';
110 my $ljob = new FS::queue ( \%hash );
111 my $error = $ljob->replace($job);
112 die $error if $error;
114 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
116 $FS::UID::AutoCommit = 1;
119 my @args = $ljob->args;
121 defined( my $pid = fork ) or do {
122 warn "WARNING: can't fork: $!\n";
123 my %hash = $job->hash;
124 $hash{'status'} = 'failed';
125 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
126 my $ljob = new FS::queue ( \%hash );
127 my $error = $ljob->replace($job);
128 die $error if $error;
129 next; #don't increment the kid counter
137 $FS::UID::dbh->{InactiveDestroy} = 1;
139 forksuidsetup($user);
141 #auto-use export classes...
142 if ( $ljob->job =~ /(FS::part_export::\w+)::/ ) {
146 warn "job use $class failed";
147 my %hash = $ljob->hash;
148 $hash{'status'} = 'failed';
149 $hash{'statustext'} = $@;
150 my $fjob = new FS::queue( \%hash );
151 my $error = $fjob->replace($ljob);
152 die $error if $error;
157 my $eval = "&". $ljob->job. '(@args);';
158 warn "running $eval";
159 eval $eval; #throw away return value? suppose so
161 warn "job $eval failed";
162 my %hash = $ljob->hash;
163 $hash{'status'} = 'failed';
164 $hash{'statustext'} = $@;
165 my $fjob = new FS::queue( \%hash );
166 my $error = $fjob->replace($ljob);
167 die $error if $error;
178 warn "received TERM signal; exiting\n";
182 warn "received INT signal; exiting\n";
188 die "Usage:\n\n freeside-queued user\n";
193 unlink $pid_file if -e $pid_file;
198 chomp( my $msg = shift );
199 my $log = new IO::File ">>$log_file";
200 flock($log, LOCK_EX);
202 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
203 flock($log, LOCK_UN);
209 chdir "/" or die "Can't chdir to /: $!";
210 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
211 defined(my $pid = fork) or die "Can't fork: $!";
213 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
214 exit unless $pid_file;
215 my $pidfh = new IO::File ">$pid_file" or exit;
216 print $pidfh "$pid\n";
219 #open STDOUT, '>/dev/null'
220 # or die "Can't write to /dev/null: $!";
221 #setsid or die "Can't start a new session: $!";
222 #open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
227 open STDOUT, '>/dev/null'
228 or die "Can't write to /dev/null: $!";
229 setsid or die "Can't start a new session: $!";
230 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
235 freeside-queued - Job queue daemon
243 Job queue daemon. Should be running at all times.
245 user: from the mapsecrets file - see config.html from the base documentation