-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (41 loc) · 1.83 KB
/
index.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
const schedule = require('node-schedule');
const { spawn } = require("child_process");
const { sendWebhook } = require("./utilities/webhook");
const { extractSize } = require("./utilities/extractSize");
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '.env') });
//collect rule times for cron job
let rule = `${process.env.RULE_SECOND} ${process.env.RULE_MINUTE} ${process.env.RULE_HOUR} ${process.env.RULE_DATE} ${process.env.RULE_MONTH} ${process.env.RULE_WEEK_DAY}`;
//start cron
const job = schedule.scheduleJob(rule, function(){
//command to service restart wings
let wings = spawn('docker', ['image', 'prune', '-a', '-f']);
//if the command runs correctly
wings.stdout.on('data', (data) => {
let response = data.toString();
//if the response contains the reclaimed space
if(response.includes("space:")){
let size = extractSize(data.toString());
sendWebhook(`${process.env.TITLE} Succesfully Ran And Reclaimed ${size}`, process.env.DISCORD_URL, false);
//if not assume command didnt run correctly
}else{
sendWebhook(`${process.env.TITLE} Failed To Run`, process.env.DISCORD_URL, true);
}
});
//if the command returns errors
wings.stderr.on('data', (data) => {
sendWebhook(`${process.env.TITLE} Failed To Run`, process.env.DISCORD_URL, true);
});
//if the command window fails to start
wings.on('error', function(err){
sendWebhook(`${process.env.TITLE} Failed To Run`, process.env.DISCORD_URL, true);
});
wings.on('exit', (code) => {
//if the command runs incorrectly
if(code != 0){
sendWebhook(`${process.env.TITLE} Failed To Run`, process.env.DISCORD_URL, true);
}
});
//close window
wings.stdin.end();
});