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
64 { 'status' => 'new' },
66 driver_name =~ /^mysql$/i
67 ? 'ORDER BY jobnum LIMIT 1 FOR UPDATE'
68 : 'ORDER BY jobnum FOR UPDATE LIMIT 1'
70 sleep 5; #connecting to db is expensive
74 my %hash = $job->hash;
75 $hash{'status'} = 'locked';
76 my $ljob = new FS::queue ( \%hash );
77 my $error = $ljob->replace($job);
80 my @args = $ljob->args;
82 defined( my $pid = fork ) or do {
83 warn "WARNING: can't fork: $!\n";
84 my %hash = $job->hash;
85 $hash{'status'} = 'failed';
86 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
87 my $ljob = new FS::queue ( \%hash );
88 my $error = $ljob->replace($job);
90 next; #don't increment the kid counter
98 $FS::UID::dbh->{InactiveDestroy} = 1;
99 $FS::svc_acct::icradius_dbh->{InactiveDestroy} = 1
100 if $FS::svc_acct::icradius_dbh;
101 forksuidsetup($user);
103 #auto-use export classes...
104 if ( $ljob->job =~ /(FS::part_export::\w+)::/ ) {
108 warn "job use $class failed";
109 my %hash = $ljob->hash;
110 $hash{'status'} = 'failed';
111 $hash{'statustext'} = $@;
112 my $fjob = new FS::queue( \%hash );
113 my $error = $fjob->replace($ljob);
114 die $error if $error;
119 my $eval = "&". $ljob->job. '(@args);';
120 warn "running $eval";
121 eval $eval; #throw away return value? suppose so
123 warn "job $eval failed";
124 my %hash = $ljob->hash;
125 $hash{'status'} = 'failed';
126 $hash{'statustext'} = $@;
127 my $fjob = new FS::queue( \%hash );
128 my $error = $fjob->replace($ljob);
129 die $error if $error;
140 warn "received TERM signal; exiting\n";
144 warn "received INT signal; exiting\n";
150 die "Usage:\n\n freeside-queued user\n";
155 unlink $pid_file if -e $pid_file;
160 chomp( my $msg = shift );
161 my $log = new IO::File ">>$log_file";
162 flock($log, LOCK_EX);
164 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
165 flock($log, LOCK_UN);
171 chdir "/" or die "Can't chdir to /: $!";
172 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
173 defined(my $pid = fork) or die "Can't fork: $!";
175 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
176 exit unless $pid_file;
177 my $pidfh = new IO::File ">$pid_file" or exit;
178 print $pidfh "$pid\n";
181 #open STDOUT, '>/dev/null'
182 # or die "Can't write to /dev/null: $!";
183 #setsid or die "Can't start a new session: $!";
184 #open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
189 open STDOUT, '>/dev/null'
190 or die "Can't write to /dev/null: $!";
191 setsid or die "Can't start a new session: $!";
192 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
197 freeside-queued - Job queue daemon
205 Job queue daemon. Should be running at all times.
207 user: from the mapsecrets file - see config.html from the base documentation