4 use vars qw( $log_file $sigterm $sigint );
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 my $user = shift or die &usage;
26 sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; }
27 $SIG{CHLD} = \&REAPER;
31 $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $sigint++; };
32 $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $sigterm++; };
34 $> = $FS::UID::freeside_uid unless $>;
36 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
39 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
43 $SIG{__DIE__} = \&_die;
44 $SIG{__WARN__} = \&_logmsg;
46 warn "freeside-queued starting\n";
52 { 'status' => 'new' },
54 driver_name =~ /^mysql$/i
55 ? 'ORDER BY jobnum LIMIT 1 FOR UPDATE'
56 : 'ORDER BY jobnum FOR UPDATE LIMIT 1'
62 my %hash = $job->hash;
63 $hash{'status'} = 'locked';
64 my $ljob = new FS::queue ( \%hash );
65 my $error = $ljob->replace($job);
68 my @args = $ljob->args;
70 # number of children limit?
71 defined( my $pid = fork ) or do {
72 warn "WARNING: can't fork: $!\n";
73 my %hash = $job->hash;
74 $hash{'status'} = 'failed';
75 $hash{'statustext'} = "[freeside-queued] can't fork: $!";
76 my $ljob = new FS::queue ( \%hash );
77 my $error = $ljob->replace($job);
81 unless ( $pid ) { #kid time
84 $FS::UID::dbh->{InactiveDestroy} = 1;
85 $FS::svc_acct::icradius_dbh->{InactiveDestroy} = 1
86 if $FS::svc_acct::icradius_dbh;
89 my $eval = "&". $ljob->job. '(@args);';
91 eval $eval; #throw away return value? suppose so
93 warn "job $eval failed";
94 my %hash = $ljob->hash;
95 $hash{'status'} = 'failed';
96 $hash{'statustext'} = $@;
97 my $fjob = new FS::queue( \%hash );
98 my $error = $fjob->replace($ljob);
110 warn "received TERM signal; exiting\n";
114 warn "received INT signal; exiting\n";
120 die "Usage:\n\n freeside-queued user\n";
125 unlink $pid_file if -e $pid_file;
130 chomp( my $msg = shift );
131 my $log = new IO::File ">>$log_file";
132 flock($log, LOCK_EX);
134 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
135 flock($log, LOCK_UN);
141 chdir "/" or die "Can't chdir to /: $!";
142 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
143 defined(my $pid = fork) or die "Can't fork: $!";
145 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
146 exit unless $pid_file;
147 my $pidfh = new IO::File ">$pid_file" or exit;
148 print $pidfh "$pid\n";
151 #open STDOUT, '>/dev/null'
152 # or die "Can't write to /dev/null: $!";
153 #setsid or die "Can't start a new session: $!";
154 #open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
159 open STDOUT, '>/dev/null'
160 or die "Can't write to /dev/null: $!";
161 setsid or die "Can't start a new session: $!";
162 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
167 freeside-queued - Job queue daemon
175 Job queue daemon. Should be running at all times.
177 user: from the mapsecrets file - see config.html from the base documentation