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);
11 use FS::Record qw(qsearchs);
14 # no autoloading just yet
20 my $pid_file = '/var/run/freeside-queued.pid';
22 $max_kids = '10'; #guess it should be a config file...
25 my $user = shift or die &usage;
29 sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; $kids--; }
30 $SIG{CHLD} = \&REAPER;
34 $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $sigint++; };
35 $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $sigterm++; };
37 $> = $FS::UID::freeside_uid unless $>;
39 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
42 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
46 $SIG{__DIE__} = \&_die;
47 $SIG{__WARN__} = \&_logmsg;
49 warn "freeside-queued starting\n";
54 #prevent runaway forking
55 if ( $kids >= $max_kids ) {
56 warn "WARNING: maximum $kids children reached\n" unless $warnkids++;
57 sleep 1; #waiting for signals is cheap
62 my $nodepend = 'AND 0 = ( SELECT COUNT(*) FROM queue_depend'.
63 ' WHERE queue_depend.jobnum = queue.jobnum ) ';
67 { 'status' => 'new' },
69 driver_name =~ /^mysql$/i
70 ? "$nodepend ORDER BY jobnum LIMIT 1 FOR UPDATE"
71 : "$nodepend ORDER BY jobnum FOR UPDATE LIMIT 1"
73 sleep 5; #connecting to db is expensive
77 my %hash = $job->hash;
78 $hash{'status'} = 'locked';
79 my $ljob = new FS::queue ( \%hash );
80 my $error = $ljob->replace($job);
83 my @args = $ljob->args;
85 defined( my $pid = fork ) or do {
86 warn "WARNING: can't fork: $!\n";
87 my %hash = $job->hash;
88 $hash{'status'} = 'failed';
89 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
90 my $ljob = new FS::queue ( \%hash );
91 my $error = $ljob->replace($job);
93 next; #don't increment the kid counter
101 $FS::UID::dbh->{InactiveDestroy} = 1;
103 forksuidsetup($user);
105 #auto-use export classes...
106 if ( $ljob->job =~ /(FS::part_export::\w+)::/ ) {
110 warn "job use $class failed";
111 my %hash = $ljob->hash;
112 $hash{'status'} = 'failed';
113 $hash{'statustext'} = $@;
114 my $fjob = new FS::queue( \%hash );
115 my $error = $fjob->replace($ljob);
116 die $error if $error;
121 my $eval = "&". $ljob->job. '(@args);';
122 warn "running $eval";
123 eval $eval; #throw away return value? suppose so
125 warn "job $eval failed";
126 my %hash = $ljob->hash;
127 $hash{'status'} = 'failed';
128 $hash{'statustext'} = $@;
129 my $fjob = new FS::queue( \%hash );
130 my $error = $fjob->replace($ljob);
131 die $error if $error;
142 warn "received TERM signal; exiting\n";
146 warn "received INT signal; exiting\n";
152 die "Usage:\n\n freeside-queued user\n";
157 unlink $pid_file if -e $pid_file;
162 chomp( my $msg = shift );
163 my $log = new IO::File ">>$log_file";
164 flock($log, LOCK_EX);
166 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
167 flock($log, LOCK_UN);
173 chdir "/" or die "Can't chdir to /: $!";
174 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
175 defined(my $pid = fork) or die "Can't fork: $!";
177 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
178 exit unless $pid_file;
179 my $pidfh = new IO::File ">$pid_file" or exit;
180 print $pidfh "$pid\n";
183 #open STDOUT, '>/dev/null'
184 # or die "Can't write to /dev/null: $!";
185 #setsid or die "Can't start a new session: $!";
186 #open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
191 open STDOUT, '>/dev/null'
192 or die "Can't write to /dev/null: $!";
193 setsid or die "Can't start a new session: $!";
194 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
199 freeside-queued - Job queue daemon
207 Job queue daemon. Should be running at all times.
209 user: from the mapsecrets file - see config.html from the base documentation