-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
51 lines (42 loc) · 1.2 KB
/
Copy pathapp.js
File metadata and controls
51 lines (42 loc) · 1.2 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
import { createClient } from 'redis';
import { DispatchedEvent } from './lib/DispatchedEvent.js';
import { config } from './config.js';
import { instance as dispatcher } from './lib/GSDispatcher.js';
const opts = {}
opts.host = config.redis.host;
opts.port = config.redis.port;
if (config.redis.password) {
opts.password = config.redis.password;
}
// Create a redis client
const client = createClient(opts);
// Subscribe to the in channel
client.subscribe(config.redis.in_channel, async (data) => {
try {
const event = new DispatchedEvent(data)
await event.run()
await event.save()
} catch (e) {
console.log("Redis Client Subscribe Err", e);
return false
}
return true
});
// Setup error update handler
client.on('error', async (err) => {
console.log('error', err);
await dispatcher.updateStatus({}, 'error');
});
// Setup connect update handler
client.on("connect", async () => {
console.log('Redis Client Connected');
await dispatcher.updateStatus({}, 'ready');
});
// Connect to the redis server
try {
await client.connect();
} catch (e) {
console.log("Redis Client Connect Err", e);
}
// Export the client
export default client;