Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

some fixes in inbound_socket/outbound_socket

- fix import HTTPRequest in inbound_socket
- post to hangup url in background (spawned)
- fix ringing post
- fix plivo_sched_hangup_id unset
- improve hangup flow in outbound_socket
  • Loading branch information...
commit 98b856676172e3e6c68e29a8b551a40efb5a8a71 1 parent 6218205
@tamiel tamiel authored
View
4 src/plivo/rest/freeswitch/grammar.py
@@ -892,9 +892,9 @@ def run(self, outbound_socket):
outbound_socket.speak(say_args)
event = outbound_socket._action_queue.get()
# Log Speak execute response
- outbound_socket.log.info("Speak finished %s times - (%s)" \
+ outbound_socket.log.info("Speak %s times - (%s)" \
% ((i+1), str(event.get_header('Application-Response'))))
- outbound_socket.log.info("Speak finished")
+ outbound_socket.log.info("Speak Done")
class ScheduleHangup(Grammar):
View
8 src/plivo/rest/freeswitch/inbound_socket.py
@@ -7,9 +7,11 @@
import urllib
import urllib2
+import gevent
from gevent import pool
from plivo.core.freeswitch.inboundsocket import InboundEventSocket
+from plivo.rest.freeswitch.helpers import HTTPRequest
class RESTInboundSocket(InboundEventSocket):
@@ -63,7 +65,7 @@ def on_background_job(self, ev):
self.log.debug("Request: %s cannot be completed as %s"
% (request_uuid, info))
params = {'request_uuid': request_uuid, 'reason': info}
- self.post_to_url(hangup_url, params)
+ gevent.spawn(self.post_to_url, hangup_url, params)
def on_channel_hangup(self, ev):
"""
@@ -113,7 +115,7 @@ def on_channel_state(self, ev):
caller_id = ev['Caller-Caller-ID-Number']
try:
call_state = self.calls_ring_complete[call_uuid]
- except LookupError:
+ except KeyError:
call_state = False
if not call_state:
if to:
@@ -127,7 +129,7 @@ def on_channel_state(self, ev):
"Call Ringing for: %s with request id %s"
% (to, request_uuid))
params = {'to': to, 'request_uuid': request_uuid}
- self.post_to_url(ring_url, params)
+ gevent.spawn(self.post_to_url, ring_url, params)
def hangup_complete(self, request_uuid, call_uuid, reason, ev, hangup_url):
self.log.debug("Call: %s hungup, Reason %s, Request uuid %s"
View
42 src/plivo/rest/freeswitch/outbound_socket.py
@@ -26,6 +26,10 @@
MAX_REDIRECT = 10000
+
+class Hangup(Exception): pass
+
+
class RequestLogger(object):
"""
Class RequestLogger
@@ -95,6 +99,8 @@ def _protocol_send(self, command, args=""):
response = super(PlivoOutboundEventSocket, self)._protocol_send(
command, args)
self.log.debug("Response: %s" % str(response))
+ if self.has_hangup():
+ raise Hangup()
return response
def _protocol_sendmsg(self, name, args=None, uuid="", lock=False, loops=1):
@@ -105,6 +111,8 @@ def _protocol_sendmsg(self, name, args=None, uuid="", lock=False, loops=1):
response = super(PlivoOutboundEventSocket, self)._protocol_sendmsg(
name, args, uuid, lock, loops)
self.log.debug("Response: %s" % str(response))
+ if self.has_hangup():
+ raise Hangup()
return response
# Commands like `playback`, `record` etc. return +OK "immediately".
@@ -139,7 +147,7 @@ def on_channel_hangup(self, event):
if hangup_url:
self.session_params['hangup_cause'] = self._hangup_cause
self.log.info("Posting hangup to %s" % hangup_url)
- self.post_to_url(hangup_url)
+ gevent.spawn(self.post_to_url, hangup_url)
def on_channel_hangup_complete(self, event):
if not self._hangup_cause:
@@ -218,11 +226,22 @@ def run(self):
}
# Remove sched_hangup_id from channel vars
if sched_hangup_id:
- self.unset("sched_hangup_id")
+ self.unset("plivo_sched_hangup_id")
# Run application
- self.log.debug("Processing Call")
- self.process_call()
- self.log.debug("Processing Call Done")
+ self.log.info("Processing Call")
+ try:
+ self.process_call()
+ return
+ except Hangup:
+ self.log.warn("Channel has hung up, breaking Processing Call")
+ except Exception, e:
+ self.log.error("Processing Call Failure !")
+ # If error occurs during xml parsing
+ # log exception and break
+ self.log.error(str(e))
+ [ self.log.error(line) for line in \
+ traceback.format_exc().splitlines() ]
+ self.log.info("Processing Call Ended")
def process_call(self):
"""Method to proceed on the call
@@ -234,8 +253,7 @@ def process_call(self):
for x in range(MAX_REDIRECT):
try:
if self.has_hangup():
- self.log.warn("Channel has hung up, breaking Processing Call")
- return
+ raise Hangup()
self.fetch_xml(params=params, method=fetch_method)
if not self.xml_response:
self.log.warn("No XML Response")
@@ -246,6 +264,8 @@ def process_call(self):
self.log.info("End of RESTXML")
return
except RESTRedirectException, redirect:
+ if self.has_hangup():
+ raise Hangup()
# Set target URL to Redirect URL
# Set method to Redirect method
# Set additional params to Redirect params
@@ -262,14 +282,6 @@ def process_call(self):
% self.target_url)
gevent.sleep(0.010)
continue
- except Exception, e:
- self.log.error("Processing Call Failure !")
- # If error occurs during xml parsing
- # log exception and break
- self.log.error(str(e))
- [ self.log.error(line) for line in \
- traceback.format_exc().splitlines() ]
- return
self.log.warn("Max Redirect Reached !")
def fetch_xml(self, params={}, method='POST'):
Please sign in to comment.
Something went wrong with that request. Please try again.