Skip to content

Commit

Permalink
Create a basic package for BullMQ that we can import and use
Browse files Browse the repository at this point in the history
It doesn't actually do anything yet other than start the reporter. No metrics collected. No singleton reporter (BullMQ and Express each start their own reporter).
  • Loading branch information
adamlogic committed May 20, 2024
1 parent 754eafc commit c04a793
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
8 changes: 4 additions & 4 deletions bullmq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ npm install judoscale-bullmq --save

```javascript
// ESM
import judoscale from 'judoscale-bullmq'
import judoscaleBullMQ from 'judoscale-bullmq'

// CommonJs
const judoscale = require('judoscale-bullmq').default
const judoscaleBullMQ = require('judoscale-bullmq').default

// default configuration
judoscale.reporter.start()
judoscaleBullMQ()

// custom configuration
judoscale.reporter.start({
judoscaleBullMQ({
queues: ['default', 'urgent'],
log_level: 'debug',
})
Expand Down
14 changes: 12 additions & 2 deletions bullmq/src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
const { mergeConfig, Reporter, MetricsStore, requestMetrics, WebMetricsCollector } = require('judoscale-node-core')
const Adapter = require('./adapter')
const { mergeConfig, Reporter } = require('judoscale-node-core')
const Adapter = require('./Adapter')
// const BullMQMetricsCollector = require('./BullMQMetricsCollector')

module.exports = function initJudoscaleBullMQ(config = {}) {
const finalConfig = mergeConfig(config)
// const collectors = [new BullMQMetricsCollector()]
const collectors = []
const reporter = new Reporter()

reporter.start(finalConfig, collectors, Adapter)
}
3 changes: 2 additions & 1 deletion sample_apps/express_web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"bullmq": "^5.7.9",
"ejs": "^3.1.6",
"express": "^4.17.2",
"judoscale-express": "*"
"judoscale-express": "*",
"judoscale-bullmq": "*"
},
"devDependencies": {
"nodemon": "^3.1.0"
Expand Down
26 changes: 21 additions & 5 deletions sample_apps/express_web/src/app.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import Redis from 'ioredis'
import express from 'express'
import { Queue, Worker, QueueEvents } from 'bullmq'
import judoscale from 'judoscale-express'
import judoscaleExpress from 'judoscale-express'
import judoscaleBullMQ from 'judoscale-bullmq'

const app = express()
const port = process.env.PORT || 5000
const redisConfig = { connection: { host: '127.0.0.1', port: 6379 } }
const redis = new Redis(redisConfig.connection)
const judoscaleConfig = {
api_base_url: process.env.JUDOSCALE_URL || 'https://judoscale-node-sample.requestcatcher.com',
}

app.set('views', './views')
app.set('view engine', 'ejs')

app.use(judoscale.default(judoscaleConfig))
// TODO: Consider this approach which would mirror our Ruby adapter
// Judoscale.config({
// ...
// })

// TODO: Why do we have to use default? Is this broken in our published packages?
judoscaleBullMQ({
api_base_url: process.env.JUDOSCALE_URL || 'https://judoscale-node-sample.requestcatcher.com',
})

// TODO: Only pass the configuration to the first call that starts the reporter
// app.use(judoscaleExpress())

app.use(
judoscaleExpress.default({
api_base_url: process.env.JUDOSCALE_URL || 'https://judoscale-node-sample.requestcatcher.com',
})
)

app.use(express.json()) // parse JSON bodies
app.use(express.urlencoded({ extended: true })) // parse HTML forms

Expand Down
5 changes: 5 additions & 0 deletions sample_apps/express_web/src/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Worker } from 'bullmq'
import judoscaleBullMQ from 'judoscale-bullmq'

const redisOpts = { url: process.env.REDIS_URL || 'redis://127.0.0.1:6379' }
const queueNames = ['default', 'urgent']

judoscaleBullMQ({
api_base_url: process.env.JUDOSCALE_URL || 'https://judoscale-node-sample.requestcatcher.com',
})

const workers = queueNames.map((queueName) => {
console.log(`Starting worker for ${queueName} queue`)
return new Worker(queueName, handlerForQueue(queueName), { connection: redisOpts })
Expand Down

0 comments on commit c04a793

Please sign in to comment.