pool.databaseQueryLimiter = (clientIp, fn) => q.async(function*() { const scheduleResult = yield scheduler.schedule(clientIp, q.async(function*() { const before = Date.now(); const result = yield fn(); const duration = Date.now() - before; return { cost: duration, result: result } })); return scheduleResult.result; })();
/** * Delete application versions based on application name * @param {string} applicationName * @returns {promise} */ cleanApplicationVersions(applicationName) { winston.info(`Clean application versions of ${applicationName}...`); return q.async(function* () { const applicationVersions = _.map((yield q.ninvoke(this.elasticbeanstalk, 'describeApplicationVersions', { ApplicationName: applicationName })).ApplicationVersions, 'VersionLabel'); const applicationVersionsToKeep = _.map((yield q.ninvoke(this.elasticbeanstalk, 'describeEnvironments', { ApplicationName: applicationName, IncludeDeleted: false })).Environments, 'VersionLabel'); const applicationVersionsToDelete = _.difference(applicationVersions, applicationVersionsToKeep); for (const version of applicationVersionsToDelete) { winston.info(`Delete version: ${version}`); yield this.deleteApplicationVersion({ ApplicationName: applicationName, VersionLabel: version, DeleteSourceBundle: true }); } }.bind(this))(); }
waitUtilHealthy(environmentName) { winston.info(`Waiting until ${environmentName} is healthy`); return q.async(function* () { let timeLeft = HEALTHY_TIMEOUT; let environmentDescription = {}; while (timeLeft > 0 && (environmentDescription.Health !== 'Green' || environmentDescription.Status !== 'Ready')) { process.stdout.write('.'); environmentDescription = yield this.describeEnvironment(environmentName); if (typeof environmentDescription !== 'object') { throw new Error(`Failed to heath check environment: ${environmentName}. Maybe the environment is terminated.`); } timeLeft = timeLeft - POLL_INTERVAL; yield this.wait(POLL_INTERVAL); } if (typeof environmentDescription !== 'object' || environmentDescription.Health !== 'Green' || environmentDescription.Status !== 'Ready') { throw new Error(`${environmentName} is not healthy`); } process.stdout.write('\n'); return environmentDescription; }.bind(this))(); }
mocha.Runnable.prototype.run = function( fn ) { if( isGeneratorFn( this.fn ) ) { this.fn = q.async( this.fn ); } run.apply( this, arguments ); };
/* * For each record in the modifiedRecords map, if the record was "corrected" to appear * properly in the incorrect UI, it now has to be updated to appear correctly in the * correct UI. The percentage values must be flipped: * 1 -> 3 * 2a -> 2b * 2b -> 2a * 3 -> 1 */ function flipValues() { log.info("Flipping values"); let doUpdates = Q.async(function*() { for (let [inventoryId, record] of modifiedRecords.entries()) { if (record.corrected) { log.info("Flipping values in " + inventoryId); let current = record.currentValues; let needsUpdate = false; for (let fieldName of radioFields) { let currentValue = current[fieldName]; let newValue = currentValue; if (currentValue === '1') { newValue = '3'; } else if (currentValue === '2a') { newValue = '2b'; } else if (currentValue === '2b') { newValue = '2a'; } else if (currentValue === '3') { newValue = '1'; } if (newValue !== currentValue) { log.info('Changing ' + fieldName + ': ' + currentValue + ' => ' + newValue); current[fieldName] = newValue; needsUpdate = true; } } if (needsUpdate) { log.info({ values: record.currentValues }, 'Updating ' + inventoryId + ' (' + record.csid + ')'); try { yield cspace.updateRecord('osteology', record.csid, { fields: record.currentValues }); } catch(error) { log.error({ error: error }, 'Error updating ' + inventoryId + ' (' + record.csid + ')'); } } else { log.info('No update needed for ' + inventoryId + ' (no flippable values found)'); } } else { log.info('No update needed for ' + inventoryId + ' (was not corrected in cspace)'); } } }); return doUpdates(); }
get: (req, res, next) => q.async(function* () { let query = req.query.q; query = encodeURIComponent(query); let result = yield getContent('http://localhost:8983/solr/core0/select?wt=json&indent=on&q=value:' + query, false); result = JSON.parse(result); return arf.response(res, result, 200); })().catch(next).done(),
Playlist.prototype.tracks = Q.async( function* () { // Tracks ids var trackIds = yield this.room.trackIds(); console.log('trackIds', trackIds); var roomId = this.room.attrs.id; // Map to tracks var serializedTracks = yield Q.all(_.map(trackIds, Q.async( function * (trackId) { var track = yield Track.get(roomId, trackId); if (!track) { console.log('huh? No track found for', roomId, trackId); return null; } return track.attrs; }))); return _.select(serializedTracks, function(track){ return (track !== null); }); });
it('exchange and validate google access token', function(done){ Q.async( function*(){ var url = 'https://accounts.google.com/o/oauth2/token'; var response = yield Q.ninvoke( superagent.post(url) .send({ client_id: beaver.config.oath.Google.clientId, client_secret: beaver.config.oath.Google.clientSecret, refresh_token: beaver.config.oath.Google.testUser.refreshToken, grant_type: 'refresh_token' }) .set('Content-Type', 'application/x-www-form-urlencoded') //must set content-type, other wise won't work .set('Accept', 'application/json'), 'end'); var accessToken = response.body && response.body.access_token; //validate access token var validStatus = yield beaver.modules.User.validateGoogleAccessToken( beaver.config.oath.Google.testUser.userId, accessToken ); expect(validStatus).toBe(true); done(); })() .fail(function(error){ expect(error).toBe(null); done(); }) });
proxy.doQuery = function(query, params) { const fn = q.async(function*() { const before = Date.now(); try { const result = yield proxy.querypNolog(query, params); statistics.emit('psql_query', Date.now() - before, null, _.extend(proxy.loggingContext)); return result; } catch(err) { statistics.emit('psql_query', Date.now() - before, err, _.extend({sql: query}, proxy.loggingContext)); logger.error("Query failed: ", _.extend({ query: query, params: params, error: err }, proxy.loggingContext)); throw err; } }); if(requestLimiter && proxy.loggingContext && proxy.loggingContext.clientIp) { return requestLimiter(proxy.loggingContext.clientIp, fn); } else { return fn(); } };
it('it should open multiple socket connections', function(done){ Q.async(function*(){ var socketClients = yield Q.all([socketLogin(0), socketLogin(1), socketLogin(2)]); //check if redis is set properly var allUserOnlineIds = yield Q.denodeify(beaver.redis.getSortedSetRange)("users:online", 0, -1); expect(allUserOnlineIds.length).toBe(3); var differences = _.difference( allUserOnlineIds.map(function(elem){return parseInt(elem);}), users.map(function(elem){return parseInt(elem.id);}) ); expect(differences.length).toBe(0); socketClients.forEach(function(socketClient){ socketClient.disconnect(); }); done(); })() .fail(function(error){ expect(error).toBe(null); done(); }); });
it('it should change the user password', function(done){ Q.async( function*(){ //change password var response = yield Q.ninvoke( superagent.post(beaver.config.global.origin + 'api/user/changePassword') .send({ email: user.email, password: user.password, newPassword: crypto.createHash('md5').update('111112').digest('hex') }) .set('Accept', 'application/json'), 'end') //then login using new password response = yield Q.ninvoke( superagent.post(beaver.config.global.origin + 'api/user/signin') .send({email: user.email, password: crypto.createHash('md5').update('111112').digest('hex')}) .set('Accept', 'application/json'), 'end'); var newUser = response.body; expect(newUser).not.toBe(null); expect(newUser.email).toBe(user.email); expect(newUser.name).toBe(user.name); done(); })() .fail(function(error){ expect(error).toBe(null); done(); }); });
module.getOnlineUser = function(req, res) { if(_.isUndefined(req.param("offset")) || _.isUndefined(req.param("limit"))) { return res.error('[socket: invalid-data]'); } var start = req.param("offset"), end = start + ((req.param("limit") - 1) || 19); var key = "users:online"; Q.async( function*(){ var uids = yield Q.denodeify(beaver.redis.getSortedSetRevRange)(key, start, end); var userData = yield Q.denodeify(beaver.redis.getObjectsFields)(uids.map(function(uid){ return "user:" + uid; }), ['id', 'name', 'email', 'avatar', 'status', 'lastonline']); var nUsers = yield Q.denodeify(beaver.redis.sortedSetCard)(key); //only get online user userData = userData.filter(function(item) { return item.status !== 'offline'; }); res.success({ count: nUsers, rows: userData }); })() .fail(function(error){ res.error(error.stack || error); }); }
let createNewElement = Q.async(function* (el) { let repo = 'polymerelements/seed-element'; // download and extrac the seed-element let repoPath = yield utils.extractGhRepo(repo, '.', el.name); el.install = repoPath; yield fs.removeTree(path.resolve(repoPath, '.github')); let replacePaths = yield globby([`${repoPath}/**`], {nodir: true}); // replace the text `seed-element` with the name of the new element yield Promise.all(replacePaths.map(Q.async(function*(p) { let content = yield fs.read(p); let ccName = utils.toCamelCase(el.name); content = content.replace(/seedElement/g, ccName); content = content.replace(/seed-element/g, el.name); yield fs.write(p, content); }))); let fromPath = path.resolve(repoPath, 'seed-element.html'); let toPath = path.resolve(repoPath, `${el.name}.html`); // rename the paths to the new name yield fs.rename(fromPath, toPath); return el; });
module.joinConversation = function(req, res) { var conversationId = req.param("conversationId"); if(!conversationId) { return res.error("missing required parameter conversationId"); } Q.async( function*(){ //get user id in this conversation var userInfo = yield beaver.modules.Socket.getUserInConversation(conversationId); var userIds = userInfo.userIds; //if requested user in the list; allow them to join if(_.indexOf(userIds, req.session.user.id) != -1) { req.socket.join(conversationId); } //TODO: return old message; it should only be used for user who joined conversation in the past not new user var oldMessages = yield beaver.modules.Conversation.loadPreviousMessages(conversationId); res.success({ users: userInfo.users, oldMessages: oldMessages }); })() .fail(function(error){ res.error(error.stack || error); }); }
/* * For each record in the modifiedRecords map, retrieve the current values from * cspace. Store these in the currentValues property of the record. */ function getCurrentValues() { log.info("Getting current values"); let readRecords = Q.async(function*() { yield cspace.connect(user, password); let count = 0; for (let [inventoryId, record] of modifiedRecords.entries()) { count++; let osteoRecord = yield cspace.getRecord('osteology', record.csid); record.currentValues = osteoRecord.fields; log.info('Found current values for ' + inventoryId + ' (' + count + ')'); } for (let [inventoryId, record] of modifiedRecords.entries()) { if (!record.currentValues) { log.warn('Could not find current values for ' + inventoryId + ', ' + record.csid); } } for (let [inventoryId, record] of modifiedRecords.entries()) { if (record.currentValues) { log.info({ values : record.currentValues }, 'Current values for ' + inventoryId); } } }); return readRecords(); }
module.exports = function() { return q .async( function *() { if( fs.existsSync( DEFAULT_INSTALL_LOCATION ) ) { return DEFAULT_INSTALL_LOCATION; } var download_link = yield _getDownloadUrl(); var install_location = _downloadAndUntar( download_link ); return install_location; } )() .then( function( install_dir ) { // Build /tmp/imagemagick/lib/bin/convert var convert_bin_path = path.join( install_dir, 'bin', 'convert' ); // Add /tmp/imagemagick/lib/ to LD_LIBRARY_PATH so that the exe can // find the .so's var lib_path = path.join( install_dir, 'lib' ); process.env.LD_LIBRARY_PATH = ( process.env.LD_LIBRARY_PATH ) ? process.env.LD_LIBRARY_PATH + ':' : ''; process.env.LD_LIBRARY_PATH += lib_path; return convert_bin_path; } ); };
let transformer = csv.transform((row, callback) => { let processRow = Q.async(rowHandler); processRow(row, cspace) .then( () => { return Promise.resolve(); }, (error) => { if (stopOnFailedRow) { log.warn('Processing will stop because of a failed row'); return Promise.reject(error); } // Log the error, but let processing continue. log.warn(error); return Promise.resolve(); } ) .then(() => { if (exitRequest) { throw exitRequest; } return checkConnection(); }) .then(() => { callback(); }) .catch((error) => { callback(error); }); }, {
module.getUsersSocketIds = function(userIds) { var deferred = Q.defer(); var tasks = []; _.forEach(userIds || [], function(userId){ tasks.push(module.getUserSocketIds(userId)); }); Q.async( function*(){ var a = yield Q.all(tasks); var outIds = []; _.forEach(a, function(elem){ if(elem && _.isArray(elem)) { outIds = outIds.concat(elem); } }); deferred.resolve(outIds); })() .fail(function(error){ deferred.reject(error); }); return deferred.promise; }
checkLocalLayer(image_id) { return Q.async(function* () { var image = yield this.dockerRemote.getImage(image_id); var inspected_image = yield this.dockerRemote.inspectImage(image); return inspected_image; }.bind(this))(); }
getTotalLocalSize(hubResult, tag) { return Q.async(function* () { // get from local docker var fullTagName = hubResult.namespace + '/' + hubResult.repository + ':' + tag; if (hubResult.namespace === 'library') { fullTagName = hubResult.repository + ':' + tag; } var imagesFound = yield this.dockerRemote.searchImagesByTag(fullTagName); if (!imagesFound || imagesFound.length === 0) { return ({ total_local_size : 0, localAncestors : [], imagesFound : imagesFound, }); } var localAncestors = yield this.dockerRemote.anscestors(imagesFound[0].Id); var total_local_size = _.reduce(localAncestors, function(sum, anscestor) { return sum + anscestor.imageInspect.Size; }, 0); log.debug('\n\n:: syncronizer - getTotalLocalSize ' + fullTagName + ' ::'); log.debug(prettyBytes(total_local_size)); return ({ total_local_size : total_local_size, localAncestors : localAncestors, imagesFound : imagesFound, }); }.bind(this))(); }
setTags(hubResult) { return Q.async(function* () { // get all tags var tags = yield this.dockerRegistry.tags(hubResult); var results = []; for (var name in tags) { if (tags.hasOwnProperty(name)) { var tagName = name; var imageId = tags[name]; log.debug('\n\n:: syncronizer - setTag - search image ::'); log.debug(tagName, imageId); var result = yield this.dockerRemote.setImageTag(hubResult.namespace, hubResult.repository, imageId, tagName); results.push(result); } } return results; }.bind(this))(); }
app.del('/api/bundle/:id/book/:pgid', function (req, res) { Q.async(function* () { let args = yield Q.nfcall(request, config.b4db + req.params.id), couchRes = args[0], bundle = JSON.parse(args[1]); // fail fast if we couldn't retrieve the bundle if (couchRes.statusCode !== 200) { res.json(couchRes.statusCode, bundle); return; } // fail if the bundle doesn't already have that book if (!(req.params.pgid in bundle.books)) { res.json(409, { error: "conflict", reason: "Bundle does not contain that book." }); return; } // remove the book from the bundle delete bundle.books[req.params.pgid]; // put the updated bundle back into CouchDB request.put({url: config.b4db + bundle._id, json: bundle}).pipe(res); })() .catch(function (err) { res.json(502, {error: "bad_gateway", reason: err.code}); }); });
module.exports = Model(function () { var Q = require('q'); return { //获取用户列表w getUserOrderList: function (id) { var json = {}; return D('Order').where({userid: id, nowstate: ['>', 0]}).order('id DESC').countSelect().then(function (data) { formatTime(data.data, 'YYYY-MM-DD', 'ordertime'); if (data.count == 0) { json.progressCount = ''; } else { json.progressCount = data.count; } json.progress = data.data; }).then(function () { return D('Order').where({userid: id, nowstate: ['<=', 0]}).order('id DESC').countSelect().then(function (data) { formatTime(data.data, 'YYYY-MM-DD', 'ordertime'); if (data.count == 0) { json.completeCount = ''; } else { json.completeCount = data.count; } json.complete = data.data; return json; }); }); }, get_userOrderList: Q.async(function*(openid) { return yield D('Orderproductcopy').where({openid:openid,productstate:'55'}).order('id DESC').select(); }) } })
module.exports = Controller("Admin/BaseController", function(){ "use strict"; var moment = require('moment'); var Q = require('q'); return { indexAction: Q.async(function* (){ var orderModel = D('Orderproductcopy'); var priceSUM = yield orderModel.where({productstate:['IN',['55','30']]}).sum('productprice'); var orderComplete = yield orderModel.where({productstate:'55'}).count(); var orderCancel = yield orderModel.where({productstate:'60'}).count(); var waitPay = yield orderModel.where({productstate:'10'}).count(); var hasPay = yield orderModel.where({productstate:'30'}).count(); var ordercount = yield orderModel.count(); var userCount = D('users').count(); var adminCount = D('admin').count(); var expresser = D('express').count(); this.assign({ sumPrice:priceSUM, orderComplete:orderComplete, orderCancel:orderCancel, waitPay:waitPay, hasPay:hasPay, userCount:userCount, adminCount:adminCount, expresser:expresser, ordercount:ordercount }); return this.display(); }) }; });
var deserializeUser = function(id, done) { var getUser = Q.async(User.get); getUser(id).then(function(user) { done(null, user); }, function(error) { done(error); }); };
suggestions: (req, res, next) => q.async(function* () { let query = req.query.q; query = encodeURIComponent(query); let result = yield getContent('http://localhost:8983/solr/core0/suggesthandler?rows=5&wt=json&indent=on&q=' + query, false); result = JSON.parse(result); let suggestions = result.suggest.mySuggester[req.query.q].suggestions res.jsonp(suggestions); })().catch(next).done(),
return function (callback) { Q.async(function* () { var result = yield this.dockerRemote.loadImage(outputPath, imageId); if (iProgress) { iProgress(1); } callback(null, result); }.bind(this))(); }.bind(this);
module.initChatSession = function(req, res){ var userIds = req.param("userIds"); if(!userIds || !userIds.length) { return res.error('[socket] empty user list'); } userIds = beaver.utils.parseId(userIds); if(userIds.indexOf(req.session.user.id) == -1) { return res.error('[socket] logged in user in session does not belong to requested userIds params'); } Q.async( function*(){ //get all users in the chat session var users = yield Q.denodeify(beaver.redis.getObjectsFields)(userIds.map(function(uid){ return "user:"******"chat:newChatSession", { users: users, chatSession: conversation }); }); //just return success res.success({}); } })() .fail(function(error){ res.error(error.stack || error); }); }
module.addUserIntoConversation = function(conversationId, user) { var deferred = Q.defer(); Q.async( function*(){ var conversation = yield beaver.models.mongoose.Conversation .find() .where('_id', conversationId) .where('users.id').in([user.id]) .lean() .execQ(); var userIds = conversation.userIds; if(userIds && _.isString(userIds)) { userIds = _.chain(_.uniq(userIds.split(','))) .map(function(elem){ return _.parseInt(elem); }) .filter(function(elem){ return (!_.isNaN(elem)) && (elem != user.id); }) .push(user.id) .sort().value().join(","); } if(!conversation.length) { //push user into existing array yield beaver.models.mongoose.Conversation .update({ _id: conversationId }, { $push: { users: user }, $set: { userIds: userIds || "" } }) .execQ(); } deferred.resolve(); })() .fail(function(error){ deferred.reject(error); }); return deferred.promise; }
it('it should register user and login', function(done){ Q.async(function*(){ var count = yield beaver.sequelize.models.User.count(); expect(count).toBe(3); done(); })() .fail(function(error){ expect(error).toBe(null); done(false); }) });