|
1 |
| -'use strict' |
2 |
| - |
3 |
| -const Queue = require('bull') |
| 1 | +"use strict" |
4 | 2 |
|
| 3 | +const Queue = require("bull") |
| 4 | +const redisUrlParse = require("redis-url-parse") |
5 | 5 | 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", |
9 | 9 | }
|
10 | 10 |
|
11 | 11 | // connect/create the submissions queue, attach listeners, and set global listeners
|
12 | 12 | 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 | +} |
33 | 48 |
|
34 | 49 | // listener function for queue progress updates
|
35 | 50 | 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 |
51 | 69 | }
|
52 | 70 |
|
53 | 71 | // listener function for queue completed jobs
|
54 | 72 | 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 | + } |
69 | 86 | }
|
70 | 87 |
|
71 | 88 | // 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 |
92 | 115 | }
|
0 commit comments