Example #1
0
var Daemon = function (daemons, source) {
	this.source = source;

	this.logger = new Logger ({
		related: [source._id]
	}, _.bind (function (data) {
		var e = encodeURIComponent;

		data.type = 'urn:types/log';

		var options = {
			path: '/' + e ('sys/logs') +
				'/_design/' + e ('urn:logs') +
				'/_update/' + e (data.type),
			method: 'POST',
			data: data
		};
    
		this.request (null, options, function (error) {
			if (error) {
				console.error (error);
			}
		});
	}, this));

	this.hostname = require ('os').hostname ();

	this._daemons = daemons;
	this._compile ();
};
Example #2
0
	_stream: function (db, since) {
		if (db.streaming) {
			db.streamMore = true;
			return;
		}

		db.streaming = true;

		var callback = _.bind (function (error, result) {
			if (error) {
				console.error (error);
				this._endStream (db);
				return;
			}


			_.each (result.results, function (event) {
				this._dbEvent (db, event);
			}, this);


			this._endStream (db);
		}, this);

		var path = '/' + encodeURIComponent (db.name) +
				'/_changes?include_docs=true&since=' + since

		var options = {
			path: path
		};

		this._request (null, options, callback);
	},
Example #3
0
	notify: function (event) {
		if (event.db) {
			var name = event.db;

			// deleted, created, updated
			if (event.type == 'created') {
				_.each (this.daemons, function (daemon) {
					if (typeof daemon.notifyDb == 'function') {
						daemon.notifyDb (event);
					}
				});
			}

			if (event.type == 'deleted') {
				if (this.dbs [name]) {
					delete this.dbs [name]
				}

				_.each (this.daemons, function (daemon) {
					if (typeof daemon.notifyDb == 'function') {
						daemon.notifyDb (event);
					}
				});

				return;
			}

			if (!this.dbs [name]) {
				this.dbs [name] = {
					name: name,
					info: null,
					streaming: false,
					fetching: false
				};
			}

			var db = this.dbs [name];
			if (!db.info) {
				this._info (db, _.bind (function () {
					this._stream (db, db.info.update_seq - 1);
				}, this));
			} else {
				this._stream (db, db.info.update_seq);
			}
		}
	},
CredentialsRepository.prototype.loadAllByRemoteId = function(remoteId, callback) {
    var redisClient = this.redisClient;
    var load = _.bind(this.load, this);
    var Collection = this.Collection;
    return redisClient.smembers(this.relationKey(remoteId, this.type), function(err, results) {
        if (err) {
            callback(err);
        } else {
            async.map(results, load, function(err, results) {
                if (err) {
                    callback(err);
                } else {
                    callback(null, new Collection(results));
                }
            });
        }
    })
};
Example #5
0
	reload: function () {
		var _callback = _.bind (function (error, result) {
			if (error) {
				console.error ('Failed to reload daemons', error);
				return;
			}

			_.each (this.daemons, function (daemon) {
				daemon.stop ();
			});

			this.daemons = _.map (result.rows, function (row) {
				var daemon = new Daemon (this, row.doc);
				daemon.start ();
				return daemon;
			}, this);
		}, this);

		this._daemons (_callback);
	},
module.exports = DeferredCallback = function() {
    this._results = null;
    this._callback = null;
    this.callback = _.bind(this.callback, this);
    this.done = _.bind(this.done, this);
};