Katie Horn has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/92918


Change subject: Make the queue server connection reusable As it currently 
stands, making a new connection for every message is blowing up the only job 
that uses sendSTOMP, besides being crazy-inefficient. That has to stop.
......................................................................

Make the queue server connection reusable
As it currently stands, making a new connection for every message is blowing up
the only job that uses sendSTOMP, besides being crazy-inefficient.
That has to stop.

Change-Id: Idce243689b9e534c93193921b64f464d0dd84e12
---
M sites/all/modules/globalcollect_audit/globalcollect_audit.module
M sites/all/modules/queue2civicrm/queue2civicrm_stomp.inc
2 files changed, 43 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/18/92918/1

diff --git a/sites/all/modules/globalcollect_audit/globalcollect_audit.module 
b/sites/all/modules/globalcollect_audit/globalcollect_audit.module
index 879cc81..b59b389 100644
--- a/sites/all/modules/globalcollect_audit/globalcollect_audit.module
+++ b/sites/all/modules/globalcollect_audit/globalcollect_audit.module
@@ -415,6 +415,9 @@
                        }
                }
        }
+    
+    //done with the quere server, so...
+    closeStompConnection();
        
        if ( count( $errorful_transactions ) ){
                globalcollect_audit_echo("Errorful Transactions: " . print_r( 
$errorful_transactions, true ));
diff --git a/sites/all/modules/queue2civicrm/queue2civicrm_stomp.inc 
b/sites/all/modules/queue2civicrm/queue2civicrm_stomp.inc
index 03df997..6536b6b 100644
--- a/sites/all/modules/queue2civicrm/queue2civicrm_stomp.inc
+++ b/sites/all/modules/queue2civicrm/queue2civicrm_stomp.inc
@@ -16,7 +16,6 @@
                $initial_msg = false;
        }
 
-       $stompServer = variable_get('queue2civicrm_url', 
'tcp://localhost:61613');
        if ( $initial_msg ){
                $stompQueue = variable_get('queue2civicrm_subscription', 
'/queue/test');
        } else {
@@ -26,13 +25,7 @@
        $message = json_encode( createQueueMessage( $transaction ) );
        
        if ( $message ){
-
-               // make a connection
-               $con = new Stomp( $stompServer );
-
-               // connect
-               $con->connect();
-
+        $con = getStompConnection();
                // send a message to the queue
                $result = $con->send( "$stompQueue", $message, array( 
'persistent' => 'true' ) );
 
@@ -45,7 +38,6 @@
                        $ret = true;
                }
 
-               $con->disconnect();
        } else {
                watchdog('queue2civicrm', 'createQueueMessage failed for : ' . 
$message, array(), WATCHDOG_ERROR);
                $ret = false;
@@ -55,6 +47,45 @@
 }
 
 /**
+ * getStompConnection gets (or closes) a reusable connection to the queue 
server.
+ * Doing it this way because a static var feels much nicer than a global.
+ * @staticvar Stomp|NULL $con The reusable queue server connection, or null
+ * @param bool $connect If we're after a connected connection, or a 
disconnected one.
+ * @return \Stomp|NULL The active connection, or NULL if it's closed
+ */
+function getStompConnection( $connect = true ){
+    static $con = NULL;
+    if ( $connect ){
+        if ( is_null( $con ) ){
+            //no open connection to reuse. Make a new one.
+            $stompServer = variable_get('queue2civicrm_url', 
'tcp://localhost:61613');
+
+            $con = new Stomp( $stompServer );
+        }
+
+        if ( !$con->isConnected() ){
+            $con->connect();
+        }
+        
+    } else { //disconnect
+        if ( !is_null( $con ) ){
+            $con->disconnect();
+            $con = NULL;
+        }
+    }
+    
+    return $con;
+}
+
+/**
+ * Purely for readibility.
+ * Uses getStompConnection( false ) to close the reusable queue server 
connection.
+ */
+function closeStompConnection(){
+    getStompConnection( false );
+}
+
+/**
  * Confirms that all fields expected by queue2civicrm are present before
  * a message is allowed to be placed into the Stomp queue.  Sets all of the
  * fields sent by Extension:DonationInterface to an empty string if not

-- 
To view, visit https://gerrit.wikimedia.org/r/92918
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idce243689b9e534c93193921b64f464d0dd84e12
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Katie Horn <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to