4 use vars qw( $log_file $sigterm $sigint );
5 use subs qw( _die _logmsg );
10 use FS::UID qw(adminsuidsetup);
11 use FS::Record qw(qsearchs);
14 # no autoloading just yet
19 my $pid_file = '/var/run/freeside-queued.pid';
21 $SIG{CHLD} = sub { wait }; #zombie prevention
23 my $user = shift or die &usage;
29 $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $sigint++; };
30 $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $sigterm++; };
32 $> = $FS::UID::freeside_uid unless $>;
35 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
37 $SIG{__DIE__} = \&_die;
38 $SIG{__WARN__} = \&_logmsg;
40 warn "freesied-queued starting\n";
46 { 'status' => 'new' },
48 'ORDER BY jobnum FOR UPDATE LIMIT 1'
54 my %hash = $job->hash;
55 $hash{'status'} = 'locked';
56 my $ljob = new FS::queue ( \%hash );
57 my $error = $ljob->replace($job);
60 my @args = $ljob->args;
62 #fork a child for each job (up to some maximum perhaps?)
63 #single-threaded for now.
65 my $eval = "&". $ljob->job. '(@args);';
69 warn "job $eval failed";
70 my %hash = $ljob->hash;
71 $hash{'status'} = 'failed';
72 my $fjob = new FS::queue( \%hash );
73 my $error = $fjob->replace($ljob);
81 warn "received TERM signal; exiting\n";
85 warn "received INT signal; exiting\n";
92 unlink $pid_file if -e $pid_file;
97 chomp( my $msg = shift );
98 my $log = new IO::File ">>$log_file";
101 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
102 flock($log, LOCK_UN);
108 chdir "/" or die "Can't chdir to /: $!";
109 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
110 defined(my $pid = fork) or die "Can't fork: $!";
112 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
113 exit unless $pid_file;
114 my $pidfh = new IO::File ">$pid_file" or exit;
115 print $pidfh "$pid\n";
118 open STDOUT, '>/dev/null'
119 or die "Can't write to /dev/null: $!";
120 setsid or die "Can't start a new session: $!";
121 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
127 freeside-queued - Job queue daemon
135 Job queue daemon. Should be running at all times.
137 user: from the mapsecrets file - see config.html from the base documentation