Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: removing indentation for readability #136

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 40 additions & 39 deletions restart_app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ setTimeout(function () {
// Start checking for client updates
checkForClientUpdate()

function checkForClientUpdate () {
function checkForClientUpdate() {
if (isRestarting) {
if (debug) console.log("RestartApp::We're restarting, skipping client update check..")
return
Expand Down Expand Up @@ -52,7 +52,11 @@ function checkForClientUpdate () {
})
}

function restart () {
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

function restart() {
if (debug) console.log('RestartApp::Restarting..')
if (isRestarting) {
if (debug) console.log("RestartApp::We're already restarting..")
Expand All @@ -66,45 +70,42 @@ function restart () {

var WebSocket = require('ws')
var ws = new WebSocket('ws://' + serverHostname + ':' + serverPort + '/' + serverPassword)
ws.on('open', function open () {
setTimeout(function () {
ws.send(createPacket("say NOTICE: We're updating the server in <color=orange>5 minutes</color>, so get to a safe spot!"))
setTimeout(function () {
ws.send(createPacket("say NOTICE: We're updating the server in <color=orange>4 minutes</color>, so get to a safe spot!"))
setTimeout(function () {
ws.send(createPacket("say NOTICE: We're updating the server in <color=orange>3 minutes</color>, so get to a safe spot!"))
setTimeout(function () {
ws.send(createPacket("say NOTICE: We're updating the server in <color=orange>2 minutes</color>, so get to a safe spot!"))
setTimeout(function () {
ws.send(createPacket("say NOTICE: We're updating the server in <color=orange>1 minute</color>, so get to a safe spot!"))
setTimeout(function () {
ws.send(createPacket('global.kickall <color=orange>Updating/Restarting</color>'))
setTimeout(function () {
ws.send(createPacket('quit'))
// ws.send(createPacket("restart 60")); // NOTE: Don't use restart, because that doesn't actually restart the container!
setTimeout(function () {
ws.close(1000)

// After 2 minutes, if the server's still running, forcibly shut it down
setTimeout(function () {
var fs = require('fs')
fs.unlinkSync('/tmp/restart_app.lock')

var childProcess = require('child_process')
childProcess.execSync('kill -s 2 $(pidof bash)')
}, 1000 * 60 * 2)
}, 1000)
}, 1000)
}, 1000 * 60)
}, 1000 * 60)
}, 1000 * 60)
}, 1000 * 60)
}, 1000 * 60)
}, 1000)
})
ws.on('open', async function open() {
await sleep(1000);
ws.send(createPacket("say NOTICE: We're updating the server in <color=orange>5 minutes</color>, so get to a safe spot!"));

await sleep(1000 * 60);
ws.send(createPacket("say NOTICE: We're updating the server in <color=orange>4 minutes</color>, so get to a safe spot!"));

await sleep(1000 * 60);
ws.send(createPacket("say NOTICE: We're updating the server in <color=orange>3 minutes</color>, so get to a safe spot!"));

await sleep(1000 * 60);
ws.send(createPacket("say NOTICE: We're updating the server in <color=orange>2 minutes</color>, so get to a safe spot!"));

await sleep(1000 * 60);
ws.send(createPacket("say NOTICE: We're updating the server in <color=orange>1 minute</color>, so get to a safe spot!"));

await sleep(1000 * 60);
ws.send(createPacket('global.kickall <color=orange>Updating/Restarting</color>'));

await sleep(1000);
ws.send(createPacket('quit'));

await sleep(1000);
ws.close(1000);

// After 2 minutes, if the server's still running, forcibly shut it down
await sleep(1000 * 60 * 2);
var fs = require('fs');
fs.unlinkSync('/tmp/restart_app.lock');

var childProcess = require('child_process');
childProcess.execSync('kill -s 2 $(pidof bash)');
});
}

function createPacket (command) {
function createPacket(command) {
var packet =
{
Identifier: -1,
Expand Down