Change in python/osmo-python-tests[master]: ctrl2cgi: fix deferred callbacks

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Wed Nov 28 12:52:48 UTC 2018


Max has submitted this change and it was merged. ( https://gerrit.osmocom.org/11953 )

Change subject: ctrl2cgi: fix deferred callbacks
......................................................................

ctrl2cgi: fix deferred callbacks

Previously handle_reply() was marked as deferred callback unlike soap.py
where it's synchronous function. This seems to be causing issues where
some of the callbacks are not yield properly. Let's move to the
known-to-work semantics of soap.py where async functions are limited to
Trap() class.

Change-Id: Ib2c28dd7f79cbd28d475de93750703659ddd18f1
Related: SYS#4399
---
M scripts/ctrl2cgi.py
1 file changed, 7 insertions(+), 18 deletions(-)

Approvals:
  Pau Espin Pedrol: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/scripts/ctrl2cgi.py b/scripts/ctrl2cgi.py
index 843aeb9..c566a7c 100755
--- a/scripts/ctrl2cgi.py
+++ b/scripts/ctrl2cgi.py
@@ -22,7 +22,7 @@
  */
 """
 
-__version__ = "0.0.2" # bump this on every non-trivial change
+__version__ = "0.0.3" # bump this on every non-trivial change
 
 from twisted.internet import defer, reactor
 from osmopy.twisted_ipa import CTRL, IPAFactory, __version__ as twisted_ipa_version
@@ -40,25 +40,14 @@
 assert V(twisted_ipa_version) > V('0.4')
 
 
- at defer.inlineCallbacks
 def handle_reply(f, log, resp):
     """
     Reply handler: process raw CGI server response, function f to run for each command
     """
-    #log.debug('HANDLE_REPLY: code=%r' % (resp.code))
-    #for key,val in resp.headers.getAllRawHeaders():
-    #    log.debug('HANDLE_REPLY: key=%r val=%r' % (key, val))
-    if resp.code != 200:
-        resp_body = yield resp.text()
-        log.critical('Received HTTP response %d: %s' % (resp.code, resp_body))
-        return
-
-    parsed = yield resp.json()
-    #log.debug("RESPONSE: %r" % (parsed))
-    bsc_id = parsed.get('commands')[0].split()[0].split('.')[3] # we expect 1st command to have net.0.bsc.666.bts.2.trx.1 location prefix format
-    log.info("Received CGI response for BSC %s with %d commands, error status: %s" % (bsc_id, len(parsed.get('commands')), parsed.get('error')))
-    log.debug("BSC %s commands: %r" % (bsc_id, parsed.get('commands')))
-    for t in parsed.get('commands'): # Process commands format
+    decoded = json.loads(resp.decode('utf-8'))
+    bsc_id = decoded.get('commands')[0].split()[0].split('.')[3] # we expect 1st command to have net.0.bsc.666.bts.2.trx.1 location prefix format
+    log.debug("BSC %s commands: %r" % (bsc_id, decoded.get('commands')))
+    for t in decoded.get('commands'): # Process commands format
         (_, m) = Ctrl().cmd(*t.split())
         f(m)
 
@@ -116,8 +105,8 @@
         params = make_params(bsc, data)
         self.factory.log.debug('location-state@%s.%s.%s.%s (%s) => %s' % (net, bsc, bts, trx, params['time_stamp'], data))
         params['h'] = gen_hash(params, self.factory.secret_key)
-        d = post(self.factory.location, None, params=params)
-        d.addCallback(partial(handle_reply, self.transport.write, self.factory.log)) # treq's collect helper is handy to get all reply content at once using closure on ctx
+        d = post(self.factory.location, params)
+        d.addCallback(collect, partial(handle_reply, self.transport.write, self.factory.log)) # treq's collect helper is handy to get all reply content at once
         d.addErrback(lambda e, bsc: self.factory.log.critical("HTTP POST error %s while trying to register BSC %s on %s" % (e, bsc, self.factory.location)), bsc) # handle HTTP errors
         # Ensure that we run only limited number of requests in parallel:
         yield self.factory.semaphore.acquire()

-- 
To view, visit https://gerrit.osmocom.org/11953
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: python/osmo-python-tests
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ib2c28dd7f79cbd28d475de93750703659ddd18f1
Gerrit-Change-Number: 11953
Gerrit-PatchSet: 4
Gerrit-Owner: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: daniel <dwillmann at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181128/8dd05ee9/attachment.htm>


More information about the gerrit-log mailing list