-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of github.com:visualDust/neetbox into dev
- Loading branch information
Showing
8 changed files
with
185 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Author: GavinGong aka VisualDust | ||
# URL: https://gong.host | ||
# Date: 20230414 | ||
|
||
|
||
from neetbox.daemon._local_http_client import _local_http_client | ||
from neetbox.utils import pkg | ||
from neetbox.utils.framing import get_frame_module_traceback | ||
|
||
module_name = get_frame_module_traceback().__name__ | ||
assert pkg.is_installed( | ||
"httpx", try_install_if_not=True | ||
), f"{module_name} requires httpx which is not installed" | ||
import json | ||
import time | ||
|
||
import httpx | ||
|
||
from neetbox.config import get_module_level_config | ||
from neetbox.logging import logger | ||
|
||
logger = logger("NEETBOX DAEMON API") | ||
|
||
__cfg = get_module_level_config() | ||
daemon_address = f"{__cfg['server']}:{__cfg['port']}" | ||
base_addr = f"http://{daemon_address}" | ||
|
||
|
||
def get_status_of(name=None): | ||
name = name or "" | ||
api_addr = f"{base_addr}/status" | ||
logger.info(f"Fetching from {api_addr}") | ||
r = _local_http_client.get(api_addr) | ||
_data = r.json() | ||
return _data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Author: GavinGong aka VisualDust | ||
# URL: https://gong.host | ||
# Date: 20230414 | ||
|
||
from neetbox.utils import pkg | ||
from neetbox.utils.framing import get_frame_module_traceback | ||
|
||
module_name = get_frame_module_traceback().__name__ | ||
assert pkg.is_installed( | ||
"flask", try_install_if_not=True | ||
), f"{module_name} requires flask which is not installed" | ||
import sys | ||
import time | ||
from threading import Thread | ||
|
||
from flask import Flask, abort, json, request | ||
|
||
from neetbox.config import get_module_level_config | ||
|
||
_STAT_POOL = {} | ||
__DAEMON_SHUTDOWN_IF_NO_UPLOAD_TIMEOUT_SEC = 60 * 60 * 12 # 12 Hours | ||
__COUNT_DOWN = __DAEMON_SHUTDOWN_IF_NO_UPLOAD_TIMEOUT_SEC | ||
__DAEMON_NAME = "NEETBOX DAEMON" | ||
|
||
|
||
def daemon_process(daemon_config=None): | ||
import setproctitle | ||
|
||
setproctitle.setproctitle(__DAEMON_NAME) | ||
daemon_config = daemon_config or get_module_level_config() | ||
api = Flask(__DAEMON_NAME) | ||
|
||
@api.route("/hello", methods=["GET"]) | ||
def just_send_hello(): | ||
return json.dumps({"hello": "hello"}) | ||
|
||
@api.route("/status", methods=["GET"], defaults={"name": None}) | ||
@api.route("/status/<name>", methods=["GET"]) | ||
def return_status_of(name): | ||
global __COUNT_DOWN | ||
global _STAT_POOL | ||
__COUNT_DOWN = __DAEMON_SHUTDOWN_IF_NO_UPLOAD_TIMEOUT_SEC | ||
_returning_stat = dict(_STAT_POOL) | ||
if not name: | ||
pass # returning full dict | ||
elif name in _returning_stat: | ||
_returning_stat = _returning_stat[name] # returning specific status | ||
else: | ||
abort(404) | ||
return _returning_stat | ||
|
||
@api.route("/status/list", methods=["GET"]) | ||
def return_names_of_status(name): | ||
global __COUNT_DOWN | ||
global _STAT_POOL | ||
__COUNT_DOWN = __DAEMON_SHUTDOWN_IF_NO_UPLOAD_TIMEOUT_SEC | ||
_names = {_STAT_POOL.keys()} | ||
return _names | ||
|
||
@api.route("/sync/<name>", methods=["POST"]) | ||
def sync_status_of(name): | ||
global __COUNT_DOWN | ||
global _STAT_POOL | ||
__COUNT_DOWN = __DAEMON_SHUTDOWN_IF_NO_UPLOAD_TIMEOUT_SEC | ||
_json_data = request.get_json() | ||
_STAT_POOL[name] = _json_data | ||
return "ok" | ||
|
||
@api.route("/shutdown", methods=["POST"]) | ||
def shutdown(): | ||
global __COUNT_DOWN | ||
__COUNT_DOWN = -1 | ||
|
||
def __sleep_and_shutdown(secs=3): | ||
time.sleep(secs=secs) | ||
sys.exit(0) | ||
|
||
Thread(target=__sleep_and_shutdown, args=(3)).start() # shutdown after 3 seconds | ||
return "ok" | ||
|
||
def _count_down_thread(): | ||
global __COUNT_DOWN | ||
while True: | ||
__COUNT_DOWN -= 1 | ||
if not __COUNT_DOWN: | ||
sys.exit(0) | ||
time.sleep(1) | ||
|
||
count_down_thread = Thread(target=_count_down_thread, daemon=True) | ||
count_down_thread.start() | ||
|
||
api.run(host="0.0.0.0", port=daemon_config["port"]) |