-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathserver.js
More file actions
84 lines (72 loc) · 2.43 KB
/
server.js
File metadata and controls
84 lines (72 loc) · 2.43 KB
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
import express from 'express'
import {Queue} from 'bullmq'
import Redis from 'ioredis'
import {env} from 'node:process'
import { dirname } from 'node:path'
import { fileURLToPath, URL } from 'node:url'
// Get the directory this script is located in
const root = dirname(fileURLToPath(import.meta.url))
// Serve on PORT on Heroku and on localhost:5001 locally
const PORT = env.PORT || '5001'
// Connect to a local redis instance locally, and the Heroku-provided URL in production
const redisUrl = new URL(env.REDIS_URL || 'redis://127.0.0.1:6379')
const redisConnection = new Redis({
host: redisUrl.hostname,
port: redisUrl.port,
user: redisUrl.username,
password: redisUrl.password,
// Redis connections on Heroku use TLS to encrypt traffic + self-signed certificates so we'll
// configure this client with `rejectUnauthorized` to treat this connection as trusted.
// See: https://devcenter.heroku.com/articles/connecting-heroku-redis
tls: redisUrl.protocol === 'rediss:' ? { rejectUnauthorized: false } : false,
})
// Create / Connect to a named work queue
const workQueue = new Queue('work', {
connection: redisConnection
})
// Create our server app
const app = express()
// Serve the two static assets
app
.get('/', (req, res) => {
res.sendFile('index.html', { root })
})
.get('/client.js', (req, res) => {
res.sendFile('client.js', { root })
})
// Kick off a new job by adding it to the work queue
app.post('/job', async (req, res) => {
// This would be where you could pass arguments as job data
// Ex: workQueue.add('paint', { color: 'red' })
// Docs: https://api.docs.bullmq.io/classes/v5.Queue.html#add
const data = {} // empty for this example app
const job = await workQueue.add('example', data)
console.log(`Enqueued job: ${job.id}`)
res.json({
id: job.id
})
});
// Allows the client to query the state of a background job
app.get('/job/:id', async (req, res) => {
const { id } = req.params
const job = await workQueue.getJob(id)
if (job === null) {
res.status(404).end()
} else {
let state = await job.getState()
const { progress, failedReason } = job
res.json({
id,
state,
progress,
failedReason
})
}
});
// You can listen to events to get notified when jobs are processed
workQueue.on('removed', async (job) => {
console.log(`Job removed with result ${await job.getState()}`)
});
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`)
})