Skip to content

Commit

Permalink
improved restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekliptor authored and Ekliptor committed Oct 18, 2018
1 parent 68b1db7 commit 4fb3198
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 17 deletions.
7 changes: 5 additions & 2 deletions public/js/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/js/bundle.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions public/js/constants.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions public/js/controllers/Config.ts
Expand Up @@ -82,9 +82,12 @@ export class Config extends TableController {
title: AppF.tr('restarting'),
text: AppF.tr('restartingTxt')
}
let disconnected = AppF.translate(pageData.html.misc.disablePage, vars);
let disconnected = AppF.translate(pageData.html.misc.restartDialog, vars);
$(AppClass.cfg.appSel).append(disconnected);
this.send({restart: true})
this.send({restart: true});
$("#reloadPage").click((event) => {
window.location.reload(true);
});
let checkRestartDone = (responseCount) => {
setTimeout(() => {
let data = new FormData(); // multipart POST data
Expand Down
2 changes: 2 additions & 0 deletions public/locales/en.json
Expand Up @@ -353,6 +353,8 @@
"restartConfirm": "Do you really want to restart the app?",
"restarting": "Restarting",
"restartingTxt": "The app is restarting. This page will automatically reload once the restart is done.",
"notReloading": "Is the page not reloading after 1 minute? Then just press the reload button below or in your browser.",
"reload": "Reload",
"pauseTrading": "Pause trading",
"resumeTrading": "Resume Trading",
"pauseOpeningPositions": "Pause opening new positions",
Expand Down
14 changes: 8 additions & 6 deletions src/Controller.ts
Expand Up @@ -272,23 +272,23 @@ export class Controller extends AbstractController { // TODO implement graceful

if (nconf.get("ai") === true) {
if (this.brain === null)
this.brain = new Brain(argv.config, this.exchangeConntroller.getExchanges());
this.brain = new Brain(this.exchangeConntroller.getConfigFilename(), this.exchangeConntroller.getExchanges());
if (argv.train)
tasks.push(this.brain.train());
scheduleAgain = !argv.train; // brain instance for training should only be used once
}
else if (nconf.get("lending") === true) {
if (this.lendingAdvisor === null)
this.lendingAdvisor = new LendingAdvisor(argv.config, this.exchangeConntroller);
this.lendingAdvisor = new LendingAdvisor(this.exchangeConntroller.getConfigFilename(), this.exchangeConntroller);
tasks.push(this.lendingAdvisor.process())
}
else if (nconf.get("social") === true) {
if (!this.socialController)
this.socialController = new SocialController(argv.config, this.exchangeConntroller);
this.socialController = new SocialController(this.exchangeConntroller.getConfigFilename(), this.exchangeConntroller);
tasks.push(this.socialController.process())
}
if (!this.tradeAdvisor) { // tradeAdvisor is always needed for websocket UI
this.tradeAdvisor = new TradeAdvisor(argv.config, this.exchangeConntroller)
this.tradeAdvisor = new TradeAdvisor(this.exchangeConntroller.getConfigFilename(), this.exchangeConntroller)
this.websocketController = new WebSocketController(this.serverSocket, this.tradeAdvisor, this.lendingAdvisor, this.socialController, this.brain);
}
if (nconf.get("ai") !== true && nconf.get("lending") !== true) {
Expand Down Expand Up @@ -430,7 +430,7 @@ export class Controller extends AbstractController { // TODO implement graceful
}

public async getStatus(req: http.IncomingMessage) {
const postData = utils.getJsonPostData((req as any).formFields);
const postData = req != null ? utils.getJsonPostData((req as any).formFields) : null;
let status = {
ready: false,
strategyInfos: this.tradeAdvisor ? this.tradeAdvisor.getStrategyInfos() : (this.lendingAdvisor ? this.lendingAdvisor.getStrategyInfos() : null),
Expand All @@ -444,7 +444,9 @@ export class Controller extends AbstractController { // TODO implement graceful
if (postData.prices === true)
status.prices = (await this.socialController.getPriceData(status.social)).toObject();
}
if (process.uptime() > 8.0 && this.isRunning === true && (status.strategyInfos !== null || status.social !== null)) // add more checks if we use different features later
// TODO isRunning ooesn't get set to true sometimes. why?
if (process.uptime() > 12.0 && /*this.isRunning === true && */(
status.strategyInfos !== null || status.social !== null || status.prices !== null)) // add more checks if we use different features later
status.ready = true;
return {data: status}
}
Expand Down
4 changes: 4 additions & 0 deletions src/ExchangeController.ts
Expand Up @@ -86,6 +86,10 @@ export default class ExchangeController extends AbstractSubController {
return this.exchangesIdle;
}

public getConfigFilename() {
return this.configFilename.replace(/\.json$/, "");
}

/**
* Loads a single new exchange instance. Useful for social crawler and other non-trading bots.
* @param {string} exchangeName
Expand Down
43 changes: 37 additions & 6 deletions src/WebSocket/ConfigEditor.ts
Expand Up @@ -124,8 +124,13 @@ export class ConfigEditor extends AppPublisher {
this.selectedConfig = this.advisor.getConfigName();
this.activeConfig = this.selectedConfig;
this.selectedTrader = this.getSelectedTrader();
nconf.set("debugRestart", nconf.get("debug"))
nconf.set("serverConfig:lastWorkingConfigName", this.selectedConfig);
nconf.set("debugRestart", nconf.get("debug"));
if (this.selectedTradingMode === "trading") {
setTimeout(() => {
if (typeof this.selectedConfig === "string" && this.selectedConfig.length !== 0)
nconf.set("serverConfig:lastWorkingConfigName", this.selectedConfig);
}, 5000);
}
serverConfig.saveConfigLocal();

// store results on exit (kill signal)
Expand Down Expand Up @@ -214,7 +219,9 @@ export class ConfigEditor extends AppPublisher {
protected onData(data: ConfigReq, clientSocket: ClientSocketOnServer, initialRequest: http.IncomingMessage): void {
if (typeof data.configChange === "string") {
this.readConfigFile(data.configChange).then((configFileData) => {
this.selectedConfig = this.getPlainConfigName(data.configChange);
let configName = this.getPlainConfigName(data.configChange);
if (configName)
this.selectedConfig = configName;
this.send(clientSocket, {
configFileData: configFileData
});
Expand Down Expand Up @@ -378,9 +385,20 @@ export class ConfigEditor extends AppPublisher {

public restart(forceDefaults = false) {
if (forceDefaults === true) {
this.selectedTradingMode = "trading";
// bad idea to just restore to "Noop" because the user might not have API keys or modified/broke that config
this.selectedConfig = nconf.get("serverConfig:lastWorkingConfigName");
let resetAll = false;
if (this.selectedTradingMode !== "trading") {
// try the 1st config file of that mode if we aren't already using it
let configFile = this.getFirstFileForMode(this.selectedTradingMode);
if (this.selectedConfig !== configFile)
this.selectedConfig = configFile;
else
resetAll = true;
}
if (resetAll === true) {
this.selectedTradingMode = "trading";
// bad idea to just restore to "Noop" because the user might not have API keys or modified/broke that config
this.selectedConfig = nconf.get("serverConfig:lastWorkingConfigName");
}
}
this.saveState().then(() => {
let processArgs = Object.assign([], process.execArgv)
Expand Down Expand Up @@ -1095,4 +1113,17 @@ export class ConfigEditor extends AppPublisher {
protected send(ws: ClientSocketOnServer, data: ConfigRes, options?: ServerSocketSendOptions) {
return super.send(ws, data, options);
}

protected getFirstFileForMode(mode: BotTrade.TradingMode) {
const dirPath = TradeConfig.getConfigDirForMode(mode);
try {
let files = fs.readdirSync(dirPath);
if (files.length > 0)
return files[0].replace(/\.json$/, "");
}
catch (err) {
logger.error("Error getting 1st config file for mode %s in %s", mode, dirPath);
}
return "";
}
}
13 changes: 13 additions & 0 deletions views/misc.html
Expand Up @@ -44,6 +44,19 @@ <h2>{title}</h2>
</div>
</div>
<!-- /disablePage -->
<!-- restartDialog -->
<div>
<div id="modal-background"></div>
<div id="modal" class="modal">
<div class="modal-inner">
<h2>{title}</h2>
<p>{text}</p>
<p>{tr:notReloading}</p>
<button id="reloadPage" class="btn btn-primary" type="button" name="reloadPage">{tr:reload}</button>
</div>
</div>
</div>
<!-- /restartDialog -->
<!-- error -->
<h1>{error}</h1>
<h1>{msg}</h1>
Expand Down

0 comments on commit 4fb3198

Please sign in to comment.