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