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 $>;
34 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
37 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
39 $SIG{__DIE__} = \&_die;
40 $SIG{__WARN__} = \&_logmsg;
42 warn "freesied-queued starting\n";
48 { 'status' => 'new' },
50 'ORDER BY jobnum FOR UPDATE LIMIT 1'
56 my %hash = $job->hash;
57 $hash{'status'} = 'locked';
58 my $ljob = new FS::queue ( \%hash );
59 my $error = $ljob->replace($job);
62 my @args = $ljob->args;
64 #fork a child for each job (up to some maximum perhaps?)
65 #single-threaded for now.
67 my $eval = "&". $ljob->job. '(@args);';
71 warn "job $eval failed";
72 my %hash = $ljob->hash;
73 $hash{'status'} = 'failed';
74 my $fjob = new FS::queue( \%hash );
75 my $error = $fjob->replace($ljob);
83 warn "received TERM signal; exiting\n";
87 warn "received INT signal; exiting\n";
93 die "Usage:\n\n freeside-queued user\n";
98 unlink $pid_file if -e $pid_file;
103 chomp( my $msg = shift );
104 my $log = new IO::File ">>$log_file";
105 flock($log, LOCK_EX);
107 print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
108 flock($log, LOCK_UN);
114 chdir "/" or die "Can't chdir to /: $!";
115 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
116 defined(my $pid = fork) or die "Can't fork: $!";
118 print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
119 exit unless $pid_file;
120 my $pidfh = new IO::File ">$pid_file" or exit;
121 print $pidfh "$pid\n";
124 open STDOUT, '>/dev/null'
125 or die "Can't write to /dev/null: $!";
126 setsid or die "Can't start a new session: $!";
127 open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
133 freeside-queued - Job queue daemon
141 Job queue daemon. Should be running at all times.
143 user: from the mapsecrets file - see config.html from the base documentation