-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.js
100 lines (79 loc) · 2.72 KB
/
process.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict'
require('dotenv').config()
const Queue = require('bull')
const express = require('express')
const server = express()
const promClient = require('prom-client')
const { faker } = require('@faker-js/faker')
const { config } = require('./config')
const { randomSleep, logMemory } = require('./utils')
console.log(`[${config.env}] process app starting`)
if(!process.env.SECRET_KEY) throw new Error('SECRET_KEY: This private key is necessary')
promClient.collectDefaultMetrics({
timeout: 10000,
gcDurationBuckets: [0.001, 0.01, 0.1, 1, 2, 5], // These are the default buckets.
})
const videoQueue = new Queue('video transcoding', { ...config.redis })
const imageQueue = new Queue('image transcoding', { ...config.redis })
const dataProcessed = []
const createProcessor = (name = 'default') => async (job = {}) => {
console.log(`[${config.env}:${job.id}] processing ${job.queue.name}...`)
dataProcessed.push({
job,
metadata: faker.random.words(500_000)
})
// processing long running job (up to 3s)
await randomSleep(4)
if (job.id % 8 === 0) throw new Error('some unexpected error')
console.log(`[${config.env}:${job.id}] processing ${job.queue.name} done`)
return {
metadata: faker.random.words(500_000)
}
}
const videoProcessor = createProcessor('video')
const imageProcessor = createProcessor('image')
/*
* Processing queues
*/
videoQueue.process(function (job) {
return videoProcessor(job)
})
imageQueue.process(function (job) {
return imageProcessor(job)
})
videoQueue.on('completed', async function (job, result) {
const videoJobCounts = await job.queue.getJobCounts()
console.log(`[${config.env}:video] count jobs - waiting: ${videoJobCounts.waiting}, completed: ${videoJobCounts.completed}, failed: ${videoJobCounts.failed}`)
})
imageQueue.on('completed', async function (job, result) {
const videoJobCounts = await job.queue.getJobCounts()
console.log(`[${config.env}:image] count jobs - waiting: ${videoJobCounts.waiting}, completed: ${videoJobCounts.completed}, failed: ${videoJobCounts.failed}`)
})
/*
* Server part
*/
server.get('/', (req, res) => {
res.send('Hello World!')
})
server.get('/healthz', async (req, res) => {
try {
res.send({
healthy: true,
processingData: dataProcessed.length,
app: 'dummy-app',
env: config.env
})
} catch (ex) {
res.status(500).end(ex)
}
})
server.get('/metrics', async (req, res) => {
try {
res.set('Content-Type', promClient.register.contentType)
res.end(await promClient.register.metrics())
} catch (ex) {
res.status(500).end(ex)
}
})
console.log(`[${config.env}] Server listening on :${config.server.port}, metrics exposed on /metrics endpoint, health exposed on /healthz endpoint`)
server.listen(config.server.port)