Skip to content

Commit

Permalink
Added docker stop command after finish
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterbrandsen committed Jan 22, 2023
1 parent efa93dc commit 0eb93c7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
3 changes: 2 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
COMPOSE_PROJECT_NAME=screeps-server
COMPOSE_PROJECT_NAME=screeps-server
COMPOSE_FILE=./docker-compose.yml
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
19 changes: 11 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { execSync } from 'child_process';
import Helper from './helper.js';
import Config from './config.js';
import Setup from "./setup.js"
Expand All @@ -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();
});

Expand Down Expand Up @@ -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}`);
}
Expand Down Expand Up @@ -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');
Expand All @@ -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);
}
}
Expand All @@ -258,5 +261,5 @@ class Tester {
await Setup();
const tester = new Tester();
await tester.run();

process.emit('SIGINT');
})();
3 changes: 2 additions & 1 deletion src/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 0eb93c7

Please sign in to comment.