Commits

David Hessing committed 265689f

Initial working version.

Comments (0)

Files changed (5)

+*.log
+*.csv
+*.BAK
+*.bat
+*.zip
+*.payload
+iron.json
+node_modules/
+localconfig.js
+manifest.yml
+'use strict';
+require('JSON');
+var fs = require('fs');
+var winston = require('winston');
+var crypto = require('crypto');
+var _ = require('underscore');
+var config = {};
+
+
+
+config.logInit = function (level) {
+    level = level || 'info';
+	var trnsprts = [
+        new (winston.transports.Console)({ json: false, colorize: true, prettyPrint: true, level: level}),
+        new (winston.transports.File)({ filename: 'iron_scheduler.log', level: level }) ];
+	this.logger = new (winston.Logger)({ transports: trnsprts });
+};
+config.logInit();
+
+//load the payload values
+process.argv.forEach(function (val, index, array) {
+    var payload = '';
+    if (val === '-payload') { 
+        payload = fs.readFileSync(process.argv[index + 1], 'ascii');
+        _.extend(config, JSON.parse(payload));
+    }
+});
+
+
+
+module.exports.config = config;
+
+'use strict';
+var config = require('./config').config;
+var ironio = require('node-ironio')(config.ironOauthToken);
+var project = ironio.projects(config.ironProjectId);
+var Q = require('q');
+var _ = require('underscore');
+
+var logger = config.logger;
+
+
+var queueTask = function (task, delay) {
+    var deferred = Q.defer(),
+        t = _.clone(task);
+    if (delay) { t.delay = delay; }
+    project.tasks.queue(t, function (err, res) {
+        if (err) {
+            deferred.reject(err);
+        } else {
+            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));
+            }
+        }
+    }
+} else if (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...');
+}
+
+
+
+
+

iron_scheduler.worker

+runtime "node"
+exec "index.js"
+dir "node_modules"
+file "package.json"
+{
+  "name": "ironio_scheduler",
+  "version": "0.0.1",
+  "description": "Scheduling ironio actions",
+  "main": "index.js",
+  "directories": {
+    "test": "test"
+  },
+  "dependencies": {
+    "q": "~0.9.6",
+    "winston": "~0.7.1",
+    "node-ironio": "~0.3.1",
+    "underscore": "~1.5.1",
+    "JSON": "~1.0.0"
+  },
+  "devDependencies": {},
+  "scripts": {
+    "start": "node index"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git@bitbucket.org:delemach/iron_scheduler.git"
+  },
+  "keywords": [
+    "ironio"
+  ],
+  "author": "David Hessing",
+  "readmeFilename": "README.md"
+}