fix 'Can't call method "setup" on an undefined value' error when using into rates...
[freeside.git] / FS / bin / freeside-cdrrewrited
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw( $conf );
5 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
6 use FS::UID qw( adminsuidsetup );
7 use FS::Record qw( qsearch qsearchs );
8 #use FS::cdr;
9 #use FS::cust_pkg;
10 #use FS::queue;
11
12 my $user = shift or die &usage;
13
14 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
15 daemonize1('freeside-cdrrewrited');
16
17 drop_root();
18
19 adminsuidsetup($user);
20
21 logfile( "%%%FREESIDE_LOG%%%/cdrrewrited-log.". $FS::UID::datasrc );
22
23 daemonize2();
24
25 $conf = new FS::Conf;
26
27 die "not running; cdr-asterisk_forward_rewrite, cdr-charged_party_rewrite ".
28     " and cdr-taqua-accountcode_rewrite conf options are all off\n"
29   unless _shouldrun();
30
31 #--
32
33 my %accountcode_unmatch = ();
34 my $accountcode_retry = 4 * 60 * 60; # 4 hours
35 my $accountcode_giveup = 4 * 24 * 60 * 60; # 4 days
36
37 while (1) {
38
39   #hmm... don't want to do an expensive search with an ever-growing bunch
40   # of unprocessed CDRs during the month... better to mark them all as
41   # rewritten "skipped", i.e. why we're a daemon in the first place
42   # instead of just doing this search like normal CDRs
43
44   #hmm :/
45   my @recent = grep { ($accountcode_unmatch{$_} + $accountcode_retry) > time }
46                  keys %accountcode_unmatch;
47   my $extra_sql = scalar(@recent)
48                     ? ' AND acctid NOT IN ('. join(',', @recent). ') '
49                     : '';
50
51   my $found = 0;
52   my %skip = ();
53   foreach my $cdr ( 
54     qsearch( {
55       'table'     => 'cdr',
56       'extra_sql' => 'FOR UPDATE',
57       'hashref'   => {},
58       'extra_sql' => 'WHERE freesidestatus IS NULL '.
59                      ' AND freesiderewritestatus IS NULL '.
60                      $extra_sql.
61                      ' LIMIT 1024', #arbitrary, but don't eat too much memory
62     } )
63   ) {
64
65     next if $skip{$cdr->acctid};
66
67     $found = 1;
68     my @status = ();
69
70     if ( $conf->exists('cdr-asterisk_forward_rewrite')
71          && $cdr->dstchannel =~ /^Local\/(\d+)/i && $1 ne $cdr->dst
72        )
73     {
74
75       my $dst = $1;
76
77       warn "dst ". $cdr->dst. " does not match dstchannel $dst ".
78            "(". $cdr->dstchannel. "); rewriting CDR as a forwarded call";
79
80       $cdr->charged_party($cdr->dst);
81       $cdr->dst($dst);
82       $cdr->amaflags(2);
83
84       push @status, 'asterisk_forward';
85
86     }
87
88     if ( $conf->exists('cdr-charged_party_rewrite') && ! $cdr->charged_party ) {
89
90       $cdr->set_charged_party;
91       push @status, 'charged_party';
92
93     }
94
95     if ( $conf->exists('cdr-taqua-accountcode_rewrite')
96          && $cdr->lastapp eq 'acctcode' && $cdr->cdrtypenum == 1
97        )
98     {
99
100       #find the matching CDR
101       my $primary = qsearchs('cdr', {
102         'sessionnum'  => $cdr->sessionnum,
103         'src'         => $cdr->subscriber,
104         #'accountcode' => '',
105       });
106
107       unless ( $primary ) {
108
109         my $cantfind = "can't find primary CDR with session ". $cdr->sessionnum.
110                        ", src ". $cdr->subscriber;
111         if ( $cdr->calldate_unix + $accountcode_giveup < time ) {
112           warn "ERROR: $cantfind; giving up\n";
113           push @status, 'taqua-accountcode-NOTFOUND';
114           $cdr->status('done'); #so it doesn't try to rate
115           delete $accountcode_unmatch{$cdr->acctid}; #so it doesn't suck mem
116         } else {
117           warn "WARNING: $cantfind; will keep trying\n";
118           $accountcode_unmatch{$cdr->acctid} = time;
119           next;
120         }
121
122       } else {
123
124         $primary->accountcode( $cdr->lastdata );
125         #$primary->freesiderewritestatus( 'taqua-accountcode-primary' );
126         my $error = $primary->replace;
127         if ( $error ) {
128           warn "WARNING: error rewriting primary CDR (will retry): $error\n";
129           next;
130         }
131         $skip{$primary->acctid} = 1;
132
133         push @status, 'taqua-accountcode';
134         $cdr->status('done'); #so it doesn't try to rate
135
136       }
137
138     }
139
140     $cdr->freesiderewritestatus(
141       scalar(@status) ? join('/', @status) : 'skipped'
142     );
143
144     my $error = $cdr->replace;
145
146     if ( $error ) {
147       warn "WARNING: error rewriting CDR (will retry in 30 seconds):".
148            " $error\n";
149       sleep 30; #i dunno, wait and see if the database comes back?
150     }
151
152     last if sigterm() || sigint();
153
154   }
155
156   myexit() if sigterm() || sigint();
157   #sleep 1 unless $found;
158   sleep 5 unless $found;
159
160 }
161
162 #--
163
164 sub _shouldrun {
165      $conf->exists('cdr-asterisk_forward_rewrite')
166   || $conf->exists('cdr-charged_party_rewrite')
167   || $conf->exists('cdr-taqua-accountcode_rewrite');
168 }
169
170 sub usage { 
171   die "Usage:\n\n  freeside-cdrrewrited user\n";
172 }
173
174 =head1 NAME
175
176 freeside-cdrrewrited - Real-time daemon for CDR rewriting
177
178 =head1 SYNOPSIS
179
180   freeside-cdrrewrited
181
182 =head1 DESCRIPTION
183
184 Runs continuously, searches for CDRs and does forwarded-call rewriting if any
185 of the "cdr-asterisk_forward_rewrite", "cdr-charged_party_rewrite" or
186 "cdr-taqua-accountcode_rewrite" config options are enabled.
187
188 =head1 SEE ALSO
189
190 =cut
191
192 1;