Skip to content

Commit

Permalink
Merge pull request hummingbot#176 from CoinAlpha/bug/sc-25624/client-…
Browse files Browse the repository at this point in the history
…restart-docker-when-calling-config

gateway no longer tries to autorestart (it was broken), now hummingbo…
  • Loading branch information
Martin Kou authored Mar 30, 2022
2 parents ccae372 + 9dca8b4 commit 18da4f2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 76 deletions.
29 changes: 4 additions & 25 deletions gateway/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable @typescript-eslint/ban-types */
import express from 'express';
import { Request, Response, NextFunction } from 'express';
import { Server } from 'http';
import { SolanaRoutes } from './chains/solana/solana.routes';
import { WalletRoutes } from './services/wallet/wallet.routes';
import { logger, updateLoggerToStdout } from './services/logger';
import { logger } from './services/logger';
import { addHttps } from './https';
import {
asyncHandler,
Expand All @@ -26,8 +25,6 @@ import morgan from 'morgan';
const swaggerUi = require('swagger-ui-express');

export const gatewayApp = express();
let gatewayServer: Server;
let swaggerServer: Server;

// parse body for application/json
gatewayApp.use(express.json());
Expand Down Expand Up @@ -95,19 +92,6 @@ gatewayApp.post(
req.body.configValue
);

logger.info('Reload logger to stdout.');
updateLoggerToStdout();

logger.info('Reloading Ethereum routes.');
// EthereumRoutes.reload();

logger.info('Reloading Solana routes.');
SolanaRoutes.reload();

logger.info('Restarting gateway.');
await stopGateway();
await startGateway();

res.status(200).json({ message: 'The config has been updated' });
}
)
Expand Down Expand Up @@ -151,7 +135,7 @@ export const startSwagger = async () => {

swaggerApp.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

swaggerServer = await swaggerApp.listen(swaggerPort);
await swaggerApp.listen(swaggerPort);
};

