Skip to content

Commit

Permalink
Unify more redis usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dschmidt committed Jul 13, 2023
1 parent 176158c commit 02b91a1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
7 changes: 4 additions & 3 deletions packages/@uppy/companion/src/companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ module.exports.app = (optionsArg = {}) => {
logger.setMaskables(getMaskableSecrets(options))

// create singleton redis client
if (options.redisUrl) {
redis.client(options)
console.log('redisOptions', options.redisOptions)
if (options.redisOptions) {
redis.client(options.redisOptions)
}
const emitter = createEmitter(options.redisUrl, options.redisPubSubScope)
const emitter = createEmitter(options.redisOptions, options.redisPubSubScope)

const app = express()

Expand Down
4 changes: 2 additions & 2 deletions packages/@uppy/companion/src/server/emitter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ let emitter
* Used to transmit events (such as progress, upload completion) from controllers,
* such as the Google Drive 'get' controller, along to the client.
*/
module.exports = (redisUrl, redisPubSubScope) => {
module.exports = (redisOptions, redisPubSubScope) => {
if (!emitter) {
emitter = redisUrl ? redisEmitter(redisUrl, redisPubSubScope) : nodeEmitter()
emitter = redisOptions ? redisEmitter(redisPubSubScope) : nodeEmitter()
}

return emitter
Expand Down
6 changes: 3 additions & 3 deletions packages/@uppy/companion/src/server/emitter/redis-emitter.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const Redis = require('ioredis').default
const { EventEmitter } = require('node:events')

const logger = require('../logger')
const redis = require('../redis')

/**
* This module simulates the builtin events.EventEmitter but with the use of redis.
* This is useful for when companion is running on multiple instances and events need
* to be distributed across.
*/
module.exports = (redisUrl, redisPubSubScope) => {
module.exports = (redisPubSubScope) => {
const prefix = redisPubSubScope ? `${redisPubSubScope}:` : ''
const getPrefixedEventName = (eventName) => `${prefix}${eventName}`
const publisher = new Redis(redisUrl, { lazyConnect: true })
const publisher = redis.client().duplicate({ lazyConnect: true })
publisher.on('error', err => logger.error('publisher redis error', err.toString()))
let subscriber

Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/companion/src/server/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let redisClient
*/
function createClient (opts) {
if (!redisClient) {
redisClient = new Redis(opts.url, opts)
redisClient = new Redis(opts)

redisClient.on('error', err => logger.error('redis error', err.toString()))
}
Expand Down
13 changes: 10 additions & 3 deletions packages/@uppy/companion/src/standalone/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,18 @@ const getConfigFromEnv = () => {
periodicPingCount: process.env.COMPANION_PERIODIC_PING_COUNT
? parseInt(process.env.COMPANION_PERIODIC_PING_COUNT, 10) : undefined,
filePath: process.env.COMPANION_DATADIR,
redisUrl: process.env.COMPANION_REDIS_URL,
redisPubSubScope: process.env.COMPANION_REDIS_PUBSUB_SCOPE,
// adding redisOptions to keep all companion options easily visible
// redisOptions refers to https://www.npmjs.com/package/redis#options-object-properties
redisOptions: {},
// redisOptions refers to https://redis.github.io/ioredis/index.html#RedisOptions
redisOptions: (() => {
try {
const obj = JSON.parse(process.env.COMPANION_REDIS_OPTIONS)
return obj
} catch (e) {
console.log('COMPANION_REDIS_OPTIONS parse error', e)
return process.env.COMPANION_REDIS_URL
}
})(),
sendSelfEndpoint: process.env.COMPANION_SELF_ENDPOINT,
uploadUrls: uploadUrls ? uploadUrls.split(',') : null,
secret: getSecret('COMPANION_SECRET'),
Expand Down

0 comments on commit 02b91a1

Please sign in to comment.