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
17 my $pid_file = '/var/run/freeside-queued.pid';
19 $SIG{CHLD} = sub { wait }; #zombie prevention
21 my $user = shift or die &usage;
27 $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $sigint++; };
28 $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $sigterm++; };
30 $> = $FS::UID::freeside_uid unless $>;
33 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
35 $SIG{__DIE__} = \&_die;
36 $SIG{__WARN__} = \&_logmsg;
38 warn "freesied-queued starting\n";
44 { 'status' => 'new' },
46 'ORDER BY jobnum FOR UPDATE LIMIT 1'
52 my %hash = $job->hash;
53 $hash{'status'} = 'locked';
54 my $ljob = new FS::queue ( \%hash );
55 my $error = $ljob->replace($job);
58 my @args = $ljob->args;
60 #fork a child for each job (up to some maximum perhaps?)
61 #single-threaded for now.
63 my $eval = "&". $ljob->job. '(@args);';
67 warn "job $eval failed";
68 my $hash = $ljob->hash;
69 $hash{'status'} = 'failed';
70 my $fjob = new FS::queue( \%hash );
71 my $error = $fjob->replace($ljob);
79 warn "received TERM signal; exiting\n";
83 warn "received INT signal; exiting\n";
90 unlink $pid_file if -e $pid_file;
95 chomp( my $msg = shift );
96 my $log = new IO::File ">>$log_file";
99 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
100 flock($log, LOCK_UN);
106 chdir "/" or die "Can't chdir to /: $!";
107 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
108 defined(my $pid = fork) or die "Can't fork: $!";
110 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
111 exit unless $pid_file;
112 my $pidfh = new IO::File ">$pid_file" or exit;
113 print $pidfh "$pid\n";
116 open STDOUT, '>/dev/null'
117 or die "Can't write to /dev/null: $!";
118 setsid or die "Can't start a new session: $!";
119 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
125 freeside-queued - Job queue daemon
133 Job queue daemon. Should be running at all times.
135 user: from the mapsecrets file - see config.html from the base documentation