Commits

David Hessing  committed 07e7b3a

Abstracted all scheduling. Scheduled tasks are now configured entirely in the payload.

  • Participants
  • Parent commits 71bde36

Comments (0)

Files changed (2)

     }
 });
 
-
+//config.logger.info('config: ', config);
 
 module.exports.config = config;
 
 var logger = config.logger;
 
 
+
+
 var queueTask = function (task, delay) {
     var deferred = Q.defer(),
         t = _.clone(task);
     if (delay) { t.delay = delay; }
+    //if the payload of the task is filtered rename it here
+    if (t.payloadSecret) { t.payload = t.payloadSecret; }
+    
     project.tasks.queue(t, function (err, res) {
         if (err) {
+            logger.error('Failed to queue a task! ', err);
             deferred.reject(err);
         } else {
+            logger.info('Successfully queued task! ID: %s', res.tasks[0].id);
             deferred.resolve(res);
         }
     });
     return deferred;
 };
 
-var getnotes = { code_name: 'lcjob',
-    payload: config.getnotesSecret
-    };
-
-var orderH = { code_name: 'lcjob',
-    payload: config.orderSecret_Hessing
-    };
-
-
-//MAIN LOGIC HERE
-
-//This code works but is super slow
-//var tz = require('timezone');
-//var usTime = tz(require('timezone/America'));
-//var nowPacificTime = new Date(usTime((new Date()).getTime(), '%F %T', 'America/Los_Angeles'));
-//logger.info(nowPacificTime.toTimeString());
-
-
-var now = new Date(),
-    changeDST = new Date('Sun, 03 Nov 2013 09:00:00 GMT'),
-    SFoffset = -7; //currently SF is 7 hours behind GMT
-//Hurray for deferring problems into the future! http://www.timeanddate.com/worldclock/clockchange.html?n=224
-if (now > changeDST) {
-    throw new Error('This code only works until DST changes. Time for a more robust solution!');
-}
-
-//http://blog.lendingclub.com/2013/04/20/a-note-on-our-recent-loan-volume/
-//notes are loaded at 6AM, 10AM, 2PM, and 6PM Pacific Time. In practice they take a few minutes.
-var promises = [];
-if (now.getMinutes() < 3) { //give a little leeway since iron timing isn't precise. This also assumes the scheduler runs every five min.
-    var hours = now.getHours() + SFoffset,
-        i = 0;
-    hours = hours < 0 ? (hours + 24) % 24 : hours % 24;
-    if (hours === 6 || hours === 10 || hours === 14 || hours === 18) {
-        logger.info('The time is ' + now.toTimeString() + '. Queueing multiple getnotes tasks and order tasks...');
-        for (i = 0; i <= 600; i = i + 20) {
-            promises.push(queueTask(getnotes, i));
-            //queue order attempts on the minute
-            if (i % 60 === 0) {
-                promises.push(queueTask(orderH, i));
+
+var processScheduledTasks = function (scheduledTasks) {
+    var promises = [],
+        now = new Date(),
+        nowStr = now.toUTCString();
+    
+    logger.info('The time is now %s. Checking %d scheduled tasks to see what should run now...', nowStr, scheduledTasks.length);
+    _.each(scheduledTasks, function (scheduledTask) {
+        var regex = new RegExp(scheduledTask.schedule),
+            number = Number(scheduledTask.number),
+            interval = Number(scheduledTask.interval),
+            i = 0,
+            exp = null;
+
+        //first check if any tasks are expired. For now just blow everything up if yes.
+        if (scheduledTask.expires) {
+            exp = new Date(scheduledTask.expires);
+            if (now > exp) {
+                throw new Error('The scheduledTask named "' + scheduledTask.name + '" has expired on ' + scheduledTask.expires + '.');
+            }
+        }
+        
+        //TODO: this is a hack, since if the interval is left off or is explicitly set to zero then this does create a (one * nth) second delay for each task.
+        //This will need to be fixed in order to support launching n concurrent tasks.
+        if (!interval || interval < 1) { interval = 1; }
+
+        logger.debug('Now: %s schedRegEx: %s', nowStr, scheduledTask.schedule);
+        if (nowStr.match(regex)) {
+            logger.info('Queueing %d scheduledTask(s) named "%s"...', number, scheduledTask.name);
+            for (i = 0; i < (number * interval); i = i + interval) {
+                promises.push(queueTask(scheduledTask.task, i));
             }
         }
-    }
-} 
-
-//if it's not one of the times when notes are uploaded to LC then just do a regular refresh every 15 min
-if (promises.length === 0 && now.getMinutes() % 15 < 2) { //give a little leeway since iron timing isn't precise. This also assumes the scheduler runs every five min.
-    logger.info('The time is ' + now.toTimeString() + '. Queueing a getnotes task...');
-    //just get latest notes every 15 min
-    promises.push(queueTask(getnotes));
-}
-
-//promises.push(queueTask(getnotes));
-if (promises.length > 0) {
-    logger.info('Promised to queue ' + promises.length + ' tasks...');
-    Q.allSettled(promises)
-        .then(function (results) {
-            results.forEach(function (result) {
-                if (result.state === "fulfilled") {
-                    //logger.info(result.value);
-                } else {
-                    logger.info('Failed to queue a task! ' + result.reason);
-                }
-            });
-        });
-} else {
-    logger.info('The time is ' + now.toTimeString() + '. No tasks to queue now...');
-}
+    });
+
+    logger.info('Processed all matching scheduled tasks and found %d total tasks to queue...', promises.length);
+    return Q.all(promises);
+
+};
+
+
+processScheduledTasks(config.scheduledTasks)
+    .catch(function (err) {
+        logger.error('Unknown error!');
+        if (err) {
+            logger.error(err);
+        }
+    })
+    .done(function () {
+    });
+