src/nopg.js
function create_watchdog(db, opts) {
	debug.assert(db).is('object');

	opts = opts || {};

	debug.assert(opts).is('object');

	opts.timeout = opts.timeout || 30000;
	debug.assert(opts.timeout).is('number');

	var w = {};
	w.db = db;
	w.opts = opts;

	/* Setup */

	w.timeout = setTimeout(function() {
		debug.warn('Got timeout.');
		w.timeout = undefined;
		Q.fcall(function() {
			var tr_open, tr_commit, tr_rollback, state, tr_unknown, tr_disconnect;

			// NoPg instance
			if(w.db === undefined) {
				debug.warn("Timeout exceeded and database instance undefined. Nothing done.");
				return;
			}

			if(!(w.db && w.db._tr_state)) {
				debug.warn("Timeout exceeded but db was not NoPg instance.");
				return;
			}

			state = w.db._tr_state;
			tr_open = (state === 'open') ? true : false;
			tr_commit = (state === 'commit') ? true : false;
			tr_rollback = (state === 'rollback') ? true : false;
			tr_disconnect = (state === 'disconnect') ? true : false;
			tr_unknown = ((!tr_open) && (!tr_commit) && (!tr_rollback) && (!tr_disconnect)) ? true : false;

			if(tr_unknown) {
				debug.warn("Timeout exceeded and transaction state was unknown ("+state+"). Nothing done.");
				return;
			}

			if(tr_open) {
				debug.warn("Timeout exceeded and transaction still open. Closing it by rollback.");
				return w.db.rollback().fail(function(err) {
					debug.error("Rollback failed: " + (err.stack || err) );
				});
			}

			if(tr_disconnect) {
				//debug.log('...but .disconnect() was already done.');
				return;
			}

			if(tr_commit) {
				//debug.log('...but .commit() was already done.');
				return;
			}

			if(tr_rollback) {
				//debug.log('...but .rollback() was already done.');
				return;
			}

		}).fin(function() {
			if(w && w.db) {
				w.db._events.emit('timeout');
			}
		}).done();
	}, opts.timeout);

	/* Set object */
	w.reset = function(o) {
		debug.assert(o).is('object');
		//debug.log('Resetting the watchdog.');
		w.db = o;
	};

	/** Clear the timeout */
	w.clear = function() {
		if(w.timeout) {
			//debug.log('Clearing the watchdog.');
			clearTimeout(w.timeout);
			w.timeout = undefined;
		}
	};

	return w;
}
function create_watchdog(db, opts) {
	debug.assert(db).is('object');

	opts = opts || {};

	debug.assert(opts).is('object');

	opts.timeout = opts.timeout || 30000;
	debug.assert(opts.timeout).is('number');

	var w = {};
	w.db = db;
	w.opts = opts;

	/* Setup */

	w.timeout = setTimeout(function() {
		debug.warn('Got timeout.');
		w.timeout = undefined;
		Q.fcall(function() {
			var tr_open, tr_commit, tr_rollback, state, tr_unknown, tr_disconnect;

			// NoPg instance
			if(w.db === undefined) {
				debug.warn("Timeout exceeded and database instance undefined. Nothing done.");
				return;
			}

			if(!(w.db && w.db._tr_state)) {
				debug.warn("Timeout exceeded but db was not NoPg instance.");
				return;
			}

			state = w.db._tr_state;
			tr_open = (state === 'open') ? true : false;
			tr_commit = (state === 'commit') ? true : false;
			tr_rollback = (state === 'rollback') ? true : false;
			tr_disconnect = (state === 'disconnect') ? true : false;
			tr_unknown = ((!tr_open) && (!tr_commit) && (!tr_rollback) && (!tr_disconnect)) ? true : false;

			if(tr_unknown) {
				debug.warn("Timeout exceeded and transaction state was unknown ("+state+"). Nothing done.");
				return;
			}

			if(tr_open) {
				debug.warn("Timeout exceeded and transaction still open. Closing it by rollback.");
				return w.db.rollback().fail(function(err) {
					debug.error("Rollback failed: " + (err.stack || err) );
				});
			}

			if(tr_disconnect) {
				//debug.log('...but .disconnect() was already done.');
				return;
			}

			if(tr_commit) {
				//debug.log('...but .commit() was already done.');
				return;
			}

			if(tr_rollback) {
				//debug.log('...but .rollback() was already done.');
				return;
			}

		}).fin(function() {
			if(w && w.db) {
				w.db._events.emit('timeout');
			}
		}).done();
	}, opts.timeout);

	/* Set object */
	w.reset = function(o) {
		debug.assert(o).is('object');
		//debug.log('Resetting the watchdog.');
		w.db = o;
	};

	/** Clear the timeout */
	w.clear = function() {
		if(w.timeout) {
			//debug.log('Clearing the watchdog.');
			clearTimeout(w.timeout);
			w.timeout = undefined;
		}
	};

	return w;
}
function create_watchdog ( db , opts ) { debug . assert ( db ) . is ( 'object' ) ; opts = opts || { } ; debug . assert ( opts ) . is ( 'object' ) ; opts . timeout = opts . timeout || 30000 ; debug . assert ( opts . timeout ) . is ( 'number' ) ; var w = { } ; w . db = db ; w . opts = opts ; /* Setup */ w . timeout = setTimeout ( function ( ) { debug . warn ( 'Got timeout.' ) ; w . timeout = undefined ; Q . fcall ( function ( ) { var tr_open , tr_commit , tr_rollback , state , tr_unknown , tr_disconnect ; // NoPg instance if ( w . db === undefined ) { debug . warn ( "Timeout exceeded and database instance undefined. Nothing done." ) ; return ; } if ( ! ( w . db && w . db . _tr_state ) ) { debug . warn ( "Timeout exceeded but db was not NoPg instance." ) ; return ; } state = w . db . _tr_state ; tr_open = ( state === 'open' ) ? true : false ; tr_commit = ( state === 'commit' ) ? true : false ; tr_rollback = ( state === 'rollback' ) ? true : false ; tr_disconnect = ( state === 'disconnect' ) ? true : false ; tr_unknown = ( ( ! tr_open ) && ( ! tr_commit ) && ( ! tr_rollback ) && ( ! tr_disconnect ) ) ? true : false ; if ( tr_unknown ) { debug . warn ( "Timeout exceeded and transaction state was unknown (" + state + "). Nothing done." ) ; return ; } if ( tr_open ) { debug . warn ( "Timeout exceeded and transaction still open. Closing it by rollback." ) ; return w . db . rollback ( ) . fail ( function ( err ) { debug . error ( "Rollback failed: " + ( err . stack || err ) ) ; } ) ; } if ( tr_disconnect ) { //debug.log('...but .disconnect() was already done.'); return ; } if ( tr_commit ) { //debug.log('...but .commit() was already done.'); return ; } if ( tr_rollback ) { //debug.log('...but .rollback() was already done.'); return ; } } ) . fin ( function ( ) { if ( w && w . db ) { w . db . _events . emit ( 'timeout' ) ; } } ) . done ( ) ; } , opts . timeout ) ; /* Set object */ w . reset = function ( o ) { debug . assert ( o ) . is ( 'object' ) ; //debug.log('Resetting the watchdog.'); w . db = o ; } ; /** Clear the timeout */ w . clear = function ( ) { if ( w . timeout ) { //debug.log('Clearing the watchdog.'); clearTimeout ( w . timeout ) ; w . timeout = undefined ; } } ; return w ; }
Create watchdog timer
@param db
@param opts
@return {{}}
Create watchdog timer