Skip to content

Commit 23801b2

Browse files
committed
add tls on server
1 parent 9b6d764 commit 23801b2

File tree

3 files changed

+187
-118
lines changed

3 files changed

+187
-118
lines changed
Lines changed: 98 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,115 @@
1-
'use strict'
2-
3-
const Queue = require('bull')
1+
"use strict"
42

3+
const Queue = require("bull")
4+
const redisUrlParse = require("redis-url-parse")
55
const progressStatusMap = {
6-
0: 'CREATED', // never set - just for reference
7-
50: 'COMPILING',
8-
100: 'COMPLETED'
6+
0: "CREATED", // never set - just for reference
7+
50: "COMPILING",
8+
100: "COMPLETED",
99
}
1010

1111
// connect/create the submissions queue, attach listeners, and set global listeners
1212
module.exports.initCompileQueue = () => {
13-
14-
try {
15-
// get queue url
16-
const url = strapi.config.get('compile_queue.url')
17-
18-
// connect to queue
19-
const compile_queue = new Queue('submissions', url)
20-
21-
// add the submission progress listener
22-
compile_queue.on('global:progress', strapi.services.submission.updateProgress)
23-
24-
// add the submission complete listener
25-
compile_queue.on('global:completed', strapi.services.submission.completeJob)
26-
27-
// add queue globally
28-
strapi.connections.compile_queue = compile_queue
29-
} catch(err) {
30-
console.error('err init queue', err)
31-
}
32-
}
13+
try {
14+
// get queue url
15+
const url = strapi.config.get("compile_queue.url")
16+
const { host, port, password } = redisUrlParse(url)
17+
const bullOptions = url.includes("rediss://")
18+
? {
19+
redis: {
20+
port: Number(port),
21+
host,
22+
password,
23+
tls: {
24+
rejectUnauthorized: false,
25+
requestCert: true,
26+
},
27+
},
28+
}
29+
: url
30+
// connect to queue
31+
const compile_queue = new Queue("submissions", bullOptions)
32+
33+
// add the submission progress listener
34+
compile_queue.on(
35+
"global:progress",
36+
strapi.services.submission.updateProgress
37+
)
38+
39+
// add the submission complete listener
40+
compile_queue.on("global:completed", strapi.services.submission.completeJob)
41+
42+
// add queue globally
43+
strapi.connections.compile_queue = compile_queue
44+
} catch (err) {
45+
console.error("err init queue", err)
46+
}
47+
}
3348

3449
// listener function for queue progress updates
3550
module.exports.updateProgress = async (jobId, progress) => {
36-
37-
try {
38-
const status = progressStatusMap[progress]
39-
40-
// let completeJob handle last progress
41-
if (progress == 100) return
42-
43-
// get the submission_id from the job
44-
const { data } = await strapi.connections.compile_queue.getJob(jobId)
45-
46-
// update the submission
47-
await strapi.services.submission.update({ id: data.submissionId }, { status })
48-
} catch (err) {
49-
console.error(`err updating job ${jobId} progress`, err)
50-
}5
51+
try {
52+
const status = progressStatusMap[progress]
53+
54+
// let completeJob handle last progress
55+
if (progress == 100) return
56+
57+
// get the submission_id from the job
58+
const { data } = await strapi.connections.compile_queue.getJob(jobId)
59+
60+
// update the submission
61+
await strapi.services.submission.update(
62+
{ id: data.submissionId },
63+
{ status }
64+
)
65+
} catch (err) {
66+
console.error(`err updating job ${jobId} progress`, err)
67+
}
68+
5
5169
}
5270

5371
// listener function for queue completed jobs
5472
module.exports.completeJob = async (jobId, result) => {
55-
56-
try {
57-
// get the submission_id from the job
58-
const { data } = await strapi.connections.compile_queue.getJob(jobId)
59-
60-
// parse the results
61-
const updates = JSON.parse(result)
62-
updates.status = 'COMPLETED'
63-
64-
// update the submission
65-
await strapi.services.submission.update({ id: data.submissionId }, updates)
66-
} catch (err) {
67-
console.error(`err completing job ${jobId}`, err)
68-
}
73+
try {
74+
// get the submission_id from the job
75+
const { data } = await strapi.connections.compile_queue.getJob(jobId)
76+
77+
// parse the results
78+
const updates = JSON.parse(result)
79+
updates.status = "COMPLETED"
80+
81+
// update the submission
82+
await strapi.services.submission.update({ id: data.submissionId }, updates)
83+
} catch (err) {
84+
console.error(`err completing job ${jobId}`, err)
85+
}
6986
}
7087

7188
// create a submission entry and add a job to the queue
72-
module.exports.startJob = async (config) => {
73-
74-
// create the submission
75-
const { board, sketch, id: submissionId } = await strapi.services.submission.create({
76-
...config,
77-
status: 'CREATED'
78-
})
79-
80-
// add the submission to the queue
81-
const { id: job_id } = await strapi.connections.compile_queue.add({
82-
board,
83-
sketch,
84-
submissionId
85-
})
86-
87-
// add the job_id to the submission
88-
const submission = await strapi.services.submission.update({ id: submissionId }, { job_id })
89-
90-
// return the new submission
91-
return submission
89+
module.exports.startJob = async config => {
90+
// create the submission
91+
const {
92+
board,
93+
sketch,
94+
id: submissionId,
95+
} = await strapi.services.submission.create({
96+
...config,
97+
status: "CREATED",
98+
})
99+
100+
// add the submission to the queue
101+
const { id: job_id } = await strapi.connections.compile_queue.add({
102+
board,
103+
sketch,
104+
submissionId,
105+
})
106+
107+
// add the job_id to the submission
108+
const submission = await strapi.services.submission.update(
109+
{ id: submissionId },
110+
{ job_id }
111+
)
112+
113+
// return the new submission
114+
return submission
92115
}

server/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
"build-client": "rm -rf ./public/client/* && cd ../client && env PUBLIC_URL=/client yarn build && mv ./build/* ../server/public/client/"
1212
},
1313
"dependencies": {
14-
"bull": "^3.29.0",
14+
"bull": "^4.10.0",
1515
"dotenv": "^10.0.0",
1616
"knex": "0.21.18",
1717
"pg": "^8.7.1",
18+
"redis-url-parse": "^2.0.0",
1819
"sharp": "^0.29.3",
1920
"strapi": "3.6.7",
2021
"strapi-admin": "3.6.7",

0 commit comments

Comments
 (0)