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