Skip to content

Commit

Permalink
Opt: Add an option to disable reload
Browse files Browse the repository at this point in the history
  • Loading branch information
18870 committed Jan 14, 2022
1 parent 52dca3d commit c808a7b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
3 changes: 3 additions & 0 deletions deploy/template/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Deploy:
InstallUiautomator2: true

Update:
# Use auto update and builtin updater feature
# This may cause problem https://github.com/LmeSzinc/AzurLaneAutoScript/issues/876
EnableReload: true
# Check update every X minute
# [Disable] -1
# [Default] 5
Expand Down
28 changes: 25 additions & 3 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ def bind(self) -> socket.socket:
# monkey patch Config.bing_socket
uvicorn.Config.bind_socket = bind


def run_with_reload(host, port):
"""
Same as command below, but without reuse address patch.
uvicorn module.webui.app:app --factory --host 127.0.0.1 --port 22267 --reload --reload-include="reloadflag" --reload-exclude="*.py" --reload-dir="./config/"
"""
uvicorn.run('module.webui.app:app', host=host, port=port, factory=True, reload_dirs=[os.path.join(os.getcwd(), './config/')],
reload=True, reload_includes=['reloadflag'], reload_excludes=['*.py'])


def run_without_reload(host, port):
from module.webui.app import app
uvicorn.run(app, host=host, port=port, factory=True)


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Alas web service')
parser.add_argument('--host', type=str,
Expand All @@ -88,13 +103,18 @@ def bind(self) -> socket.socket:
help='Port to listen. Default to WebuiPort in deploy setting')
parser.add_argument('-d', '--disable-address-reuse', action="store_true",
help='Disable SO_REUSEADDR.')
parser.add_argument('--electron', action="store_true",
help='Runs by electron client.')
parser.add_argument('--reload', action="store_true",
help='Able to use auto update and builtin updater')
args, _ = parser.parse_known_args()

webui_config = WebuiConfig()
host = args.host or webui_config.WebuiHost or '0.0.0.0'
port = args.port or int(webui_config.WebuiPort) or 22267
reload = args.reload or webui_config.bool('EnableReload')

if args.disable_address_reuse:
if args.disable_address_reuse or args.electron:
reuseaddr = False
logger.hr('Server config')
logger.attr('Host', host)
Expand All @@ -105,5 +125,7 @@ def bind(self) -> socket.socket:
except:
pass

uvicorn.run('module.webui.app:app', host=host, port=port, factory=True, reload_dirs=[os.path.join(os.getcwd(), './config/')],
reload=True, reload_includes=['reloadflag'], reload_excludes=['*.py'])
if reload:
run_with_reload(host=host, port=port)
else:
run_without_reload(host=host, port=port)
17 changes: 17 additions & 0 deletions module/webui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,20 @@ def __setattr__(self, key, value):
if key[0].isupper() and key in self.config:
self.config[key] = value
self.write()

@staticmethod
def to_bool(value):
value = value.lower()
if value == 'null' or value == 'false' or value == '':
return False
return True

def bool(self, key):
"""
Args:
key (str):
Returns:
bool: Option is ON or OFF.
"""
return self.to_bool(self.config[key])
4 changes: 4 additions & 0 deletions module/webui/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def schedule_time(self):
self.read()
return datetime.time.fromisoformat(self.config['AutoRestartTime'])

@cached_property
def enabled(self):
return self.bool('EnableReload')

@cached_property
def repo(self):
return self.config['Repository']
Expand Down
1 change: 0 additions & 1 deletion module/webui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ def loop(self) -> None:

def _get_thread(self) -> threading.Thread:
thread = Thread(target=self.loop)
thread.daemon = True
return thread

def start(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions webapp/packages/main/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ const PythonExecutable = config.Deploy.Python.PythonExecutable;
const WebuiPort = config.Deploy.Webui.WebuiPort.toString();

export const pythonPath = (path.isAbsolute(PythonExecutable) ? PythonExecutable : path.join(alasPath, PythonExecutable));
export const webuiUrl = `http://localhost:${WebuiPort}`;
export const webuiUrl = `http://127.0.0.1:${WebuiPort}`;
export const webuiPath = 'gui.py';
export const webuiArgs = ['--port', WebuiPort];
export const webuiArgs = ['--port', WebuiPort, '--electron'];

0 comments on commit c808a7b

Please sign in to comment.