forked from peter-mount/notify-rabbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
167 lines (148 loc) · 6.64 KB
/
config.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* config.js Handles our new yaml style configuration
*/
var DSNParser = require('dsn-parser');
var yaml = require('js-yaml'),
fs = require('fs'),
pgp = require('pg-promise')(options),
promise = require('bluebird'),
options = {
// Initialization Options
promiseLib: promise,
// global event notification;
error: function (error, e) {
if (e.cn) {
// A connection-related error;
//
// Connections are reported back with the password hashed,
// for safe errors logging, without exposing passwords.
console.log("CN:", e.cn);
console.log("EVENT:", error.message || error);
}
}
};
var config = {},
databases = {};
function $main(options) {
// Load the config file
try {
config = yaml.load(fs.readFileSync(
options.config ? options.config : 'config.yaml',
'utf8'));
} catch (e) {
console.error(e);
}
// We need the databases loaded
if (options.databases && config.databases)
databases = Object.keys(config.databases)
.reduce(function (a, b) {
var c = config.databases[b];
if (c.enabled) {
if (c.dsn) {
var url = c.dsn;
if (Array.from(url)[0] === '$') {
url = process.env[url.substring(1)];
}
var dsn = new DSNParser(url).getParts();
a[b] = pgp({
host: dsn.host,
port: dsn.port,
database: dsn.database,
user: dsn.user,
password: dsn.password,
ssl: c.ssl ? { rejectUnauthorized: false } : false,
keepAlive: true
});
} else {
a[b] = pgp({
host: c.host,
port: c.port ? c.port : 5432,
database: c.database,
user: c.user,
password: c.password,
ssl: c.ssl ? { rejectUnauthorized: false } : false,
keepAlive: true
});
}
}
return a;
}, {});
return {
// Link to the pg-promise databases
db: databases,
// Link to the configuraton
config: config,
// Link to enable notify code
notify: notify
};
}
// =============================================================================
function notify(handlers) {
if (config.notify) {
config.notify
.filter(function (n) {
return n.enabled === true;
})
.reduce(function (a, n) {
var db = databases[n.database];
// Add any handlers
var actions = n.handlers ? Object.keys(handlers)
.filter(function (b) {
return n.handlers[b];
})
.reduce(function (a, b) {
// Call the handler with config, listener and the handler's config
// expect a function back that accepts the payload or null to ignore
var f = handlers[b](config, n, n.handlers[b]);
if (f)
a.push(f);
return a;
}, [])
: [];
if (actions && actions.length) {
db.connect({direct: true})
.then(function (sco) {
sco.client.on('notification', function (data) {
try {
// Optional debug, log the message as we receive it
if (n.debug)
console.log('Notify:\t' + Object.keys(data)
.reduce(function (a, b) {
a.push([b, data[b]].join(b.length < 8 ? '\t\t' : '\t'));
return a;
}, [])
.join('\n\t'));
// The payload, either string or json as per config
var payload = n.json ? JSON.parse(data.payload) : data.payload;
actions.forEach(function (f) {
// Try block so an action doesn't cause us to fail
try {
f(payload);
} catch (e) {
console.error(e);
}
});
} catch (e) {
console.error(e);
}
});
return sco.none('LISTEN $1~', n.name);
})
.catch(function (error) {
console.error('Error: ', error);
// Exit the process. In production this will cause docker to restart the entire application
process.exit(1);
});
}
return a;
}, {});
}
}
// =============================================================================
module.exports = $main;
//module.exports = {
// getStatus: getStatus,
// setFail: setFail,
// setRun: setRun,
// setSuccess: setSuccess
//};