better retry behavior for non-found taqua CDR rewrites, RT#12181
[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   my @recent = grep { ($accountcode_unmatch{$_} + $accountcode_retry) < time }
45                  keys %accountcode_unmatch;
46   #hmm :/
47   my $extra_sql = '';
48   if ( @recent ) {
49     $extra_sql .= ' AND '.
50       join(' AND ',
51                     map { my($sessionnum,$src) = split(',', $_);
52                           "( sessionnum != '$sessionnum' OR src != '$src' )";
53                         }
54                       @recent
55           );
56   }
57
58   my $found = 0;
59   foreach my $cdr ( 
60     qsearch( {
61       'table'     => 'cdr',
62       'extra_sql' => 'FOR UPDATE',
63       'hashref'   => {},
64       'extra_sql' => 'WHERE freesidestatus IS NULL '.
65                      ' AND freesiderewritestatus IS NULL '.
66                      $extra_sql.
67                      ' LIMIT 1024', #arbitrary, but don't eat too much memory
68     } )
69   ) {
70
71     $found = 1;
72     my @status = ();
73
74     if ( $conf->exists('cdr-asterisk_forward_rewrite')
75          && $cdr->dstchannel =~ /^Local\/(\d+)/i && $1 ne $cdr->dst
76        )
77     {
78
79       my $dst = $1;
80
81       warn "dst ". $cdr->dst. " does not match dstchannel $dst ".
82            "(". $cdr->dstchannel. "); rewriting CDR as a forwarded call";
83
84       $cdr->charged_party($cdr->dst);
85       $cdr->dst($dst);
86       $cdr->amaflags(2);
87
88       push @status, 'asterisk_forward';
89
90     }
91
92     if ( $conf->exists('cdr-charged_party_rewrite') && ! $cdr->charged_party ) {
93
94       $cdr->set_charged_party;
95       push @status, 'charged_party';
96
97     }
98
99     if ( $conf->exists('cdr-taqua-accountcode_rewrite')
100          && $cdr->lastapp eq 'acctcode' && $cdr->cdrtypenum == 1
101        )
102     {
103
104       #find the matching CDR
105       my $primary = qsearchs('cdr', {
106         'sessionnum'  => $cdr->sessionnum,
107         'src'         => $cdr->subscriber,
108         #'accountcode' => '',
109       });
110
111       unless ( $primary ) {
112
113         my $cantfind = "can't find primary CDR with session ". $cdr->sessionnum.
114                        ", src ". $cdr->subscriber;
115         if ( $cdr->calldate_unix + $accountcode_giveup < time ) {
116           push @status, 'taqua-accountcode-NOTFOUND';
117           $cdr->status('done'); #so it doesn't try to rate
118         } else {
119           warn "WARNING: $cantfind; will keep trying\n";
120           $accountcode_unmatch{$cdr->sessionnum.','.$cdr->subscriber} = time;
121           next;
122         }
123
124       } else {
125
126         $primary->accountcode( $cdr->lastdata );
127         #$primary->freesiderewritestatus( 'taqua-accountcode-primary' );
128         my $error = $primary->replace;
129         if ( $error ) {
130           warn "WARNING: error rewriting primary CDR (will retry): $error\n";
131           next;
132         }
133
134         push @status, 'taqua-accountcode';
135         $cdr->status('done'); #so it doesn't try to rate
136
137       }
138
139     }
140
141     $cdr->freesiderewritestatus(
142       scalar(@status) ? join('/', @status) : 'skipped'
143     );
144
145     my $error = $cdr->replace;
146
147     if ( $error ) {
148       warn "WARNING: error rewriting CDR (will retry in 30 seconds):".
149            " $error\n";
150       sleep 30; #i dunno, wait and see if the database comes back?
151     }
152
153     last if sigterm() || sigint();
154
155   }
156
157   myexit() if sigterm() || sigint();
158   #sleep 1 unless $found;
159   sleep 5 unless $found;
160
161 }
162
163 #--
164
165 sub _shouldrun {
166      $conf->exists('cdr-asterisk_forward_rewrite')
167   || $conf->exists('cdr-charged_party_rewrite')
168   || $conf->exists('cdr-taqua-accountcode_rewrite');
169 }
170
171 sub usage { 
172   die "Usage:\n\n  freeside-cdrrewrited user\n";
173 }
174
175 =head1 NAME
176
177 freeside-cdrrewrited - Real-time daemon for CDR rewriting
178
179 =head1 SYNOPSIS
180
181   freeside-cdrrewrited
182
183 =head1 DESCRIPTION
184
185 Runs continuously, searches for CDRs and does forwarded-call rewriting if any
186 of the "cdr-asterisk_forward_rewrite", "cdr-charged_party_rewrite" or
187 "cdr-taqua-accountcode_rewrite" config options are enabled.
188
189 =head1 SEE ALSO
190
191 =cut
192
193 1;