Skip to content

Commit 387e3ac

Browse files
authored
fix(mounting): differentiate base and appBase paths when disableListen (#623)
1 parent 790f623 commit 387e3ac

File tree

7 files changed

+291
-11
lines changed

7 files changed

+291
-11
lines changed

example/express.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
const Arena = require('../');
2+
const express = require('express');
3+
const {Queue, Worker, FlowProducer} = require('bullmq');
4+
const RedisServer = require('redis-server');
5+
6+
// Select ports that are unlikely to be used by other services a developer might be running locally.
7+
const HTTP_SERVER_PORT = 4735;
8+
const REDIS_SERVER_PORT = 4736;
9+
10+
// Create a Redis server. This is only for convenience
11+
12+
async function main() {
13+
const app = express();
14+
const server = new RedisServer(REDIS_SERVER_PORT);
15+
await server.open();
16+
const queueName = 'name_of_my_queue';
17+
const parentQueueName = 'name_of_my_parent_queue';
18+
19+
const queue = new Queue(queueName, {
20+
connection: {port: REDIS_SERVER_PORT},
21+
});
22+
new Queue(parentQueueName, {
23+
connection: {port: REDIS_SERVER_PORT},
24+
});
25+
26+
const flow = new FlowProducer({
27+
connection: {port: REDIS_SERVER_PORT},
28+
});
29+
30+
new Worker(
31+
queueName,
32+
async function (job) {
33+
await job.updateProgress(20);
34+
35+
// Wait 5sec
36+
await new Promise((res) => setTimeout(res, 5000));
37+
38+
// Randomly succeeds or fails the job to put some jobs in completed and some in failed.
39+
if (Math.random() > 0.5) {
40+
throw new Error('fake error');
41+
}
42+
},
43+
{
44+
concurrency: 3,
45+
connection: {port: REDIS_SERVER_PORT},
46+
}
47+
);
48+
49+
new Worker(
50+
parentQueueName,
51+
async function () {
52+
// Wait 10sec
53+
await new Promise((res) => setTimeout(res, 10000));
54+
55+
// Randomly succeeds or fails the job to put some jobs in completed and some in failed.
56+
if (Math.random() > 0.5) {
57+
throw new Error('fake error');
58+
}
59+
},
60+
{
61+
connection: {port: REDIS_SERVER_PORT},
62+
}
63+
);
64+
65+
const children = Array.from(Array(65).keys()).map((index) => ({
66+
name: 'child',
67+
data: {idx: index, foo: 'bar'},
68+
queueName,
69+
}));
70+
await flow.add({
71+
name: 'parent-job',
72+
queueName: parentQueueName,
73+
data: {},
74+
children,
75+
});
76+
77+
// adding delayed jobs
78+
const delayedJob = await queue.add('delayed', {}, {delay: 60 * 1000});
79+
delayedJob.log('Log message');
80+
81+
const arena = Arena(
82+
{
83+
BullMQ: Queue,
84+
85+
queues: [
86+
{
87+
// Required for each queue definition.
88+
name: queueName,
89+
90+
// User-readable display name for the host. Required.
91+
hostId: 'Queue Server 1',
92+
93+
// Queue type (Bull or Bullmq or Bee - default Bull).
94+
type: 'bullmq',
95+
96+
redis: {
97+
// host: 'localhost',
98+
port: REDIS_SERVER_PORT,
99+
},
100+
},
101+
{
102+
// Required for each queue definition.
103+
name: parentQueueName,
104+
105+
// User-readable display name for the host. Required.
106+
hostId: 'Queue Server 2',
107+
108+
// Queue type (Bull or Bullmq or Bee - default Bull).
109+
type: 'bullmq',
110+
111+
redis: {
112+
// host: 'localhost',
113+
port: REDIS_SERVER_PORT,
114+
},
115+
},
116+
],
117+
},
118+
{
119+
basePath: '/',
120+
disableListen: true,
121+
}
122+
);
123+
124+
app.use('/arena', arena);
125+
126+
app.listen(HTTP_SERVER_PORT, () =>
127+
console.log(`Arena listening on port ${HTTP_SERVER_PORT}!`)
128+
);
129+
}
130+
131+
main().catch((err) => {
132+
console.error(err);
133+
process.exit(1);
134+
});

example/fastify.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
const Arena = require('../');
2+
const fastify = require('fastify');
3+
const {Queue, Worker, FlowProducer} = require('bullmq');
4+
const RedisServer = require('redis-server');
5+
6+
// Select ports that are unlikely to be used by other services a developer might be running locally.
7+
const HTTP_SERVER_PORT = 4735;
8+
const REDIS_SERVER_PORT = 4736;
9+
10+
// Create a Redis server. This is only for convenience
11+
12+
async function main() {
13+
const app = fastify();
14+
const server = new RedisServer(REDIS_SERVER_PORT);
15+
await server.open();
16+
const queueName = 'name_of_my_queue';
17+
const parentQueueName = 'name_of_my_parent_queue';
18+
19+
const queue = new Queue(queueName, {
20+
connection: {port: REDIS_SERVER_PORT},
21+
});
22+
new Queue(parentQueueName, {
23+
connection: {port: REDIS_SERVER_PORT},
24+
});
25+
26+
const flow = new FlowProducer({
27+
connection: {port: REDIS_SERVER_PORT},
28+
});
29+
30+
new Worker(
31+
queueName,
32+
async function (job) {
33+
await job.updateProgress(20);
34+
35+
// Wait 5sec
36+
await new Promise((res) => setTimeout(res, 5000));
37+
38+
// Randomly succeeds or fails the job to put some jobs in completed and some in failed.
39+
if (Math.random() > 0.5) {
40+
throw new Error('fake error');
41+
}
42+
},
43+
{
44+
concurrency: 3,
45+
connection: {port: REDIS_SERVER_PORT},
46+
}
47+
);
48+
49+
new Worker(
50+
parentQueueName,
51+
async function () {
52+
// Wait 10sec
53+
await new Promise((res) => setTimeout(res, 10000));
54+
55+
// Randomly succeeds or fails the job to put some jobs in completed and some in failed.
56+
if (Math.random() > 0.5) {
57+
throw new Error('fake error');
58+
}
59+
},
60+
{
61+
connection: {port: REDIS_SERVER_PORT},
62+
}
63+
);
64+
65+
const children = Array.from(Array(65).keys()).map((index) => ({
66+
name: 'child',
67+
data: {idx: index, foo: 'bar'},
68+
queueName,
69+
}));
70+
await flow.add({
71+
name: 'parent-job',
72+
queueName: parentQueueName,
73+
data: {},
74+
children,
75+
});
76+
77+
// adding delayed jobs
78+
const delayedJob = await queue.add('delayed', {}, {delay: 60 * 1000});
79+
delayedJob.log('Log message');
80+
81+
const arena = Arena(
82+
{
83+
BullMQ: Queue,
84+
85+
queues: [
86+
{
87+
// Required for each queue definition.
88+
name: queueName,
89+
90+
// User-readable display name for the host. Required.
91+
hostId: 'Queue Server 1',
92+
93+
// Queue type (Bull or Bullmq or Bee - default Bull).
94+
type: 'bullmq',
95+
96+
redis: {
97+
// host: 'localhost',
98+
port: REDIS_SERVER_PORT,
99+
},
100+
},
101+
{
102+
// Required for each queue definition.
103+
name: parentQueueName,
104+
105+
// User-readable display name for the host. Required.
106+
hostId: 'Queue Server 2',
107+
108+
// Queue type (Bull or Bullmq or Bee - default Bull).
109+
type: 'bullmq',
110+
111+
redis: {
112+
// host: 'localhost',
113+
port: REDIS_SERVER_PORT,
114+
},
115+
},
116+
],
117+
},
118+
{
119+
basePath: '/',
120+
disableListen: true,
121+
}
122+
);
123+
124+
await app.register(require('@fastify/express'));
125+
app.use('/arena', arena);
126+
127+
app.listen({port: HTTP_SERVER_PORT}, (err, address) => {
128+
if (err) throw err;
129+
console.log(`Arena listening on port ${HTTP_SERVER_PORT}!`);
130+
});
131+
}
132+
133+
main().catch((err) => {
134+
console.error(err);
135+
process.exit(1);
136+
});

example/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "An example project that uses Arena",
55
"main": "bee.js",
66
"scripts": {
7+
"start:fastify": "node fastify.js",
8+
"start:express": "node express.js",
79
"start:bee": "node bee.js",
810
"start:bull": "node bull.js",
911
"start:bullmq": "node bullmq.js",
@@ -13,10 +15,12 @@
1315
"author": "",
1416
"license": "MIT",
1517
"dependencies": {
18+
"@fastify/express": "^2.3.0",
1619
"bee-queue": "^1.4.0",
1720
"bull": "^3.22.6",
1821
"bullmq": "^3.0.0",
1922
"express": "^4.17.1",
23+
"fastify": "^4.13.0",
2024
"redis-server": "^1.2.2"
2125
}
2226
}

index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ function run(config, listenOpts = {}) {
99
Queues.useCdn =
1010
typeof listenOpts.useCdn !== 'undefined' ? listenOpts.useCdn : true;
1111

12-
app.locals.appBasePath = listenOpts.basePath || app.locals.appBasePath;
13-
14-
app.use(
15-
app.locals.appBasePath,
16-
express.static(path.join(__dirname, 'public'))
17-
);
18-
app.use(app.locals.appBasePath, routes);
19-
20-
const port = listenOpts.port || 4567;
21-
const host = listenOpts.host || '0.0.0.0'; // Default: listen to all network interfaces.
22-
if (!listenOpts.disableListen) {
12+
if (listenOpts.disableListen) {
13+
app.locals.appBasePath =
14+
listenOpts.basePath == '/' ? app.locals.appBasePath : listenOpts.basePath;
15+
app.use(
16+
listenOpts.basePath ? listenOpts.basePath : '/',
17+
express.static(path.join(__dirname, 'public'))
18+
);
19+
app.use(listenOpts.basePath ? listenOpts.basePath : '/', routes);
20+
} else {
21+
const appBasePath = listenOpts.basePath || app.locals.appBasePath;
22+
app.use(appBasePath, express.static(path.join(__dirname, 'public')));
23+
app.use(appBasePath, routes);
24+
const port = listenOpts.port || 4567;
25+
const host = listenOpts.host || '0.0.0.0'; // Default: listen to all network interfaces.
2326
app.listen(port, host, () => {
2427
console.log(`Arena is running on port ${port} at host ${host}`);
2528
});

src/server/views/dashboard/flowDetails.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ async function handler(req, res) {
55
const {Flows} = req.app.locals;
66
const flow = await Flows.get(connectionName, flowHost);
77
const basePath = req.baseUrl;
8+
89
if (!flow)
910
return res.status(404).render('dashboard/templates/flowNotFound', {
1011
basePath,

src/server/views/dashboard/queueDetails.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ async function handler(req, res) {
55
const {Queues, Flows} = req.app.locals;
66
const queue = await Queues.get(queueName, queueHost);
77
const basePath = req.baseUrl;
8+
89
if (!queue)
910
return res.status(404).render('dashboard/templates/queueNotFound', {
1011
basePath,

src/server/views/dashboard/queueJobsByState.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ async function _html(req, res) {
8080
const {Queues, Flows} = req.app.locals;
8181
const queue = await Queues.get(queueName, queueHost);
8282
const basePath = req.baseUrl;
83+
8384
if (!queue)
8485
return res.status(404).render('dashboard/templates/queueNotFound', {
8586
basePath,

0 commit comments

Comments
 (0)