From 0eb93c71c454290e6865789ff772a842ebc73312 Mon Sep 17 00:00:00 2001 From: PieterBrandsen Date: Sun, 22 Jan 2023 19:16:17 +0100 Subject: [PATCH] Added docker stop command after finish --- example.env | 3 ++- package.json | 2 +- src/helper.js | 3 +-- src/index.js | 19 +++++++++++-------- src/setup.js | 3 ++- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/example.env b/example.env index 5ab90ff..f9aa377 100644 --- a/example.env +++ b/example.env @@ -1 +1,2 @@ -COMPOSE_PROJECT_NAME=screeps-server \ No newline at end of file +COMPOSE_PROJECT_NAME=screeps-server +COMPOSE_FILE=./docker-compose.yml \ No newline at end of file diff --git a/package.json b/package.json index 1c80a57..97413b7 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "screeps-api": "1.16.0" }, "scripts": { - "server": "node src/index.js --maxTicks=50000 --maxBots=5 --serverPort=21025 --cliPort=21026", + "server": "node src/index.js --maxTicks=10 --maxBots=5 --serverPort=21025 --cliPort=21026", "server-test": "node src/index.js --maxTicks=50000 --maxBots=5 --serverPort=21025 --cliPort=21026 --force", "lint": "eslint **/*", "lint-fix": "eslint **/* --fix" diff --git a/src/helper.js b/src/helper.js index d148324..38a821b 100644 --- a/src/helper.js +++ b/src/helper.js @@ -13,8 +13,7 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); import * as dotenv from 'dotenv'; dotenv.config({ path: join(__dirname, '../.env') }); -const dockerComposePath = join(__dirname, '../docker-compose.yml'); -const basicCommand = `docker-compose -f "${dockerComposePath}"`; +const basicCommand = "docker-compose"; const filter = { controller: (o) => { diff --git a/src/index.js b/src/index.js index 0762126..656d740 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +import { execSync } from 'child_process'; import Helper from './helper.js'; import Config from './config.js'; import Setup from "./setup.js" @@ -10,12 +11,16 @@ let lastTick = 0; const start = Date.now(); process.once('SIGINT', () => { - console.log('SIGINT received...'); - console.log(`${lastTick} End of simulation`); + console.log('Stop received...'); + const end = Date.now(); + console.log(`${lastTick} ticks elapsed, ${Math.floor((end - start) / 1000)} seconds`); console.log('Status:'); console.log(JSON.stringify(status, null, 2)); console.log('Milestones:'); console.log(JSON.stringify(Config.milestones, null, 2)); + console.log('Executing docker-compose stop'); + execSync('docker-compose stop', { stdio: 'ignore' }) + console.log('Exiting done...'); process.exit(); }); @@ -68,7 +73,7 @@ class Tester { setTimeout(() => { console.log('Timeout reached!'); process.exit(1); - }, Math.min(this.maxTicks, 40000) * 5000); + }, Math.min(this.maxTicks+1000, 20000) * 10000); } catch (e) { console.log(`Cannot parse runtime argument ${process.argv} ${e}`); } @@ -228,9 +233,9 @@ class Tester { } async run() { - if (!await Helper.startServer()) return; + if (!await Helper.startServer()) process.emit('SIGINT'); await Helper.sleep(10); - if (!await Helper.restartServer()) return; + if (!await Helper.restartServer()) process.emit('SIGINT'); await Helper.sleep(10); console.log('Starting... done'); @@ -242,8 +247,6 @@ class Tester { exitCode = 1; console.log(`${lastTick} ${e}`); } - const end = Date.now(); - console.log(`${lastTick} ticks elapsed, ${Math.floor((end - start) / 1000)} seconds`); process.exit(exitCode); } } @@ -258,5 +261,5 @@ class Tester { await Setup(); const tester = new Tester(); await tester.run(); - + process.emit('SIGINT'); })(); diff --git a/src/setup.js b/src/setup.js index 8c365c6..00f939c 100644 --- a/src/setup.js +++ b/src/setup.js @@ -75,9 +75,10 @@ function updateConfigFile() { function UpdateEnvFile() { const envFile = join(__dirname, '../.env'); if (fs.existsSync(envFile) && !argv.force) return console.log('Env file already exists, use --force to overwrite it'); + const dockerComposePath = join(__dirname, '../docker-compose.yml'); const exampleEnvFilePath = join(__dirname, '../example.env'); - let exampleEnvText = fs.readFileSync(exampleEnvFilePath, 'utf8'); + let exampleEnvText = fs.readFileSync(exampleEnvFilePath, 'utf8').replace('COMPOSE_FILE=./docker-compose.yml', `COMPOSE_FILE=${dockerComposePath}`); if (ports.serverPort) exampleEnvText = exampleEnvText.replaceAll('COMPOSE_PROJECT_NAME=screeps-server', `COMPOSE_PROJECT_NAME=screeps-server-${ports.serverPort}`); fs.writeFileSync(envFile, exampleEnvText); console.log('Env file created');