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). ') '
56 'extra_sql' => 'FOR UPDATE',
58 'extra_sql' => 'WHERE freesidestatus IS NULL '.
59 ' AND freesiderewritestatus IS NULL '.
61 ' LIMIT 1024', #arbitrary, but don't eat too much memory
65 next if $skip{$cdr->acctid};
70 if ( $conf->exists('cdr-asterisk_forward_rewrite')
71 && $cdr->dstchannel =~ /^Local\/(\d+)/i && $1 ne $cdr->dst
77 warn "dst ". $cdr->dst. " does not match dstchannel $dst ".
78 "(". $cdr->dstchannel. "); rewriting CDR as a forwarded call";
80 $cdr->charged_party($cdr->dst);
84 push @status, 'asterisk_forward';
88 if ( $conf->exists('cdr-charged_party_rewrite') && ! $cdr->charged_party ) {
90 $cdr->set_charged_party;
91 push @status, 'charged_party';
95 if ( $conf->exists('cdr-taqua-accountcode_rewrite')
96 && $cdr->lastapp eq 'acctcode' && $cdr->cdrtypenum == 1
100 #find the matching CDR
101 my $primary = qsearchs('cdr', {
102 'sessionnum' => $cdr->sessionnum,
103 'src' => $cdr->subscriber,
104 #'accountcode' => '',
107 unless ( $primary ) {
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
117 warn "WARNING: $cantfind; will keep trying\n";
118 $accountcode_unmatch{$cdr->acctid} = time;
124 $primary->accountcode( $cdr->lastdata );
125 #$primary->freesiderewritestatus( 'taqua-accountcode-primary' );
126 my $error = $primary->replace;
128 warn "WARNING: error rewriting primary CDR (will retry): $error\n";
131 $skip{$primary->acctid} = 1;
133 push @status, 'taqua-accountcode';
134 $cdr->status('done'); #so it doesn't try to rate
140 $cdr->freesiderewritestatus(
141 scalar(@status) ? join('/', @status) : 'skipped'
144 my $error = $cdr->replace;
147 warn "WARNING: error rewriting CDR (will retry in 30 seconds):".
149 sleep 30; #i dunno, wait and see if the database comes back?
152 last if sigterm() || sigint();
156 myexit() if sigterm() || sigint();
157 #sleep 1 unless $found;
158 sleep 5 unless $found;
165 $conf->exists('cdr-asterisk_forward_rewrite')
166 || $conf->exists('cdr-charged_party_rewrite')
167 || $conf->exists('cdr-taqua-accountcode_rewrite');
171 die "Usage:\n\n freeside-cdrrewrited user\n";
176 freeside-cdrrewrited - Real-time daemon for CDR rewriting
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.