Skip to content

Commit

Permalink
add tls on server
Browse files Browse the repository at this point in the history
  • Loading branch information
mikelxk committed Oct 12, 2022
1 parent 9b6d764 commit 23801b2
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 118 deletions.
173 changes: 98 additions & 75 deletions server/api/submission/services/submission.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,115 @@
'use strict'

const Queue = require('bull')
"use strict"

const Queue = require("bull")
const redisUrlParse = require("redis-url-parse")
const progressStatusMap = {
0: 'CREATED', // never set - just for reference
50: 'COMPILING',
100: 'COMPLETED'
0: "CREATED", // never set - just for reference
50: "COMPILING",
100: "COMPLETED",
}

// connect/create the submissions queue, attach listeners, and set global listeners
module.exports.initCompileQueue = () => {

try {
// get queue url
const url = strapi.config.get('compile_queue.url')

// connect to queue
const compile_queue = new Queue('submissions', url)

// add the submission progress listener
compile_queue.on('global:progress', strapi.services.submission.updateProgress)

// add the submission complete listener
compile_queue.on('global:completed', strapi.services.submission.completeJob)

// add queue globally
strapi.connections.compile_queue = compile_queue
} catch(err) {
console.error('err init queue', err)
}
}
try {
// get queue url
const url = strapi.config.get("compile_queue.url")
const { host, port, password } = redisUrlParse(url)
const bullOptions = url.includes("rediss://")
? {
redis: {
port: Number(port),
host,
password,
tls: {
rejectUnauthorized: false,
requestCert: true,
},
},
}
: url
// connect to queue
const compile_queue = new Queue("submissions", bullOptions)

// add the submission progress listener
compile_queue.on(
"global:progress",
strapi.services.submission.updateProgress
)

// add the submission complete listener
compile_queue.on("global:completed", strapi.services.submission.completeJob)

// add queue globally
strapi.connections.compile_queue = compile_queue
} catch (err) {
console.error("err init queue", err)
}
}

// listener function for queue progress updates
module.exports.updateProgress = async (jobId, progress) => {

try {
const status = progressStatusMap[progress]

// let completeJob handle last progress
if (progress == 100) return

// get the submission_id from the job
const { data } = await strapi.connections.compile_queue.getJob(jobId)

// update the submission
await strapi.services.submission.update({ id: data.submissionId }, { status })
} catch (err) {
console.error(`err updating job ${jobId} progress`, err)
}5
try {
const status = progressStatusMap[progress]

// let completeJob handle last progress
if (progress == 100) return

// get the submission_id from the job
const { data } = await strapi.connections.compile_queue.getJob(jobId)

// update the submission
await strapi.services.submission.update(
{ id: data.submissionId },
{ status }
)
} catch (err) {
console.error(`err updating job ${jobId} progress`, err)
}
5
}

// listener function for queue completed jobs
module.exports.completeJob = async (jobId, result) => {

try {
// get the submission_id from the job
const { data } = await strapi.connections.compile_queue.getJob(jobId)

// parse the results
const updates = JSON.parse(result)
updates.status = 'COMPLETED'

// update the submission
await strapi.services.submission.update({ id: data.submissionId }, updates)
} catch (err) {
console.error(`err completing job ${jobId}`, err)
}
try {
// get the submission_id from the job
const { data } = await strapi.connections.compile_queue.getJob(jobId)

// parse the results
const updates = JSON.parse(result)
updates.status = "COMPLETED"

// update the submission
await strapi.services.submission.update({ id: data.submissionId }, updates)
} catch (err) {
console.error(`err completing job ${jobId}`, err)
}
}

// create a submission entry and add a job to the queue
module.exports.startJob = async (config) => {

// create the submission
const { board, sketch, id: submissionId } = await strapi.services.submission.create({
...config,
status: 'CREATED'
})

// add the submission to the queue
const { id: job_id } = await strapi.connections.compile_queue.add({
board,
sketch,
submissionId
})

// add the job_id to the submission
const submission = await strapi.services.submission.update({ id: submissionId }, { job_id })

// return the new submission
return submission
module.exports.startJob = async config => {
// create the submission
const {
board,
sketch,
id: submissionId,
} = await strapi.services.submission.create({
...config,
status: "CREATED",
})

// add the submission to the queue
const { id: job_id } = await strapi.connections.compile_queue.add({
board,
sketch,
submissionId,
})

// add the job_id to the submission
const submission = await strapi.services.submission.update(
{ id: submissionId },
{ job_id }
)

// return the new submission
return submission
}
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
"build-client": "rm -rf ./public/client/* && cd ../client && env PUBLIC_URL=/client yarn build && mv ./build/* ../server/public/client/"
},
"dependencies": {
"bull": "^3.29.0",
"bull": "^4.10.0",
"dotenv": "^10.0.0",
"knex": "0.21.18",
"pg": "^8.7.1",
"redis-url-parse": "^2.0.0",
"sharp": "^0.29.3",
"strapi": "3.6.7",
"strapi-admin": "3.6.7",
Expand Down
Loading

0 comments on commit 23801b2

Please sign in to comment.