You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/03/15 14:39:54 UTC

[GitHub] csantanapr closed pull request #99: Add an optional batch mode

csantanapr closed pull request #99: Add an optional batch mode
URL: https://github.com/apache/incubator-openwhisk-package-cloudant/pull/99
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/provider/lib/constants.js b/provider/lib/constants.js
index 57e6d58..b279256 100644
--- a/provider/lib/constants.js
+++ b/provider/lib/constants.js
@@ -2,10 +2,12 @@ const TRIGGER_DB_SUFFIX = 'cloudanttrigger';
 const DEFAULT_MAX_TRIGGERS = -1;
 const RETRY_ATTEMPTS = 10;
 const RETRY_DELAY = 1000; //in milliseconds
+const BATCH_INTERVAL_MS = 1000;
 
 module.exports = {
     TRIGGER_DB_SUFFIX: TRIGGER_DB_SUFFIX,
     DEFAULT_MAX_TRIGGERS: DEFAULT_MAX_TRIGGERS,
     RETRY_ATTEMPTS: RETRY_ATTEMPTS,
-    RETRY_DELAY: RETRY_DELAY
+    RETRY_DELAY: RETRY_DELAY,
+    BATCH_INTERVAL_MS: BATCH_INTERVAL_MS
 };
diff --git a/provider/lib/utils.js b/provider/lib/utils.js
index 94af602..f58de96 100644
--- a/provider/lib/utils.js
+++ b/provider/lib/utils.js
@@ -63,19 +63,39 @@ module.exports = function(
             dataTrigger.feed = feed;
             that.triggers[dataTrigger.id] = dataTrigger;
 
-            feed.on('change', function (change) {
-                logger.info(method, 'Trigger', dataTrigger.id, 'got change from', dataTrigger.dbname);
+            var batchedChanges = [];
 
+            var fireTrigger = function (payload) {
                 var triggerHandle = that.triggers[dataTrigger.id];
                 if (triggerHandle && (triggerHandle.maxTriggers === -1 || triggerHandle.triggersLeft > 0)) {
                     try {
-                        that.fireTrigger(dataTrigger.id, change);
+                        that.fireTrigger(dataTrigger.id, payload);
                     } catch (e) {
                         logger.error(method, 'Exception occurred while firing trigger', dataTrigger.id,  e);
                     }
                 }
+            };
+
+            feed.on('change', function (change) {
+                logger.info(method, 'Trigger', dataTrigger.id, 'got change from', dataTrigger.dbname);
+
+                if(dataTrigger.batchChanges) {
+                    batchedChanges.push(change);
+                } else {
+                    fireTrigger(change);
+                }
             });
 
+            if(dataTrigger.batchChanges) {
+                var triggerHandle = that.triggers[dataTrigger.id];
+                triggerHandle.interval = setInterval(function() {
+                    if(batchedChanges.length > 0) {
+                        fireTrigger(batchedChanges);
+                        batchedChanges = [];
+                    }
+                }, constants.BATCH_INTERVAL_MS);
+            }
+
             feed.follow();
 
             return new Promise(function(resolve, reject) {
@@ -301,6 +321,11 @@ module.exports = function(
         if (trigger) {
             logger.info(method, 'Stopped cloudant trigger', id, 'listening for changes in database', trigger.dbname);
             trigger.feed.stop();
+
+            if(trigger.interval) {
+                clearInterval(trigger.interval);
+            }
+
             delete that.triggers[id];
         } else {
             logger.info(method, 'trigger', id, 'could not be found in the trigger list.');
@@ -308,14 +333,19 @@ module.exports = function(
         }
     };
 
-    this.fireTrigger = function (id, change) {
+    this.fireTrigger = function (id, changes) {
         var method = 'fireTrigger';
 
         var dataTrigger = that.triggers[id];
         var apikey = dataTrigger.apikey;
         var triggerObj = that.parseQName(dataTrigger.id);
 
-        var form = change;
+        var form; // the trigger payload
+        if(Array.isArray(changes)) {
+            form = { 'changes': changes };
+        } else {
+            form = changes;
+        }
         form.dbname = dataTrigger.dbname;
 
         logger.info(method, 'firing trigger', dataTrigger.id, 'with db update');


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services