Callback daemon

From BGNV Wiki

Jump to: navigation, search

[edit] callbackd.pl

This is a modified version of the Perl script posted by Tyler Frederick. It has been modified so that the callback will be placed into the proper queue in able to support multiple queues with a single daemon. You will need to modify the outchan parameter to suit your system.

#!/usr/bin/perl

use strict;
use DBI;
use POSIX;

my $calldir = "/var/spool/asterisk/outgoing";
my $sleepsec = 20; # How often do we check for callbacks?
my $outchan = "IAX2/ROUTING_OUT"; # What channel do we call our customers back on?
my $dbhost = "localhost";
my $dbuser = "acd";
my $dbpass = "acd";
my $dbname = "acd";

##############################################


      $| = 1;  # Flush stdout

      print "Daemonizing...\n";
      &daemonize;

      my $dbh = DBI->connect("dbi:mysql:$dbname:$dbhost","$dbuser","$dbpass")
              or die $DBI::errstr;

       my $qql = "SELECT queuename FROM queuename";
       my $aryRef = $dbh->selectall_arrayref($qql);
       while (1) {  # begin main loop

              sleep($sleepsec);
               foreach my $rowa (@$aryRef ) {       # check each queue
                my ($queue) = @$rowa;
                       my $q1 =        "SELECT uniqueid,callbacknum,queueid FROM callers WHERE queuename='".$queue."' ".
                                        "ORDER BY uniqueid LIMIT 1";
                       my $row = $dbh->selectrow_hashref( $q1 );

                       if (!$row->{callbacknum}) { next; }

                       open CALLFILE, "+>>/tmp/cb$row->{uniqueid}.call";
                       print CALLFILE "Channel: $outchan/$row->{callbacknum}\n";
                       print CALLFILE "MaxRetries: 3\n";
                       print CALLFILE "RetryTime: 60\n";
                       print CALLFILE "WaitTime: 30\n";
                       print CALLFILE "Context: callback\n";
                       print CALLFILE "Extension: s\n";
                       print CALLFILE "Priority: 1\n";
                       print CALLFILE "Set: queueid=$row->{queueid}\n";
                       close CALLFILE;

                       $dbh->do("DELETE FROM callers WHERE uniqueid=$row->{uniqueid}");
                       rename("/tmp/cb$row->{uniqueid}.call", "$calldir/$row->{uniqueid}.call");
               }


        }  # end main loop


################################################

sub daemonize {

  chdir '/'                 or die "Can't chdir to /: $!";
  open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
  open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
  open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
  defined(my $pid = fork)   or die "Can't fork: $!";
  exit if $pid;
  setsid                    or die "Can't start a new session: $!";
  umask 0;

}
Personal tools
user administration