rewrite task execution as API instead of a script
parent
ecd9647a2e
commit
7b55df5931
@ -1,113 +1,43 @@
|
|||||||
var Fs = require("fs");
|
|
||||||
var Path = require("path");
|
|
||||||
|
|
||||||
var nThen = require("nthen");
|
var nThen = require("nthen");
|
||||||
|
var Tasks = require("../storage/tasks");
|
||||||
|
var Logger = require("../lib/log");
|
||||||
|
|
||||||
var config = require("../lib/load-config");
|
var config = require("../lib/load-config");
|
||||||
|
|
||||||
var FileStorage = require('../' + config.storage || './storage/file');
|
var FileStorage = require('../' + config.storage || './storage/file');
|
||||||
var root = Path.resolve('../' + config.taskPath || './tasks');
|
|
||||||
|
|
||||||
var dirs;
|
|
||||||
var nt;
|
|
||||||
var store;
|
|
||||||
|
|
||||||
var queue = function (f) {
|
|
||||||
nt = nt.nThen(f);
|
|
||||||
};
|
|
||||||
|
|
||||||
var tryParse = function (s) {
|
nThen(function (w) {
|
||||||
try { return JSON.parse(s); }
|
Logger.create(config, w(function (_log) {
|
||||||
catch (e) { return null; }
|
config.log = _log;
|
||||||
};
|
}));
|
||||||
|
}).nThen(function (w) {
|
||||||
var CURRENT = +new Date();
|
FileStorage.create(config, w(function (_store) {
|
||||||
|
config.store = _store;
|
||||||
var handleTask = function (str, path, cb) {
|
|
||||||
var task = tryParse(str);
|
|
||||||
if (!Array.isArray(task)) {
|
|
||||||
console.error('invalid task: not array');
|
|
||||||
return cb();
|
|
||||||
}
|
|
||||||
if (task.length < 2) {
|
|
||||||
console.error('invalid task: too small');
|
|
||||||
return cb();
|
|
||||||
}
|
|
||||||
|
|
||||||
var time = task[0];
|
|
||||||
var command = task[1];
|
|
||||||
var args = task.slice(2);
|
|
||||||
|
|
||||||
if (time > CURRENT) {
|
// config.taskPath
|
||||||
// not time for this task yet
|
// config.store
|
||||||
console.log('not yet time');
|
// config.filePath
|
||||||
return cb();
|
// config.blobPath
|
||||||
}
|
// config.coldPath
|
||||||
|
|
||||||
nThen(function (waitFor) {
|
// config.enableTaskScheduling
|
||||||
switch (command) {
|
|
||||||
case 'EXPIRE':
|
|
||||||
// FIXME noisy!
|
|
||||||
console.log("expiring: %s", args[0]);
|
|
||||||
store.removeChannel(args[0], waitFor());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// FIXME noisy
|
|
||||||
console.log("unknown command", command);
|
|
||||||
}
|
|
||||||
}).nThen(function () {
|
|
||||||
// remove the task file...
|
|
||||||
Fs.unlink(path, function (err) { // FIXME deletion
|
|
||||||
if (err) { console.error(err); }
|
|
||||||
cb();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
nt = nThen(function (w) {
|
|
||||||
Fs.readdir(root, w(function (e, list) {
|
|
||||||
if (e) { throw e; }
|
|
||||||
dirs = list;
|
|
||||||
if (dirs.length === 0) {
|
|
||||||
w.abort();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}).nThen(function (waitFor) {
|
|
||||||
FileStorage.create(config, waitFor(function (_store) {
|
|
||||||
store = _store;
|
|
||||||
}));
|
}));
|
||||||
}).nThen(function () {
|
}).nThen(function (w) {
|
||||||
dirs.forEach(function (dir, dIdx) {
|
Tasks.create(config, w(function (err, _tasks) {
|
||||||
queue(function (w) {
|
if (err) {
|
||||||
// FIXME noisy!
|
throw err;
|
||||||
console.log('recursing into %s', dir);
|
|
||||||
Fs.readdir(Path.join(root, dir), w(function (e, list) {
|
|
||||||
list.forEach(function (fn) {
|
|
||||||
queue(function (w) {
|
|
||||||
var filePath = Path.join(root, dir, fn);
|
|
||||||
var cb = w();
|
|
||||||
|
|
||||||
// FIXME noisy!
|
|
||||||
console.log("processing file at %s", filePath);
|
|
||||||
Fs.readFile(filePath, 'utf8', function (e, str) {
|
|
||||||
if (e) {
|
|
||||||
console.error(e);
|
|
||||||
return void cb();
|
|
||||||
}
|
}
|
||||||
|
config.tasks = _tasks;
|
||||||
handleTask(str, filePath, cb);
|
}));
|
||||||
});
|
}).nThen(function (w) {
|
||||||
});
|
config.tasks.runAll(w(function (err) {
|
||||||
});
|
if (err) {
|
||||||
if (dIdx === (dirs.length - 1)) {
|
// either TASK_CONCURRENCY
|
||||||
queue(function () {
|
// or an error from tasks.list
|
||||||
store.shutdown();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
}).nThen(function () {
|
||||||
|
config.store.shutdown();
|
||||||
|
config.log.shutdown();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue