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 );
12 my $user = shift or die &usage;
14 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
15 daemonize1('freeside-cdrrewrited');
19 adminsuidsetup($user);
21 logfile( "%%%FREESIDE_LOG%%%/cdrrewrited-log.". $FS::UID::datasrc );
27 die "not running; cdr-asterisk_forward_rewrite, cdr-charged_party_rewrite ".
28 " and cdr-taqua-accountcode_rewrite conf options are all off\n"
33 my %accountcode_unmatch = ();
34 my $accountcode_retry = 4 * 60 * 60; # 4 hours
35 my $accountcode_giveup = 4 * 24 * 60 * 60; # 4 days
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
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). ') '
55 'extra_sql' => 'FOR UPDATE',
57 'extra_sql' => 'WHERE freesidestatus IS NULL '.
58 ' AND freesiderewritestatus IS NULL '.
60 ' LIMIT 1024', #arbitrary, but don't eat too much memory
67 if ( $conf->exists('cdr-asterisk_forward_rewrite')
68 && $cdr->dstchannel =~ /^Local\/(\d+)/i && $1 ne $cdr->dst
74 warn "dst ". $cdr->dst. " does not match dstchannel $dst ".
75 "(". $cdr->dstchannel. "); rewriting CDR as a forwarded call";
77 $cdr->charged_party($cdr->dst);
81 push @status, 'asterisk_forward';
85 if ( $conf->exists('cdr-charged_party_rewrite') && ! $cdr->charged_party ) {
87 $cdr->set_charged_party;
88 push @status, 'charged_party';
92 if ( $conf->exists('cdr-taqua-accountcode_rewrite')
93 && $cdr->lastapp eq 'acctcode' && $cdr->cdrtypenum == 1
97 #find the matching CDR
98 my $primary = qsearchs('cdr', {
99 'sessionnum' => $cdr->sessionnum,
100 'src' => $cdr->subscriber,
101 #'accountcode' => '',
104 unless ( $primary ) {
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 warn" ERROR: $cantfind; giving up\n";
110 push @status, 'taqua-accountcode-NOTFOUND';
111 $cdr->status('done'); #so it doesn't try to rate
112 delete $accountcode_unmatch($cdr->acctid}; #so it doesn't suck mem
114 warn "WARNING: $cantfind; will keep trying\n";
115 $accountcode_unmatch{$cdr->acctid} = time;
121 $primary->accountcode( $cdr->lastdata );
122 #$primary->freesiderewritestatus( 'taqua-accountcode-primary' );
123 my $error = $primary->replace;
125 warn "WARNING: error rewriting primary CDR (will retry): $error\n";
129 push @status, 'taqua-accountcode';
130 $cdr->status('done'); #so it doesn't try to rate
136 $cdr->freesiderewritestatus(
137 scalar(@status) ? join('/', @status) : 'skipped'
140 my $error = $cdr->replace;
143 warn "WARNING: error rewriting CDR (will retry in 30 seconds):".
145 sleep 30; #i dunno, wait and see if the database comes back?
148 last if sigterm() || sigint();
152 myexit() if sigterm() || sigint();
153 #sleep 1 unless $found;
154 sleep 5 unless $found;
161 $conf->exists('cdr-asterisk_forward_rewrite')
162 || $conf->exists('cdr-charged_party_rewrite')
163 || $conf->exists('cdr-taqua-accountcode_rewrite');
167 die "Usage:\n\n freeside-cdrrewrited user\n";
172 freeside-cdrrewrited - Real-time daemon for CDR rewriting
180 Runs continuously, searches for CDRs and does forwarded-call rewriting if any
181 of the "cdr-asterisk_forward_rewrite", "cdr-charged_party_rewrite" or
182 "cdr-taqua-accountcode_rewrite" config options are enabled.