-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
55 lines (47 loc) · 1.37 KB
/
utils.js
File metadata and controls
55 lines (47 loc) · 1.37 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
52
53
54
55
const https = require('https');
/**
* Stop execution for a given number of milliseconds
*
* @param {number} ms - number of milliseconds to stop execution
* @returns {Promise<void>}
*/
module.exports.sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
/**
* Sends alert to the Slack/Telegram
*
* @param {string} text - message to send
* @returns {Promise<void>}
*/
module.exports.sendReport = async function sendReport(text) {
const message = `🦩 Hawk workers (${process.env.ENVIRONMENT_NAME || 'unknown'}) | ${text}`;
const postData = 'parse_mode=Markdown&message=' + encodeURIComponent(message);
const endpoint = process.env.CODEX_BOT_WEBHOOK;
if (!endpoint) {
return;
}
return new Promise((resolve) => {
const request = https.request(endpoint, {
method: 'POST',
timeout: 3000,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}, (response) => {
response.setEncoding('utf8');
response.on('data', (chunk) => {
console.log('📤 Reporting:', chunk);
resolve();
});
});
request.on('error', (e) => {
console.log('📤 Reporting failed:', e);
/**
* Does not throw error, so we don't need to catch it higher
* and the application will not exit
*/
resolve();
});
request.write(postData);
request.end();
});
};