-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.ts
133 lines (116 loc) · 3.48 KB
/
adapter.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { crocks, isHyperErr, R } from './deps.ts'
import type { ImplConfig } from './types.ts'
import { Queue } from './lib/queue.ts'
import { Worker } from './lib/worker.ts'
const { Async } = crocks
const { always, ifElse } = R
const handleHyperErr = ifElse(
isHyperErr,
Async.Resolved,
Async.Rejected,
)
export function adapter(
{
fetch,
redis,
createQueue,
createWorker,
concurrency,
failedTtl,
keyPrefix,
}: ImplConfig,
) {
const $queue = (() => {
const queue = Queue(keyPrefix)
const queueClient = createQueue({ ...redis, keyPrefix })
return {
all: queue.all({ redis: redis.client }),
create: queue.create({ redis: redis.client }),
destroy: queue.destroy({ redis: redis.client }),
enqueue: queue.enqueue({ redis: redis.client, queue: queueClient }),
jobs: queue.jobs({ redis: redis.client, queue: queueClient }),
retry: queue.retry({ redis: redis.client, queue: queueClient }),
cancel: queue.cancel({ redis: redis.client, queue: queueClient }),
close: queue.close({ redis: redis.client, queue: queueClient }),
}
})()
/**
* Create workers according to the desired concurrency
*/
const $workers = (() => {
const worker = Worker(keyPrefix)
const processor = worker.process({ redis: redis.client, fetch, failedTtl })
const workerClients = Array(concurrency).fill(0).map(() =>
createWorker({ ...redis, processor, keyPrefix })
)
return {
closeAll: () => Promise.all(workerClients.map((wc) => worker.close({ worker: wc })())),
}
})()
Deno.addSignalListener(
'SIGINT',
() =>
Promise.resolve()
.then(() => $workers.closeAll())
.then(() => $queue.close())
.then(() => redis.client.quit())
.then(() => Deno.exit()),
)
function index() {
return $queue.all()
.map((queues) => ({ ok: true, queues }))
.bichain(handleHyperErr, Async.Resolved)
.toPromise()
}
function create({ name, target, secret }: { name: string; target: string; secret?: string }) {
return Async.of({ name, target, secret })
.chain($queue.create)
.map(always({ ok: true }))
.bichain(handleHyperErr, Async.Resolved)
.toPromise()
}
function destroy(name: string) {
return Async.of(name)
.chain($queue.destroy)
.map(always({ ok: true }))
.bichain(handleHyperErr, Async.Resolved)
.toPromise()
}
function post({ name, job }: { name: string; job: Record<string, unknown> }) {
return Async.of({ name, job })
.chain($queue.enqueue)
.map((job) => ({ ok: true, id: job.id }))
.bichain(handleHyperErr, Async.Resolved)
.toPromise()
}
function get({ name, status }: { name: string; status: 'READY' | 'ERROR' }) {
return Async.of({ name, status })
.chain($queue.jobs)
.map((jobs) => ({ ok: true, jobs }))
.bichain(handleHyperErr, Async.Resolved)
.toPromise()
}
function retry({ name, id }: { name: string; id: string }) {
return Async.of({ name, id })
.chain($queue.retry)
.map(() => ({ ok: true, id }))
.bichain(handleHyperErr, Async.Resolved)
.toPromise()
}
function cancel({ name, id }: { name: string; id: string }) {
return Async.of({ name, id })
.chain($queue.cancel)
.map(() => ({ ok: true, id }))
.bichain(handleHyperErr, Async.Resolved)
.toPromise()
}
return Object.freeze({
index,
create,
delete: destroy,
post,
get,
retry,
cancel,
})
}