// Helper to set caches const setCaches = (options, lando) => { // Get the github user github.authenticate({type: 'token', token: options['github-auth']}); return github.users.get({}) .then(user => { // Reset this apps metacache const metaData = lando.cache.get(`${options.name}.meta.cache`) || {}; lando.cache.set(`${options.name}.meta.cache`, _.merge({}, metaData, {email: user.data.email}), {persist: true}); // Reset github tokens const tokens = lando.cache.get(githubTokenCache) || []; const cache = {token: options['github-auth'], user: user.data.login, date: _.toInteger(_.now() / 1000)}; lando.cache.set(githubTokenCache, sortTokens(tokens, [cache]), {persist: true}); }); }
api.getAccountInfo().then(me => { // Get the project const project = _.find(me.projects, {name: options['platformsh-site']}); // Or error if there is no spoon if (_.isEmpty(project)) throw Error(`${options['platformsh-site']} does not appear to be a platform.sh site!`); // This is a good token, lets update our cache const cache = {token: options['platformsh-auth'], email: me.mail, date: _.toInteger(_.now() / 1000)}; // Update lando's store of platformsh machine tokens const tokens = lando.cache.get(platformshTokenCache) || []; lando.cache.set(platformshTokenCache, utils.sortTokens(tokens, [cache]), {persist: true}); // Update app metdata const metaData = lando.cache.get(`${options.name}.meta.cache`); lando.cache.set(`${options.name}.meta.cache`, _.merge({}, metaData, cache), {persist: true}); return {config: { id: _.get(project, 'id', 'lando'), }}; })
// Set the app caches, validate tokens and update token cache _.forEach(['pull', 'push', 'switch'], command => { app.events.on(`post-${command}`, (config, answers) => { // Only run if answer.auth is set, this allows these commands to all be // overriden without causing a failure here if (answers.auth) { const api = new PantheonApiClient(answers.auth, app.log); return api.auth().then(() => api.getUser().then(results => { const cache = {token: answers.auth, email: results.email, date: _.toInteger(_.now() / 1000)}; // Reset this apps metacache lando.cache.set(app.metaCache, _.merge({}, app.meta, cache), {persist: true}); // Set lando's store of pantheon machine tokens lando.cache.set(app.pantheonTokenCache, utils.sortTokens(app.pantheonTokens, [cache]), {persist: true}); // Wipe out the apps tooling cache to reset with the new MT lando.cache.remove(`${app.name}.tooling.cache`); })) // Throw some sort of error // NOTE: this provides some error handling when we are completely non-interactive .catch(err => { throw (_.has(err, 'response.data')) ? new Error(err.response.data) : err; }); } }); });
api.auth().then(() => Promise.all([api.getSites(), api.getUser()])) // Parse the dataz and set the things .then(results => { // Get our site and email const site = _.head(_.filter(results[0], site => site.name === options['pantheon-site'])); const user = results[1]; // Error if site doesn't exist if (_.isEmpty(site)) throw Error(`${site} does not appear to be a Pantheon site!`); // This is a good token, lets update our cache const cache = {token: options['pantheon-auth'], email: user.email, date: _.toInteger(_.now() / 1000)}; // Update lando's store of pantheon machine tokens const tokens = lando.cache.get(pantheonTokenCache) || []; lando.cache.set(pantheonTokenCache, utils.sortTokens(tokens, [cache]), {persist: true}); // Update app metdata const metaData = lando.cache.get(`${options.name}.meta.cache`); lando.cache.set(`${options.name}.meta.cache`, _.merge({}, metaData, cache), {persist: true}); // Add some stuff to our landofile return {config: { framework: _.get(site, 'framework', 'drupal'), site: _.get(site, 'name', options.name), id: _.get(site, 'id', 'lando'), }}; })
/* * This event is intended to make sure we reset the active token and cache when it is passed in * via the lando pull or lando push commands */ _.forEach(['pull', 'push'], command => { app.events.on(`post-${command}`, (config, answers) => { // Only run if answer.auth is set, this allows these commands to all be // overriden without causing a failure here if (answers.auth) { const api = new PlatformshApiClient({api_token: answers.auth}); return api.getAccountInfo().then(me => { // This is a good token, lets update our cache const cache = {token: answers.auth, email: me.mail, date: _.toInteger(_.now() / 1000)}; // Update lando's store of platformsh machine tokens const tokens = lando.cache.get(app.platformsh.tokenCache) || []; lando.cache.set(app.platformsh.tokenCache, utils.sortTokens(tokens, [cache]), {persist: true}); // Update app metdata const metaData = lando.cache.get(`${app.name}.meta.cache`); lando.cache.set(`${app.name}.meta.cache`, _.merge({}, metaData, cache), {persist: true}); }); } }); });
userSeenUpdate (userName) { const timestamp = _.now() this.usersseen.set(userName, timestamp) return Promise.resolve() }
processMessage (msg, setTimestamp = false) { delete msg.id delete msg.timestamp if (setTimestamp) { msg.timestamp = _.now() } msg.author = this.userName || msg.author return msg }
jwt() { const secret = this.config.session.secret; const payload = _.extend({}, this.toPlainObject(), { timestamp: _.now() }); return jwt.sign(payload, secret, { expiresIn: config.session.expiresIn }); }
it('should update an instance heartbeat', function (done) { this.timeout(4000) this.slow(2000) chatService = startService({ heartbeatRate: 500 }) const start = _.now() setTimeout(() => { Promise.join( chatService.getInstanceHeartbeat(chatService.instanceUID), chatService.getInstanceHeartbeat(), (ts1, ts2) => { expect(ts1).within(start, start + 2000) expect(ts2).within(start, start + 2000) done() }) }, 1000) })
messageAdd (msg) { const timestamp = _.now() const smsg = JSON.stringify(msg) return this.redis.messageAdd( this.makeKeyName('lastMessageId'), this.makeKeyName('historyMaxSize'), this.makeKeyName('messagesIds'), this.makeKeyName('messagesTimestamps'), this.makeKeyName('messagesHistory'), smsg, timestamp) .spread(id => { msg.id = id msg.timestamp = timestamp return msg }) }
updateHeartbeat () { return this.redis.set( this.makeKeyName('instances', this.instanceUID, 'heartbeat'), _.now()) .catchReturn() }
lockToRoom (roomName, ttl) { return uid(18).then(val => { const start = _.now() return this.lock(roomName, val, ttl).then(() => { return Promise.resolve().disposer(() => { if (start + ttl < _.now()) { this.server.emit( 'lockTimeExceeded', val, { userName: this.userName, roomName }) } return this.unlock(roomName, val) }) }) }) }
updateHeartbeat () { this.heartbeatStamp = _.now() return Promise.resolve() }
userSeenUpdate (userName) { const timestamp = _.now() return this.redis.hset(this.makeKeyName('usersseen'), userName, timestamp) }