channeladvisor-localdb
Version:
library that keeps the local inventory database updated
221 lines (220 loc) • 7.55 kB
JavaScript
// Generated by LiveScript 1.3.1
(function(){
var mysql, debug, updaters, queries, models, EventEmitter, Sequelize, ref$, UpdatesUpdateInfo, CatalogUpdateInfo, UpdateStartInfo, UpdateStopInfo, UpdateDoneInfo, UpdateProgressInfo, ErrorInfo, unpromise, CALDB;
mysql = require('mysql');
debug = require('debug');
updaters = require('./updaters');
queries = require('./queries');
models = require('./models');
EventEmitter = require('events').EventEmitter;
Sequelize = require('sequelize');
ref$ = require('./info-objects/'), UpdatesUpdateInfo = ref$.UpdatesUpdateInfo, CatalogUpdateInfo = ref$.CatalogUpdateInfo, UpdateStartInfo = ref$.UpdateStartInfo, UpdateStopInfo = ref$.UpdateStopInfo, UpdateDoneInfo = ref$.UpdateDoneInfo, UpdateProgressInfo = ref$.UpdateProgressInfo, ErrorInfo = ref$.ErrorInfo;
unpromise = function(promise, callback, spread){
var d, fnc;
spread == null && (spread = false);
d = debug('CALDB:unpromise');
d("unpromising function (spread: " + spread + ")");
fnc = spread
? bind$(promise, 'spread')
: bind$(promise, 'then');
return fnc(function(){
d("unpromise call success");
return callback.apply(this, [null].concat(arguments));
}, function(){
d("unpromise call fail");
return callback.apply(this, arguments);
});
};
unpromise.spread = function(promise, callback){
return unpromise.call(this, promise, callback, true);
};
CALDB = (function(superclass){
var prototype = extend$((import$(CALDB, superclass).displayName = 'CALDB', CALDB), superclass).prototype, constructor = CALDB;
function CALDB(arg$){
var d;
this.account = arg$.account, this.dburi = arg$.dburi, this.client = arg$.client, this.logger = arg$.logger;
d = debug('CALDB:construct');
d('constructing');
CALDB.superclass.call(this);
this.initialized = false;
this.resetStats();
this.models = models;
this.unpromise = unpromise;
}
prototype.errout = function(it){
var d;
d = debug("CALDB:errout");
d('sending error');
it.fatal == null && (it.fatal = true);
it.error == null && (it.error = new Error(it.message));
return this.emit('error', new ErrorInfo(it));
};
prototype.resetStats = function(){
var d;
d = debug("CALDB:reset-stats");
d('resetting stats');
return this.stats = {
added: 0,
changed: 0,
deleted: 0
};
};
prototype.start = function(manual, comment, noop){
var d, this$ = this;
d = debug('CALDB:start');
d('starting');
if (this.initialized) {
d("already initialized, quitting");
return;
}
this.initialized = true;
return setTimeout(function(){
return this$.runChecks(function(){
return this$.prepareUpdater(function(){
if (!noop) {
d('not a noop, running updater');
return this$.runUpdater(manual, comment);
} else {
d('noop, emitting ready');
return this$.emit('ready');
}
});
});
});
};
prototype.runChecks = function(callback){
var d, this$ = this;
d = debug('CALDB:run-checks');
d("initializing sequelize");
this.db = new Sequelize(this.dburi, {
logging: false
});
d("authenticating to DB");
return this.unpromise(this.db.authenticate(), function(err){
if (err) {
return this$.errout({
error: err,
message: "Could not connect to db",
stage: "run-checks"
});
}
d("check client init");
if (!this$.client.initialized) {
return this$.errout({
message: "channeladvisor2 client non-existent/non-initialized",
stage: "run-checks"
});
}
return callback();
});
};
prototype.prepareUpdater = function(callback){
var d, this$ = this;
d = debug("CALDB:prepare-updater");
d("defining models");
models.define.call(this);
d("syncing database");
return this.unpromise(this.db.sync({
force: false
}), function(err){
if (err) {
return this$.errout({
error: err,
message: "could not sync sequelize to db",
stage: "prepare-updater"
});
}
return callback();
});
};
prototype.catalogDone = function(){
var d;
d = debug("CALDB:catalog-done");
d("catalog updater done, running updates updater");
return this.runUpdater(false, '');
};
prototype.updatesDone = function(immediate){
var d, timer, this$ = this;
d = debug("CALDB:updates-done");
d("updates updater done, requeuing (immedate: " + immediate + ")");
timer = 1000 * 60 * 10;
if (immediate) {
timer = 0;
}
return setTimeout(function(){
d("starting updates updater");
return this$.runUpdater(false, '');
}, timer);
};
prototype.runUpdater = function(manual, comment){
var d, this$ = this;
d = debug("CALDB:run-updater");
d("determining updater");
if (manual === true) {
d("is forced, calling catalog");
return setTimeout(function(){
return updaters.catalog.call(this$, comment, void 8, manual);
});
}
return this.unpromise(this.models.RunLog.count(), function(err, count){
if (err) {
return this$.errout({
error: err,
message: "could not run database query, at @models.RunLog.count()",
stage: "determine-updater"
});
}
if (count[0] === 0) {
d("nothing in the run log, calling catalog");
return setTimeout(function(){
return updaters.catalog.call(this$, comment);
});
}
return this$.models.RunLog.getIncompleteCatalogRun(function(err, runlog){
if (err) {
return this$.errout({
error: err,
message: "could not run database query, at run-updater,select-incomplete-catalog-run",
stage: "determine-updater"
});
}
if (runlog.length !== 0 && runlog[0].date) {
d("incomplete catalog update, calling catalog");
return setTimeout(function(){
return updaters.catalog.call(this$, comment, runlog[0].date);
});
}
return setTimeout(function(){
d("all checks seem to have passed, calling updates");
return updaters.updates.call(this$, comment);
});
});
});
};
prototype.exposeModels = function(){
return this.models;
};
prototype.exposeSequelize = function(){
return this.db;
};
prototype.stop = function(){
return this._stop = true;
};
return CALDB;
}(EventEmitter));
module.exports = CALDB;
function bind$(obj, key, target){
return function(){ return (target || obj)[key].apply(obj, arguments) };
}
function extend$(sub, sup){
function fun(){} fun.prototype = (sub.superclass = sup).prototype;
(sub.prototype = new fun).constructor = sub;
if (typeof sup.extended == 'function') sup.extended(sub);
return sub;
}
function import$(obj, src){
var own = {}.hasOwnProperty;
for (var key in src) if (own.call(src, key)) obj[key] = src[key];
return obj;
}
}).call(this);