-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedisAdapter.ts
64 lines (51 loc) · 2.19 KB
/
RedisAdapter.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
import { Subject } from "rxjs"
import ioRedis from "ioredis";
import {AdapterInterface} from "../Interfaces/AdapterInterface";
import {QueryWatcherJsService} from "../Services/QueryWatcherJsService";
import {Log} from "../Utilities/Log";
const host = process.env.REDIS_HOST ?? 'localhost';
const port:number = process.env.REDIS_PORT ? parseInt(process.env.REDIS_PORT) : 6379;
export class RedisAdapter implements AdapterInterface{
protected host:string;
protected port:number;
protected channel:string;
protected Server:any;
protected $Messages: Subject<any> = new Subject();
constructor(channel:string) {
const queryWatcherJsService = QueryWatcherJsService.getInstance();
queryWatcherJsService.setParameter('-rh --redis_host <driver>');
queryWatcherJsService.setParameter('-rp --redis_port <port>');
Log.info('Adapter\'s extra Params:',queryWatcherJsService.parameters);
this.host = queryWatcherJsService.parameters.redis_host || host;
this.port = parseInt(queryWatcherJsService.parameters.redis_port) || port;
this.channel = channel;
this.connect(this.host, this.port, this.channel);
}
connect(host:string,port:number, channel:string){
console.log(`Connecting to ${host}:${port}`)
this.Server = new ioRedis(port, host);
this.Server.subscribe(channel, (err: { message: any; }, count: number) => {
if (err) {
console.error("Failed to subscribe: %s", err.message);
process.exit(1);
} else {
Log.debug(`Subscribed successfully! This client is currently subscribed to ${count} channels.`);
}
});
this.Server.on('message', (channel: string, message: any) => {
message = JSON.parse(message);
//io.emit(channel + ':' + message.event, message.data);
this.$Messages.next(message);
});
this.Server.on('error', (error: any) => {
console.error('Error during connecting to the service:\n', error);
process.exit(1);
});
}
onMessage(){
return this.$Messages;
}
getServer(){
return this.Server;
}
}