disable shutdown command for demo

This commit is contained in:
Vassili Minaev 2025-03-21 08:37:33 -06:00
parent 108193c38c
commit 01ea656fce
2 changed files with 28 additions and 18 deletions

View file

@ -28,10 +28,9 @@ async def api(request):
command = postdata.get('command').split(' ') command = postdata.get('command').split(' ')
if command[0] == "status": if command[0] == "status":
try: try:
count_tables = "SELECT COUNT(*) FROM CompanyC.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'" count_tables = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'"
await request.config_dict['cur'].execute(count_tables) results = await request.config_dict['query'](count_tables)
result = await request.config_dict['cur'].fetchone() tables = results[0][0]
tables = result[0]
db_status = 'OK' db_status = 'OK'
except Exception as e: except Exception as e:
tables = 0 tables = 0
@ -43,28 +42,38 @@ async def api(request):
ram_stats = psutil.virtual_memory() ram_stats = psutil.virtual_memory()
ram_total = ram_stats[0] ram_total = ram_stats[0]
ram_used = ram_stats[3] ram_used = ram_stats[3]
data = f"""\
Web server status: {request.config_dict['stats']['status']}, started {request.config_dict['start']:%Y-%m-%d %H:%M:%S}.
Database at {request.config_dict['config']['database']['host']}: {db_status}, access to {tables:,} tables.
Load on {core_count} cores, {thread_count} threads: {cpu_load}%. Using {ram_used/1024**3:.1f} GiB of {ram_total/1024**3:.1f} GiB memory."""
return web.json_response({'status': 'ok', 'data': data, 'timestamp': timestamp})
elif command[0] == "shutdown":
#request.config_dict['stats']['status'] = 'Shutting down'
delay = 5 if len(command) == 1 else int(command[1])
delay = max(delay, 0)
#asyncio.create_task(shutdown(request.config_dict['runner'], request.config_dict['waiter'], delay))
return web.json_response({'status': 'ok', 'data': f'This would shut down the app in {delay} seconds, but the feature is disabled for this demo.', 'timestamp': timestamp})
elif command[0] == "fortune":
magic_8_ball = [ magic_8_ball = [
"It is certain", "It is decidedly so", "Without a doubt", "Yes definitely", "You may rely on it", "It is certain", "It is decidedly so", "Without a doubt", "Yes definitely", "You may rely on it",
"As I see it, yes", "Most likely", "Outlook good", "Yes", "Signs point to yes", "As I see it, yes", "Most likely", "Outlook good", "Yes", "Signs point to yes",
"Reply hazy, try again", "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again", "Reply hazy, try again", "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again",
"Don't count on it", "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful" "Don't count on it", "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful"
] ]
return web.json_response({'status': 'ok', 'data': f'Your magic 8-ball prediction: {random.choice(magic_8_ball)}', 'timestamp': timestamp})
data = f"""\ elif command[0] == "help":
Web server status: {request.config_dict['stats']['status']}, started {request.config_dict['start']:%Y-%m-%d %H:%M:%S}. message = """\
Database at {request.config_dict['config']['database']['host']}: {db_status}, access to {tables:,} tables. status - Prints an overview of system operation and resource usage
Load on {core_count} cores, {thread_count} threads: {cpu_load}%. Using {ram_used/1024**3:.1f} GiB of {ram_total/1024**3:.1f} GiB memory. shutdown [delay] - Shuts down the application after a timeout in seconds (default 5, disabled for this demo)
Your magic 8-ball prediction: {random.choice(magic_8_ball)}.""" help - Prints this message"""
return web.json_response({'status': 'ok', 'data': data, 'timestamp': timestamp}) return web.json_response({'status': 'ok', 'data': message, 'timestamp': timestamp})
elif command[0] == "shutdown":
request.config_dict['stats']['status'] = 'Shutting down'
delay = 5 if len(command) == 1 else int(command[1])
asyncio.create_task(shutdown(request.config_dict['runner'], request.config_dict['waiter'], delay))
return web.json_response({'status': 'ok', 'data': f'Shutting down in {delay} seconds...', 'timestamp': timestamp})
else: else:
return web.json_response({'status': 'error', 'data': f'Unrecognized command "{command[0]}"', 'timestamp': timestamp}) return web.json_response({'status': 'error', 'data': f'Unrecognized command "{command[0]}", try "help".', 'timestamp': timestamp})
async def shutdown(runner, waiter, delay=0): async def shutdown(runner, waiter, delay=0):
await asyncio.sleep(delay) await asyncio.sleep(delay)

View file

@ -39,7 +39,8 @@
<select class="form-select mb-3" id="commandlist"> <select class="form-select mb-3" id="commandlist">
<option value="" disabled selected>Commands</option> <option value="" disabled selected>Commands</option>
<option value="status">Status</option> <option value="status">Status</option>
<option value="shutdown">Shut down</option> <option value="shutdown">Shut down (disabled for this demo)</option>
<option value="fortune">Fortune</option>
</select> </select>
<div id="output" class="w-100 border overflow-y-auto p-2 mb-3"></div> <div id="output" class="w-100 border overflow-y-auto p-2 mb-3"></div>
<form method="post" action="/reporter/admin/api" id="commandform" class="mb-3" autocomplete="off"> <form method="post" action="/reporter/admin/api" id="commandform" class="mb-3" autocomplete="off">