new export! infostreet and sqlradius provisioning switched over
[freeside.git] / FS / bin / freeside-queued
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw( $log_file $sigterm $sigint );
5 use subs qw( _die _logmsg );
6 use Fcntl qw(:flock);
7 use POSIX qw(setsid);
8 use Date::Format;
9 use IO::File;
10 use FS::UID qw(adminsuidsetup forksuidsetup driver_name);
11 use FS::Record qw(qsearchs);
12 use FS::queue;
13
14 # no autoloading just yet
15 use FS::cust_main;
16 use FS::svc_acct;
17 use Net::SSH 0.05;
18 use FS::part_export;
19
20 my $pid_file = '/var/run/freeside-queued.pid';
21
22 my $user = shift or die &usage;
23
24 &daemonize;
25
26 sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; }
27 $SIG{CHLD} =  \&REAPER;
28
29  $sigterm = 0;
30  $sigint = 0;
31 $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $sigint++; };
32 $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $sigterm++; };
33
34 $> = $FS::UID::freeside_uid unless $>;
35 $< = $>;
36 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
37 adminsuidsetup $user;
38
39 $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
40
41 $SIG{__DIE__} = \&_die;
42 $SIG{__WARN__} = \&_logmsg;
43
44 warn "freeside-queued starting\n";
45
46 while (1) {
47
48   my $job = qsearchs(
49     'queue',
50     { 'status' => 'new' },
51     '',
52     driver_name =~ /^mysql$/i
53       ? 'ORDER BY jobnum LIMIT 1 FOR UPDATE'
54       : 'ORDER BY jobnum FOR UPDATE LIMIT 1'
55   ) or do {
56     sleep 5;
57     next;
58   };
59
60   my %hash = $job->hash;
61   $hash{'status'} = 'locked';
62   my $ljob = new FS::queue ( \%hash );
63   my $error = $ljob->replace($job);
64   die $error if $error;
65
66   my @args = $ljob->args;
67
68   # number of children limit?
69   defined( my $pid = fork ) or do {
70     warn "WARNING: can't fork: $!\n";
71     my %hash = $job->hash;
72     $hash{'status'} = 'failed';
73     $hash{'statustext'} = "[freeside-queued] can't fork: $!";
74     my $ljob = new FS::queue ( \%hash );
75     my $error = $ljob->replace($job);
76     die $error if $error;
77   };
78
79   unless ( $pid ) { #kid time
80
81     #get new db handles
82     $FS::UID::dbh->{InactiveDestroy} = 1;
83     $FS::svc_acct::icradius_dbh->{InactiveDestroy} = 1
84       if $FS::svc_acct::icradius_dbh;
85     forksuidsetup($user);
86
87     my $eval = "&". $ljob->job. '(@args);';
88     warn "running $eval";
89     eval $eval; #throw away return value?  suppose so
90     if ( $@ ) {
91       warn "job $eval failed";
92       my %hash = $ljob->hash;
93       $hash{'status'} = 'failed';
94       $hash{'statustext'} = $@;
95       my $fjob = new FS::queue( \%hash );
96       my $error = $fjob->replace($ljob);
97       die $error if $error;
98     } else {
99       $ljob->delete;
100     }
101
102     exit;
103     #end-of-kid
104   }
105
106 } continue {
107   if ( $sigterm ) {
108     warn "received TERM signal; exiting\n";
109     exit;
110   }
111   if ( $sigint ) {
112     warn "received INT signal; exiting\n";
113     exit;
114   }
115 }
116
117 sub usage {
118   die "Usage:\n\n  freeside-queued user\n";
119 }
120
121 sub _die {
122   my $msg = shift;
123   unlink $pid_file if -e $pid_file;
124   _logmsg($msg);
125 }
126
127 sub _logmsg {
128   chomp( my $msg = shift );
129   my $log = new IO::File ">>$log_file";
130   flock($log, LOCK_EX);
131   seek($log, 0, 2);
132   print $log "[". time2str("%a %b %e %T %Y",time). "] [$$] $msg\n";
133   flock($log, LOCK_UN);
134   close $log;
135 }
136
137 sub daemonize {
138
139   chdir "/" or die "Can't chdir to /: $!";
140   open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
141   defined(my $pid = fork) or die "Can't fork: $!";
142   if ( $pid ) {
143     print "freeside-queued started with pid $pid\n"; #logging to $log_file\n";
144     exit unless $pid_file;
145     my $pidfh = new IO::File ">$pid_file" or exit;
146     print $pidfh "$pid\n";
147     exit;
148   }
149   open STDOUT, '>/dev/null'
150                             or die "Can't write to /dev/null: $!";
151   setsid                  or die "Can't start a new session: $!";
152   open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
153
154 }
155
156 =head1 NAME
157
158 freeside-queued - Job queue daemon
159
160 =head1 SYNOPSIS
161
162   freeside-queued user
163
164 =head1 DESCRIPTION
165
166 Job queue daemon.  Should be running at all times.
167
168 user: from the mapsecrets file - see config.html from the base documentation
169
170 =head1 VERSION
171
172 =head1 BUGS
173
174 =head1 SEE ALSO
175
176 =cut
177