/* * Helper to load in all app plugins */ const loadPlugins = (app, lando) => Promise.resolve(app.plugins.registry) // Filter out .filter(plugin => _.has(plugin, 'app')) // LOADEM! .map(plugin => app.plugins.load(plugin, plugin.app, app, lando)) // Remove any naughty shit .map(plugin => _.pick(plugin.data, ['config', 'composeData', 'env', 'labels'])) // Merge minotaur .each(result => _.merge(app, result))
/* * Ping connection. */ ping() { return Promise.resolve(true); }
/* * Helper to init and then report */ const initAndReport = (app, method = 'start') => { return app.init().then(() => { app.metrics.report(method, utils.metricsParse(app)); return Promise.resolve(true); }); }
/** * Close webhook after closing all current connections. * Multiple invocations do nothing if webhook is already closed. * @return {Promise} promise */ closeWebHook() { if (!this._webHook) { return Promise.resolve(); } return this._webHook.close(); }
/** * Open WebHook by listening on the port * @return {Promise} */ open() { if (this.isOpen()) { return Promise.resolve(); } return new Promise(resolve => { this._webServer.listen(this.options.port, this.options.host, () => { debug('WebHook listening on port %s', this.options.port); this._open = true; return resolve(); }); }); }
/** * @description Poll all devices of one frequency. * @param {number} pollFrequency - The frequency in milliseconds. * @returns {function()} Return a function. * @example * pollAll(60000); */ function pollAll(pollFrequency) { return async () => { if (this.devicesByPollFrequency[pollFrequency]) { return Promise.map(this.devicesByPollFrequency[pollFrequency], (device) => this.poll(device)); } return Promise.resolve(); }; }
describe(`with detached connection`, () => { let error, tsk; beforeEach(done => { db.task(function () { tsk = this; return promise.resolve(); }) .then(() => { // try use the task connection context outside of the task callback; return tsk.query(`select 'test'`); }) .catch(err => { error = err; }) .finally(done); }); it(`must throw an error on any query request`, () => { expect(error instanceof Error).toBe(true); expect(error.message).toBe($text.looseQuery); expect(tsk.ctx.level).toBe(0); expect(tsk.ctx.parent).toBeNull(); expect(tsk.ctx.connected).toBe(true); expect(tsk.ctx.inTransaction).toBe(false); }); });
/** * Stops polling after the last polling request resolves. * Multiple invocations do nothing if polling is already stopped. * Returning the promise of the last polling request is **deprecated**. * @param {Object} [options] Options * @param {Boolean} [options.cancel] Cancel current request * @param {String} [options.reason] Reason for stopping polling * @return {Promise} */ stopPolling(options) { if (!this._polling) { return Promise.resolve(); } return this._polling.stop(options); }
describe(`with a simple promise result`, () => { let result, context, THIS; beforeEach(done => { db.task(function (t) { THIS = this; context = t; return promise.resolve(`Ok`); }) .then(data => { result = data; }) .finally(done); }); it(`must resolve with that result`, () => { expect(result).toBe(`Ok`); }); it(`must provide correct connection context`, () => { expect(context && typeof context === `object`).toBeTruthy(); expect(context === THIS).toBe(true); }); });
/* * Helper to determine up and down * NOTE: we now assume that docker has been installed by this point * this means we also assume whatever neccessary installation checks have been * performed and dockers existence verified */ isUp(log = this.log, cache = this.cache, docker = this.docker) { // Auto return if cached and true if (cache.get('engineup') === true) return Promise.resolve(true); // Return true if we get a zero response and cache the result return shell.sh([`"${docker}"`, 'info']).then(() => { log.debug('engine is up.'); cache.set('engineup', true, {ttl: 5}); return Promise.resolve(true); }) .catch(error => { log.debug('engine is down with error', error); return Promise.resolve(false); }); }
/* * Retry the function fn up to max times until it successfully completes * without an error. Pause backoff * retry miliseconds between tries. */ const retry = (fn, {max = 5, backoff = 500} = {}) => Promise.resolve().then(() => { const rec = counter => Promise.try(() => fn(counter).catch(err => { if (counter <= max) { return Promise.delay(backoff * counter).then(() => rec(counter + 1)); } else { return Promise.reject(err); } })); // Init recursive function. return rec(1); })
const asyncMiddleware = (fn) => (req, res, next) => { Promise.resolve(fn(req, res, next)).catch(next); }
/* * Ping connection. */ ping() { return Promise.resolve(true); }