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 unless $>;
41 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
44 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
48 $SIG{__DIE__} = \&_die;
49 $SIG{__WARN__} = \&_logmsg;
51 warn "freeside-queued starting\n";
56 #prevent runaway forking
57 if ( $kids >= $max_kids ) {
58 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
59 sleep 1; #waiting for signals is cheap
64 my $nodepend = driver_name eq 'mysql'
66 : 'AND 0 = ( SELECT COUNT(*) FROM queue_depend'.
67 ' WHERE queue_depend.jobnum = queue.jobnum ) ';
71 # my $oldAutoCommit = $FS::UID::AutoCommit;
72 # local $FS::UID::AutoCommit = 0;
73 $FS::UID::AutoCommit = 0;
78 { 'status' => 'new' },
80 driver_name eq 'mysql'
81 ? "$nodepend ORDER BY jobnum LIMIT 1 FOR UPDATE"
82 : "$nodepend ORDER BY jobnum FOR UPDATE LIMIT 1"
84 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
85 sleep 5; #connecting to db is expensive
89 if ( driver_name eq 'mysql'
90 && qsearch('queue_depend', { 'jobnum' => $job->jobnum } ) ) {
91 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
92 sleep 5; #would be better if mysql could do everything in query above
96 my %hash = $job->hash;
97 $hash{'status'} = 'locked';
98 my $ljob = new FS::queue ( \%hash );
99 my $error = $ljob->replace($job);
100 die $error if $error;
102 $dbh->commit or die $dbh->errstr; #if $oldAutoCommit;
104 $FS::UID::AutoCommit = 1;
107 my @args = $ljob->args;
109 defined( my $pid = fork ) or do {
110 warn "WARNING: can't fork: $!\n";
111 my %hash = $job->hash;
112 $hash{'status'} = 'failed';
113 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
114 my $ljob = new FS::queue ( \%hash );
115 my $error = $ljob->replace($job);
116 die $error if $error;
117 next; #don't increment the kid counter
125 $FS::UID::dbh->{InactiveDestroy} = 1;
127 forksuidsetup($user);
129 #auto-use export classes...
130 if ( $ljob->job =~ /(FS::part_export::\w+)::/ ) {
134 warn "job use $class failed";
135 my %hash = $ljob->hash;
136 $hash{'status'} = 'failed';
137 $hash{'statustext'} = $@;
138 my $fjob = new FS::queue( \%hash );
139 my $error = $fjob->replace($ljob);
140 die $error if $error;
145 my $eval = "&". $ljob->job. '(@args);';
146 warn "running $eval";
147 eval $eval; #throw away return value? suppose so
149 warn "job $eval failed";
150 my %hash = $ljob->hash;
151 $hash{'status'} = 'failed';
152 $hash{'statustext'} = $@;
153 my $fjob = new FS::queue( \%hash );
154 my $error = $fjob->replace($ljob);
155 die $error if $error;
166 warn "received TERM signal; exiting\n";
170 warn "received INT signal; exiting\n";
176 die "Usage:\n\n freeside-queued user\n";
181 unlink $pid_file if -e $pid_file;
186 chomp( my $msg = shift );
187 my $log = new IO::File ">>$log_file";
188 flock($log, LOCK_EX);
190 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
191 flock($log, LOCK_UN);
197 chdir "/" or die "Can't chdir to /: $!";
198 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
199 defined(my $pid = fork) or die "Can't fork: $!";
201 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
202 exit unless $pid_file;
203 my $pidfh = new IO::File ">$pid_file" or exit;
204 print $pidfh "$pid\n";
207 #open STDOUT, '>/dev/null'
208 # or die "Can't write to /dev/null: $!";
209 #setsid or die "Can't start a new session: $!";
210 #open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
215 open STDOUT, '>/dev/null'
216 or die "Can't write to /dev/null: $!";
217 setsid or die "Can't start a new session: $!";
218 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
223 freeside-queued - Job queue daemon
231 Job queue daemon. Should be running at all times.
233 user: from the mapsecrets file - see config.html from the base documentation