Skip to content

Commit

Permalink
Fix: Add redis_url config option and fix the Redis connection (#40)
Browse files Browse the repository at this point in the history
The URL passed into the constructor was not being passed correctly, so it was falling back to localhost.
  • Loading branch information
adamlogic authored May 31, 2024
1 parent 005b7aa commit f03d0cf
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 29 deletions.
14 changes: 9 additions & 5 deletions packages/bull/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import { Judoscale } from 'judoscale-bull'
// CommonJS
const { Judoscale } = require('judoscale-bull')

// Initialize Judoscale with default configuration
const judoscale = new Judoscale()
// Initialize Judoscale with optional configuration
const judoscale = new Judoscale({
redis_url: process.env.REDISCLOUD_URL, // defaults to process.env.REDIS_URL
})
```

3. If you want to scale your workers all the way down, you also need to install judoscale-bull in your web process:
3. If you want to scale your workers down to zero instances, you also need to install judoscale-bull in your web process so Judoscale knows when to scale them back up:

```javascript
// ESM
Expand All @@ -34,8 +36,10 @@ import 'judoscale-bull'
const { Judoscale, middleware: judoscaleMiddleware } = require('judoscale-express')
require('judoscale-bull')

// Judoscale will automatically be initialized for both Bull and Express/Fastify
const judoscale = new Judoscale()
// Judoscale will be initialized for both Bull and Express/Fastify
const judoscale = new Judoscale({
redis_url: process.env.REDISCLOUD_URL, // defaults to process.env.REDIS_URL
})
```

## Troubleshooting
Expand Down
19 changes: 12 additions & 7 deletions packages/bull/src/bull-metrics-collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class BullMetricsCollector extends WorkerMetricsCollector {
constructor() {
super('Bull')
collectors.push(this)

this.redisUrl = process.env.REDIS_URL || 'redis://127.0.0.1:6379'
this.queues = new Map()
}

Expand All @@ -36,12 +34,11 @@ class BullMetricsCollector extends WorkerMetricsCollector {
}

async prepareQueues() {
const redis = new Redis({ url: this.redisUrl })
const redisKeys = []
let cursor = '0'

do {
const reply = await redis.scan(cursor, 'MATCH', 'bull:*:id')
const reply = await this.redis.scan(cursor, 'MATCH', 'bull:*:id')
cursor = reply[0]
redisKeys.push(...reply[1])
} while (cursor !== '0')
Expand All @@ -51,17 +48,25 @@ class BullMetricsCollector extends WorkerMetricsCollector {
const queue = new Queue(queueName, { url: this.redisUrl })
this.queues.set(queueName, queue)
}

await redis.quit()
}

async closeQueues() {
async tearDown() {
await this.redis.quit()
for (const queue of this.queues.values()) {
await queue.close()
}

this.queues.clear()
}

get redis() {
if (!this._redis) {
const redisUrl = this.config.redis_url || process.env.REDIS_URL || 'redis://127.0.0.1:6379'
this._redis = new Redis(redisUrl)
}

return this._redis
}
}

module.exports = BullMetricsCollector
9 changes: 3 additions & 6 deletions packages/bull/test/bull-metrics-collector.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Redis = require('ioredis')
const Queue = require('bull')
const BullMetricsCollector = require('../src/bull-metrics-collector')

Expand All @@ -9,14 +8,12 @@ describe('BullMetricsCollector', () => {
collector = new BullMetricsCollector()

// Clear all Bull information in Redis
const redis = new Redis({ url: this.redisUrl })
const keys = await redis.keys('bull:*')
if (keys.length) await redis.del(keys)
await redis.quit()
const keys = await collector.redis.keys('bull:*')
if (keys.length) await collector.redis.del(keys)
})

afterEach(async () => {
if (collector) await collector.closeQueues()
if (collector) await collector.tearDown()
if (queue) await queue.close()
})

Expand Down
14 changes: 9 additions & 5 deletions packages/bullmq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import { Judoscale } from 'judoscale-bullmq'
// CommonJS
const { Judoscale } = require('judoscale-bullmq')

// Initialize Judoscale with default configuration
const judoscale = new Judoscale()
// Initialize Judoscale with optional configuration
const judoscale = new Judoscale({
redis_url: process.env.REDISCLOUD_URL, // defaults to process.env.REDIS_URL
})
```

3. If you want to scale your workers all the way down, you also need to install judoscale-bullmq in your web process:
3. If you want to scale your workers down to zero instances, you also need to install judoscale-bullmq in your web process so Judoscale knows when to scale them back up:

```javascript
// ESM
Expand All @@ -34,8 +36,10 @@ import 'judoscale-bullmq'
const { Judoscale, middleware: judoscaleMiddleware } = require('judoscale-express')
require('judoscale-bullmq')

// Judoscale will automatically be initialized for both BullMQ and Express/Fastify
const judoscale = new Judoscale()
// Judoscale will be initialized for both BullMQ and Express/Fastify
const judoscale = new Judoscale({
redis_url: process.env.REDISCLOUD_URL, // defaults to process.env.REDIS_URL
})
```

## Troubleshooting
Expand Down
14 changes: 10 additions & 4 deletions packages/bullmq/src/bull-mq-metrics-collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ const { Metric, WorkerMetricsCollector } = require('judoscale-node-core')
class BullMQMetricsCollector extends WorkerMetricsCollector {
constructor() {
super('BullMQ')

const redisUrl = process.env.REDIS_URL || 'redis://127.0.0.1:6379'

this.redis = new Redis({ connection: { url: redisUrl } })
this.queueNames = new Set()
}

async collect() {
// TODO: New queues created after first collect() call will not be reported
console.log('COLLECT')
if (this.queueNames.size == 0) await this.fetchQueueNames()

let metrics = []
Expand Down Expand Up @@ -44,6 +41,15 @@ class BullMQMetricsCollector extends WorkerMetricsCollector {
this.queueNames.add(queueName)
}
}

get redis() {
if (!this._redis) {
const redisUrl = this.config.redis_url || process.env.REDIS_URL || 'redis://127.0.0.1:6379'
this._redis = new Redis(redisUrl)
}

return this._redis
}
}

module.exports = BullMQMetricsCollector
7 changes: 5 additions & 2 deletions packages/node-core/src/judoscale.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ class Judoscale {
constructor(options) {
this.config = new Config(options)

const reporter = new Reporter()
// Expose config to the collectors
for (const adapter of Judoscale.adapters) {
adapter.collector.config = this.config
}

reporter.start(this.config, Judoscale.adapters)
new Reporter().start(this.config, Judoscale.adapters)
}

static registerAdapter(identifier, collector, meta = {}) {
Expand Down
1 change: 1 addition & 0 deletions packages/node-core/src/web-metrics-collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class WebMetricsCollector {
constructor(store, collectorName = 'Web') {
this.collectorName = collectorName
this.store = store
this.config = {}
}

collect() {
Expand Down
1 change: 1 addition & 0 deletions packages/node-core/src/worker-metrics-collector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class WorkerMetricsCollector {
constructor(collectorName) {
this.collectorName = collectorName
this.config = {}
}

collect() {
Expand Down
31 changes: 31 additions & 0 deletions packages/node-core/test/judoscale.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* global test, expect, describe, jest */

const Judoscale = require('../src/judoscale')

describe('Judoscale', () => {
test('registering an adapter adds it to static adapters', () => {
const collector = {}
Judoscale.registerAdapter('judoscale-test-adapter', collector, { foo: 'bar' })

expect(Judoscale.adapters.length).toEqual(1)
expect(Judoscale.adapters[0].identifier).toEqual('judoscale-test-adapter')
expect(Judoscale.adapters[0].collector).toEqual(collector)
expect(Judoscale.adapters[0].meta.foo).toEqual('bar')
})

test('passes options to config property', () => {
const judoscale = new Judoscale({ foo: 'bar' })

expect(judoscale.config.log_level).toEqual('info')
expect(judoscale.config.foo).toEqual('bar')
})

test('exposes config to collectors', () => {
const collector = {}
Judoscale.registerAdapter('judoscale-test-adapter', collector, { foo: 'bar' })

const judoscale = new Judoscale({ foo: 'bar' })

expect(collector.config.foo).toEqual('bar')
})
})

0 comments on commit f03d0cf

Please sign in to comment.