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 $< = FS::UID::freeside_uid;
41 #freebsd is sofa king broken, won't setuid()
42 $> = $FS::UID::freeside_uid;
44 $> = $FS::UID::freeside_uid;
46 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
49 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
53 $SIG{__DIE__} = \&_die;
54 $SIG{__WARN__} = \&_logmsg;
56 warn "freeside-queued starting\n";
61 #prevent runaway forking
62 if ( $kids >= $max_kids ) {
63 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
64 sleep 1; #waiting for signals is cheap
69 my $nodepend = driver_name eq 'mysql'
71 : 'AND 0 = ( SELECT COUNT(*) FROM queue_depend'.
72 ' WHERE queue_depend.jobnum = queue.jobnum ) ';
76 # my $oldAutoCommit = $FS::UID::AutoCommit;
77 # local $FS::UID::AutoCommit = 0;
78 $FS::UID::AutoCommit = 0;
83 { 'status' => 'new' },
85 driver_name eq 'mysql'
86 ? "$nodepend ORDER BY jobnum LIMIT 1 FOR UPDATE"
87 : "$nodepend ORDER BY jobnum FOR UPDATE LIMIT 1"
89 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
90 sleep 5; #connecting to db is expensive
94 if ( driver_name eq 'mysql'
95 && qsearch('queue_depend', { 'jobnum' => $job->jobnum } ) ) {
96 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
97 sleep 5; #would be better if mysql could do everything in query above
101 my %hash = $job->hash;
102 $hash{'status'} = 'locked';
103 my $ljob = new FS::queue ( \%hash );
104 my $error = $ljob->replace($job);
105 die $error if $error;
107 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
109 $FS::UID::AutoCommit = 1;
112 my @args = $ljob->args;
114 defined( my $pid = fork ) or do {
115 warn "WARNING: can't fork: $!\n";
116 my %hash = $job->hash;
117 $hash{'status'} = 'failed';
118 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
119 my $ljob = new FS::queue ( \%hash );
120 my $error = $ljob->replace($job);
121 die $error if $error;
122 next; #don't increment the kid counter
130 $FS::UID::dbh->{InactiveDestroy} = 1;
132 forksuidsetup($user);
134 #auto-use export classes...
135 if ( $ljob->job =~ /(FS::part_export::\w+)::/ ) {
139 warn "job use $class failed";
140 my %hash = $ljob->hash;
141 $hash{'status'} = 'failed';
142 $hash{'statustext'} = $@;
143 my $fjob = new FS::queue( \%hash );
144 my $error = $fjob->replace($ljob);
145 die $error if $error;
150 my $eval = "&". $ljob->job. '(@args);';
151 warn "running $eval";
152 eval $eval; #throw away return value? suppose so
154 warn "job $eval failed";
155 my %hash = $ljob->hash;
156 $hash{'status'} = 'failed';
157 $hash{'statustext'} = $@;
158 my $fjob = new FS::queue( \%hash );
159 my $error = $fjob->replace($ljob);
160 die $error if $error;
171 warn "received TERM signal; exiting\n";
175 warn "received INT signal; exiting\n";
181 die "Usage:\n\n freeside-queued user\n";
186 unlink $pid_file if -e $pid_file;
191 chomp( my $msg = shift );
192 my $log = new IO::File ">>$log_file";
193 flock($log, LOCK_EX);
195 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
196 flock($log, LOCK_UN);
202 chdir "/" or die "Can't chdir to /: $!";
203 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
204 defined(my $pid = fork) or die "Can't fork: $!";
206 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
207 exit unless $pid_file;
208 my $pidfh = new IO::File ">$pid_file" or exit;
209 print $pidfh "$pid\n";
212 #open STDOUT, '>/dev/null'
213 # or die "Can't write to /dev/null: $!";
214 #setsid or die "Can't start a new session: $!";
215 #open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
220 open STDOUT, '>/dev/null'
221 or die "Can't write to /dev/null: $!";
222 setsid or die "Can't start a new session: $!";
223 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
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