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/12/07 18:03:45 UTC

[GitHub] csantanapr closed pull request #170: support timezone param for cron alarm

csantanapr closed pull request #170: support timezone param for cron alarm
URL: https://github.com/apache/incubator-openwhisk-package-alarms/pull/170
 
 
   

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/.gitignore b/.gitignore
index 7d8681d..c848c58 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,7 @@ node_modules/
 .vscode
 action/*.zip
 action/package.json
+package-lock.json
 
 # Eclipse
 bin/
diff --git a/Dockerfile b/Dockerfile
index 0ce1fc9..9699608 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,4 +1,4 @@
-FROM node:8.12.0
+FROM node:8.14.0
 
 # only package.json
 ADD package.json /
diff --git a/README.md b/README.md
index e73f7ed..b742230 100644
--- a/README.md
+++ b/README.md
@@ -105,6 +105,8 @@ For more information, see: http://crontab.org. The following strings are example
 
 - `trigger_payload` (*optional*): The value of this parameter becomes the content of the Trigger every time the Trigger is fired.
 
+- `timezone` (*optional*): This will modify the actual time relative to the specified timezone. If the timezone is invalid, an error is thrown. You can check all timezones available at the Moment Timezone Website (http://momentjs.com/timezone/docs/#/data-loading/getting-zone-names/).
+
 - `startDate` (*optional*): The date when the Trigger will start running. The Trigger fires based on the schedule specified by the cron parameter.
 
 - `stopDate` (*optional*): The date when the Trigger will stop running. Triggers are no longer fired once this date is reached.
diff --git a/action/alarmWebAction.js b/action/alarmWebAction.js
index aae0259..5d6b8e4 100644
--- a/action/alarmWebAction.js
+++ b/action/alarmWebAction.js
@@ -38,7 +38,8 @@ function main(params) {
             status: {
                 'active': true,
                 'dateChanged': Date.now()
-            }
+            },
+            timezone: params.timezone
         };
 
         if (params.fireOnce) {
@@ -82,14 +83,15 @@ function main(params) {
                 }
 
                 try {
-                    cronHandle = new CronJob(params.cron, function() {});
+                    cronHandle = new CronJob(params.cron, function() {}, undefined, false, params.timezone);
                     //validate cron granularity if 5 fields are allowed instead of 6
                     if (params.limitCronFields && hasSecondsGranularity(params.cron)) {
                         return common.sendError(400, 'cron pattern is limited to 5 fields with 1 minute as the finest granularity');
                     }
                     newTrigger.cron = params.cron;
                 } catch(ex) {
-                    return common.sendError(400, `cron pattern '${params.cron}' is not valid`);
+                    var message = ex.message !== 'Invalid timezone.' ? `cron pattern '${params.cron}' is not valid` : ex.message;
+                    return common.sendError(400, message);
                 }
             }
 
diff --git a/action/alarmWeb_package.json b/action/alarmWeb_package.json
index d0806b9..cd4b6fd 100644
--- a/action/alarmWeb_package.json
+++ b/action/alarmWeb_package.json
@@ -3,6 +3,6 @@
   "version": "1.0.0",
   "main": "alarmWebAction.js",
   "dependencies" : {
-    "cron": "1.4.1"
+    "cron": "1.5.0"
   }
 }
diff --git a/installCatalog.sh b/installCatalog.sh
index 01dff74..c2882a0 100755
--- a/installCatalog.sh
+++ b/installCatalog.sh
@@ -84,7 +84,7 @@ zip -r alarmFeed.zip lib package.json alarm.js
 
 $WSK_CLI -i --apihost "$EDGEHOST" action update --kind "$ACTION_RUNTIME_VERSION" --auth "$AUTH" alarms/alarm "$PACKAGE_HOME/action/alarmFeed.zip" \
      -a description 'Fire trigger when alarm occurs' \
-     -a parameters '[ {"name":"cron", "required":true}, {"name":"startDate", "required":false}, {"name":"stopDate", "required":false} ]' \
+     -a parameters '[ {"name":"cron", "required":true}, {"name":"timezone", "required":false}, {"name":"startDate", "required":false}, {"name":"stopDate", "required":false} ]' \
      -a feed true
 
 $WSK_CLI -i --apihost "$EDGEHOST" action update --kind "$ACTION_RUNTIME_VERSION" --auth "$AUTH" alarms/once "$PACKAGE_HOME/action/alarmFeed.zip" \
diff --git a/package.json b/package.json
index 46c8b87..fdc2d22 100755
--- a/package.json
+++ b/package.json
@@ -6,10 +6,10 @@
   "license": "ISC",
   "dependencies": {
     "body-parser": "^1.15.0",
-    "cron": "1.4.1",
+    "cron": "1.5.0",
     "express": "^4.13.4",
     "lodash": "^4.5.0",
-    "nano": "6.4.2",
+    "nano": "6.4.4",
     "request": "^2.69.0",
     "winston": "^2.1.1",
     "moment": "^2.12.0",
diff --git a/provider/lib/cronAlarm.js b/provider/lib/cronAlarm.js
index b811ce1..6be0d6e 100644
--- a/provider/lib/cronAlarm.js
+++ b/provider/lib/cronAlarm.js
@@ -15,9 +15,11 @@ module.exports = function(logger, newTrigger) {
         namespace: newTrigger.namespace,
         payload: newTrigger.payload,
         cron: newTrigger.cron,
+        timezone: newTrigger.timezone,
         triggerID: newTrigger.triggerID,
         uri: newTrigger.uri,
-        monitor: newTrigger.monitor
+        monitor: newTrigger.monitor,
+        additionalData: newTrigger.additionalData
     };
 
     this.scheduleAlarm = function(triggerIdentifier, callback) {
@@ -26,7 +28,7 @@ module.exports = function(logger, newTrigger) {
         try {
             return new Promise(function(resolve, reject) {
 
-                var cronHandle = new CronJob(newTrigger.cron, callback);
+                var cronHandle = new CronJob(newTrigger.cron, callback, undefined, false, newTrigger.timezone);
 
                 if (newTrigger.stopDate) {
                     cachedTrigger.stopDate = newTrigger.stopDate;
diff --git a/provider/lib/dateAlarm.js b/provider/lib/dateAlarm.js
index 921ebcf..b29d7a9 100644
--- a/provider/lib/dateAlarm.js
+++ b/provider/lib/dateAlarm.js
@@ -14,7 +14,8 @@ module.exports = function(logger, newTrigger) {
         deleteAfterFire: newTrigger.deleteAfterFire,
         triggerID: newTrigger.triggerID,
         uri: newTrigger.uri,
-        monitor: newTrigger.monitor
+        monitor: newTrigger.monitor,
+        additionalData: newTrigger.additionalData
     };
 
     this.scheduleAlarm = function(triggerIdentifier, callback) {
diff --git a/provider/lib/intervalAlarm.js b/provider/lib/intervalAlarm.js
index 34d2cf9..6760bcd 100644
--- a/provider/lib/intervalAlarm.js
+++ b/provider/lib/intervalAlarm.js
@@ -14,7 +14,8 @@ module.exports = function(logger, newTrigger) {
         minutes: newTrigger.minutes,
         triggerID: newTrigger.triggerID,
         uri: newTrigger.uri,
-        monitor: newTrigger.monitor
+        monitor: newTrigger.monitor,
+        additionalData: newTrigger.additionalData
     };
 
     this.scheduleAlarm = function(triggerIdentifier, callback) {
diff --git a/provider/lib/utils.js b/provider/lib/utils.js
index b62f31b..d70881b 100644
--- a/provider/lib/utils.js
+++ b/provider/lib/utils.js
@@ -100,11 +100,18 @@ module.exports = function(logger, triggerDB, redisClient) {
                 json: triggerData.payload
             }, function(error, response) {
                 try {
+                    var statusCode;
+                    if (!error) {
+                        statusCode = response.statusCode;
+                    }
+                    else if (error.statusCode) {
+                        statusCode = error.statusCode;
+                    }
                     var triggerIdentifier = triggerData.triggerID;
-                    logger.info(method, triggerIdentifier, 'http post request, STATUS:', response ? response.statusCode : undefined);
+                    logger.info(method, triggerIdentifier, 'http post request, STATUS:', statusCode);
 
-                    if (error || response.statusCode >= 400) {
-                        logger.error(method, 'there was an error invoking', triggerIdentifier, response ? response.statusCode : error);
+                    if (error || statusCode >= 400) {
+                        logger.error(method, 'there was an error invoking', triggerIdentifier, statusCode || error);
                         var throttleCounter = throttleCount || 0;
 
                         // only manage trigger fires if they are not infinite
@@ -112,15 +119,15 @@ module.exports = function(logger, triggerDB, redisClient) {
                             triggerData.triggersLeft++;
                         }
 
-                        if (!error && shouldDisableTrigger(response.statusCode)) {
+                        if (!error && shouldDisableTrigger(statusCode)) {
                             //disable trigger
-                            var message = 'Automatically disabled after receiving a ' + response.statusCode + ' status code when firing the trigger';
-                            disableTrigger(triggerIdentifier, response.statusCode, message);
-                            reject('Disabled trigger ' + triggerIdentifier + ' due to status code: ' + response.statusCode);
+                            var message = 'Automatically disabled after receiving a ' + statusCode + ' status code when firing the trigger';
+                            disableTrigger(triggerIdentifier, statusCode, message);
+                            reject('Disabled trigger ' + triggerIdentifier + ' due to status code: ' + statusCode);
                         }
                         else {
                             if (retryCount < retryAttempts) {
-                                throttleCounter = response && response.statusCode === HttpStatus.TOO_MANY_REQUESTS ? throttleCounter + 1 : throttleCounter;
+                                throttleCounter = statusCode === HttpStatus.TOO_MANY_REQUESTS ? throttleCounter + 1 : throttleCounter;
                                 logger.info(method, 'attempting to fire trigger again', triggerIdentifier, 'Retry Count:', (retryCount + 1));
                                 setTimeout(function () {
                                     postTrigger(triggerData, (retryCount + 1), throttleCounter)
@@ -289,8 +296,7 @@ module.exports = function(logger, triggerDB, redisClient) {
                         var uri = self.uriHost + '/api/v1/namespaces/' + namespace + '/triggers/' + name;
 
                         logger.info(method, 'Checking if trigger', triggerIdentifier, 'still exists');
-                        var cachedTrigger = self.triggers[triggerIdentifier] = {apikey: doc.apikey, additionalData: doc.additionalData};
-                        self.authRequest(cachedTrigger, {
+                        self.authRequest(doc, {
                             method: 'get',
                             url: uri
                         }, function (error, response) {
@@ -302,8 +308,8 @@ module.exports = function(logger, triggerDB, redisClient) {
                             }
                             else {
                                 createTrigger(triggerIdentifier, doc)
-                                .then(newTrigger => {
-                                    Object.assign(cachedTrigger, newTrigger);
+                                .then(cachedTrigger => {
+                                    self.triggers[triggerIdentifier] = cachedTrigger;
                                     logger.info(method, triggerIdentifier, 'created successfully');
                                     if (cachedTrigger.intervalHandle && shouldFireTrigger(cachedTrigger)) {
                                         try {
@@ -358,7 +364,6 @@ module.exports = function(logger, triggerDB, redisClient) {
                     if ((!doc.status || doc.status.active === true) && (!doc.monitor || doc.monitor === self.host)) {
                         createTrigger(triggerIdentifier, doc)
                         .then(cachedTrigger => {
-                            cachedTrigger.additionalData = doc.additionalData;
                             self.triggers[triggerIdentifier] = cachedTrigger;
                             logger.info(method, triggerIdentifier, 'created successfully');
 


 

----------------------------------------------------------------
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