Skip to content

Commit

Permalink
Merge pull request #26 from prey/local_db
Browse files Browse the repository at this point in the history
Store/restore running commands on shutdown/boot.
  • Loading branch information
tomas committed Oct 21, 2013
2 parents 288ce06 + 3e46174 commit d36e55d
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 6 deletions.
16 changes: 15 additions & 1 deletion lib/agent/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ var start = function(type, name, opts, cb){
}

logger.info('Starting ' + type + ': '+ name);
loader.load(type, name, function(err, action){
loader.load(type, name, function(err, action) {
if (err) return hooks.trigger('error', err);

action.type = type;
action.options = typeof opts == 'function' ? {} : opts;

action.start(opts, function(err, emitter) {
cb && cb(err);

Expand Down Expand Up @@ -89,4 +92,15 @@ actions.stop_all = function() {
this.stop(name);
}

actions.running = function() {
var list = [];
for (var key in running) {
if (running[key].type == 'action') {
var obj = { name: key, options: running[key].options || {} }
list.push(obj);
}
}
return list;
}

module.exports = actions;
48 changes: 48 additions & 0 deletions lib/agent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var common = require('./common'),
reports = require('./reports'),
triggers = require('./triggers'),
connection = require('./connection'),
storage = require('./storage'),
exceptions = require('./exceptions');

var config = common.config,
Expand Down Expand Up @@ -96,6 +97,8 @@ var boot = function() {
if (err) handle_error(err);

connection.watch();
run_stored_commands();

logger.info('Initialized.');
});
});
Expand Down Expand Up @@ -238,11 +241,56 @@ var wait_for_config = function() {
}, 10000);
}

////////////////////////////////////////////////////////////////////
// command persistence
////////////////////////////////////////////////////////////////////

run_stored_commands = function(cb) {
storage.all(function(err, commands) {
if (err) return;

for (var key in commands)
perform_command(commands[key]);
})
storage.clear();
}

store_running_commands = function(cb) {
var running_actions = actions.running(),
running_reports = reports.running(),
count = running_actions.length + running_reports.length;

if (count == 0)
return cb && cb()

logger.info(count + ' active actions/reports.')

var done = function() {
--count || (cb && cb())
}

var store = function(type, name, opts) {
var key = [type, name].join('-');
storage.set(key, { command: type, name: name, options: opts }, done)
}

running_actions.forEach(function(action) {
store('action', action.name, action.options)
})

running_reports.forEach(function(report) {
store('report', report.name, report.options)
})
}

////////////////////////////////////////////////////////////////////
// shutdown
////////////////////////////////////////////////////////////////////

var shutdown = function() {
logger.debug('Saving running commands...')
store_running_commands();

logger.debug('Unloading drivers.');
unload_drivers();

Expand Down
23 changes: 18 additions & 5 deletions lib/agent/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,25 @@ var get = function(report_name, options, callback) {
gather(report_name, list, cb); // get one immediately

if (options.interval)
queue(report_name, list, options.interval);
queue(report_name, list, options || {});
});
};

var queue = function(report_name, list, interval) {
var queue = function(report_name, list, opts) {
var interval = opts.interval;
if (!interval) return;

// interval reporting triggered, so force auto connect to true
config.set('auto_connect', true);

// in case the delay is sent in minutes
if (interval < 1000) interval = interval * 60 * 1000;

active[report_name] = setInterval(function(){
var timer = setInterval(function(){
gather(report_name, list);
}, interval);

active[report_name] = { timer: timer, options: opts };
};

var gather = function(report_name, list, cb) {
Expand Down Expand Up @@ -155,7 +157,8 @@ var cancel = function(report_name){
logger.warn('Cancelling ' + report_name + ' report.');
config.set('auto_connect', prev_auto_connect); // restore original value

clearInterval(active[report_name]);
var timer = active[report_name].timer;
clearInterval(timer);
// this.removeAllListeners(report_name);
delete(active[report_name]);
};
Expand All @@ -165,7 +168,17 @@ var cancel_all = function(){
cancel(report_name);
};

var running = function() {
var list = [];
for (var key in active) {
var obj = { name: key, options: active[key].options };
list.push(obj);
}
return list;
}

exports.map = map;
exports.get = get;
exports.running = running;
exports.cancel = cancel;
exports.cancel_all = cancel_all;
61 changes: 61 additions & 0 deletions lib/agent/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var fs = require('fs'),
common = require('./common'),
db_path = common.system.tempfile_path('local.db');

var db;

var load = function(cb) {
if (db) return cb();

fs.readFile(db_path, 'utf8', function(err, data) {
if (err || data.trim() == '') return cb(err);

try {
db = JSON.parse(data);
} catch(e) {
db = {};
err = e;
}
cb(err);
})
}

var save = function(cb) {
var err, str = JSON.stringify(db, null, 0);
try {
fs.writeFileSync(db_path, str);
} catch(e) {
err = e;
}
cb(err);
}

exports.set = function(key, data, cb) {
load(function(err) {
if (err) return cb(err);
db[key] = data;
save(cb);
})
}

exports.get = function(key, cb) {
load(function(err) {
if (err) return cb(err);

cb(null, db[key]);
})
}

exports.all = function(cb) {
load(function(err) {
if (err) return cb(err);

cb(null, db);
})
}

exports.clear = function(cb) {
db = {};
// fs.writeFile(db_path, '', cb);
fs.unlink(db_path);
}

0 comments on commit d36e55d

Please sign in to comment.