export const startGateway = async () => {
Expand All @@ -165,10 +149,10 @@ export const startGateway = async () => {
logger.info(`⚡️ Gateway API listening on port ${port}`);
if (ConfigManagerV2.getInstance().get('server.unsafeDevModeWithHTTP')) {
logger.info('Running in UNSAFE HTTP! This could expose private keys.');
gatewayServer = await gatewayApp.listen(port);
await gatewayApp.listen(port);
} else {
try {
gatewayServer = await addHttps(gatewayApp).listen(port);
await addHttps(gatewayApp).listen(port);
} catch (e) {
logger.error(
`Failed to start the server with https. Confirm that the SSL certificate files exist and are correct. Error: ${e}`
Expand All @@ -180,8 +164,3 @@ export const startGateway = async () => {

await startSwagger();
};

const stopGateway = async () => {
await swaggerServer.close();
return gatewayServer.close();
};
54 changes: 5 additions & 49 deletions hummingbot/client/command/gateway_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
GATEWAY_DOCKER_TAG,
GatewayPaths,
get_default_gateway_port,
start_gateway,
stop_gateway
)
from hummingbot.core.gateway.status_monitor import Status
from hummingbot.core.utils.async_utils import safe_ensure_future
Expand Down Expand Up @@ -61,13 +63,13 @@ def gateway_connect(self, connector: str = None):
safe_ensure_future(self._gateway_connect(connector), loop=self.ev_loop)

def gateway_start(self):
safe_ensure_future(self._start_gateway(), loop=self.ev_loop)
safe_ensure_future(start_gateway(), loop=self.ev_loop)

def gateway_status(self):
safe_ensure_future(self._gateway_status(), loop=self.ev_loop)

def gateway_stop(self):
safe_ensure_future(self._stop_gateway(), loop=self.ev_loop)
safe_ensure_future(stop_gateway(), loop=self.ev_loop)

def generate_certs(self):
safe_ensure_future(self._generate_certs(), loop=self.ev_loop)
Expand All @@ -88,52 +90,6 @@ async def check_gateway_image(docker_repo: str, docker_tag: str) -> bool:
image_list: List = await docker_ipc("images", name=f"{docker_repo}:{docker_tag}", quiet=True)
return len(image_list) > 0

async def _start_gateway(self):
try:
response = await docker_ipc(
"containers",
all=True,
filters={"name": get_gateway_container_name()}
)
if len(response) == 0:
raise ValueError(f"Gateway container {get_gateway_container_name()} not found. ")

container_info = response[0]
if container_info["State"] == "running":
self.notify(f"Gateway container {container_info['Id']} already running.")
return

await docker_ipc(
"start",
container=container_info["Id"]
)
self.notify(f"Gateway container {container_info['Id']} has started.")
except Exception as e:
self.notify(f"Error occurred starting Gateway container. {e}")

async def _stop_gateway(self):
try:
response = await docker_ipc(
"containers",
all=True,
filters={"name": get_gateway_container_name()}
)
if len(response) == 0:
raise ValueError(f"Gateway container {get_gateway_container_name()} not found.")

container_info = response[0]
if container_info["State"] != "running":
self.notify(f"Gateway container {container_info['Id']} not running.")
return

await docker_ipc(
"stop",
container=container_info["Id"],
)
self.notify(f"Gateway container {container_info['Id']} successfully stopped.")
except Exception as e:
self.notify(f"Error occurred stopping Gateway container. {e}")

async def _test_connection(self):
# test that the gateway is running
if await GatewayHttpClient.get_instance().ping_gateway():
Expand Down Expand Up @@ -300,7 +256,7 @@ async def _create_gateway(self):

GatewayHttpClient.get_instance().base_url = f"https://{global_config_map['gateway_api_host'].value}:" \
f"{global_config_map['gateway_api_port'].value}"
await self._start_gateway()
await start_gateway()

# create Gateway configs
await self._generate_gateway_confs(container_id=container_info["Id"])
Expand Down
57 changes: 57 additions & 0 deletions hummingbot/core/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,60 @@ def check_transaction_exceptions(
exception_list.append(f"Insufficient {asset_out} allowance {asset_out_allowance}. Amount to trade: {amount}")

return exception_list


async def start_gateway():
from hummingbot.client.hummingbot_application import HummingbotApplication
try:
response = await docker_ipc(
"containers",
all=True,
filters={"name": get_gateway_container_name()}
)
if len(response) == 0:
raise ValueError(f"Gateway container {get_gateway_container_name()} not found. ")

container_info = response[0]
if container_info["State"] == "running":
HummingbotApplication.main_application().notify(f"Gateway container {container_info['Id']} already running.")
return

await docker_ipc(
"start",
container=container_info["Id"]
)
HummingbotApplication.main_application().notify(f"Gateway container {container_info['Id']} has started.")
except Exception as e:
HummingbotApplication.main_application().notify(f"Error occurred starting Gateway container. {e}")


async def stop_gateway():
from hummingbot.client.hummingbot_application import HummingbotApplication
try:
response = await docker_ipc(
"containers",
all=True,
filters={"name": get_gateway_container_name()}
)
if len(response) == 0:
raise ValueError(f"Gateway container {get_gateway_container_name()} not found.")

container_info = response[0]
if container_info["State"] != "running":
HummingbotApplication.main_application().notify(f"Gateway container {container_info['Id']} not running.")
return

await docker_ipc(
"stop",
container=container_info["Id"],
)
HummingbotApplication.main_application().notify(f"Gateway container {container_info['Id']} successfully stopped.")
except Exception as e:
HummingbotApplication.main_application().notify(f"Error occurred stopping Gateway container. {e}")


async def restart_gateway():
from hummingbot.client.hummingbot_application import HummingbotApplication
await stop_gateway()
await start_gateway()
HummingbotApplication.main_application().notify("Gateway will be ready momentarily.")
17 changes: 15 additions & 2 deletions hummingbot/core/gateway/gateway_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from hummingbot.client.config.global_config_map import global_config_map
from hummingbot.client.config.security import Security
from hummingbot.core.event.events import TradeType
from hummingbot.core.gateway import get_gateway_paths
from hummingbot.core.gateway import (
detect_existing_gateway_container,
get_gateway_paths,
restart_gateway
)
from hummingbot.logger import HummingbotLogger


Expand Down Expand Up @@ -135,11 +139,20 @@ async def get_gateway_status(self, fail_silently: bool = False) -> List[Dict[str
)

async def update_config(self, config_path: str, config_value: Any) -> Dict[str, Any]:
return await self.api_request("post", "config/update", {
response = await self.api_request("post", "config/update", {
"configPath": config_path,
"configValue": config_value,
})

# temporary code until #25625 is implemented (delete afterwards)
# if the user had a gateway in a container, restart it
# otherwise if they manually run a gateway, they need to restart it themselves
container_info: Optional[Dict[str, Any]] = await detect_existing_gateway_container()
if container_info is not None:
await restart_gateway()

return response

async def get_connectors(self, fail_silently: bool = False) -> Dict[str, Any]:
return await self.api_request("get", "connectors", fail_silently=fail_silently)

Expand Down

0 comments on commit 18da4f2

Please sign in to comment.