-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (38 loc) · 1.23 KB
/
index.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
import { createClient } from 'redis'
import view from '@fastify/view'
import nunjucks from 'nunjucks'
import fastifyCloudPrnt from '../../index.js'
const JOB_DATA = 'printJobData'
const PRINT_QUEUE = 'printJobQueue'
const REDIS_URL = process.env.REDIS_URL
export default async function basic (fastify, options) {
// redis connection url defaults to localhost on port 6379
const redis = createClient({ url: REDIS_URL })
await redis.connect()
await fastify.register(view, {
engine: { nunjucks },
root: './examples'
})
await fastify.register(fastifyCloudPrnt, {
queueJob: async (token, jobData) => {
await redis.HSET(JOB_DATA, token, JSON.stringify(jobData))
await redis.LPUSH(PRINT_QUEUE, token)
},
getJob: async () => {
const token = await redis.LINDEX(PRINT_QUEUE, 0)
return token === undefined ? null : token
},
getJobData: async token => {
const jsonJobData = await redis.HGET(JOB_DATA, token)
if (!jsonJobData) {
return null
}
return JSON.parse(jsonJobData)
},
deleteJob: async token => {
await redis.HDEL(JOB_DATA, token)
const deletedPrintJob = await redis.LREM(PRINT_QUEUE, 1, token)
return deletedPrintJob > 0
}
})
}