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 $>;
33 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
36 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
38 $SIG{__DIE__} = \&_die;
39 $SIG{__WARN__} = \&_logmsg;
41 warn "freesied-queued starting\n";
47 { 'status' => 'new' },
49 'ORDER BY jobnum FOR UPDATE LIMIT 1'
55 my %hash = $job->hash;
56 $hash{'status'} = 'locked';
57 my $ljob = new FS::queue ( \%hash );
58 my $error = $ljob->replace($job);
61 my @args = $ljob->args;
63 #fork a child for each job (up to some maximum perhaps?)
64 #single-threaded for now.
66 my $eval = "&". $ljob->job. '(@args);';
70 warn "job $eval failed";
71 my %hash = $ljob->hash;
72 $hash{'status'} = 'failed';
73 my $fjob = new FS::queue( \%hash );
74 my $error = $fjob->replace($ljob);
82 warn "received TERM signal; exiting\n";
86 warn "received INT signal; exiting\n";
92 die "Usage:\n\n freeside-queued user\n";
97 unlink $pid_file if -e $pid_file;
102 chomp( my $msg = shift );
103 my $log = new IO::File ">>$log_file";
104 flock($log, LOCK_EX);
106 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
107 flock($log, LOCK_UN);
113 chdir "/" or die "Can't chdir to /: $!";
114 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
115 defined(my $pid = fork) or die "Can't fork: $!";
117 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
118 exit unless $pid_file;
119 my $pidfh = new IO::File ">$pid_file" or exit;
120 print $pidfh "$pid\n";
123 open STDOUT, '>/dev/null'
124 or die "Can't write to /dev/null: $!";
125 setsid or die "Can't start a new session: $!";
126 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
132 freeside-queued - Job queue daemon
140 Job queue daemon. Should be running at all times.
142 user: from the mapsecrets file - see config.html from the base documentation