mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
GraphQl 0.124 - Running server check
This commit is contained in:
@@ -66,6 +66,9 @@ def main ():
|
||||
# check file permissions and fix if required
|
||||
filePermissions()
|
||||
|
||||
# Header + init app state
|
||||
updateState("Initializing", None, None, None, 0)
|
||||
|
||||
# Open DB once and keep open
|
||||
# Opening / closing DB frequently actually casues more issues
|
||||
db = DB() # instance of class DB
|
||||
@@ -81,8 +84,7 @@ def main ():
|
||||
|
||||
mylog('debug', '[MAIN] Starting loop')
|
||||
|
||||
# Header + init app state
|
||||
updateState("Initializing")
|
||||
|
||||
|
||||
all_plugins = None
|
||||
|
||||
@@ -106,7 +108,7 @@ def main ():
|
||||
|
||||
# Update API endpoints
|
||||
update_api(db, all_plugins)
|
||||
|
||||
|
||||
# proceed if 1 minute passed
|
||||
if conf.last_scan_run + datetime.timedelta(minutes=1) < conf.loop_start_time :
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ INSTALL_PATH="/app"
|
||||
sys.path.extend([f"{INSTALL_PATH}/server"])
|
||||
|
||||
from logger import mylog
|
||||
from helper import get_setting_value, timeNowTZ
|
||||
from helper import get_setting_value, timeNowTZ, updateState
|
||||
from notification import write_notification
|
||||
|
||||
app = Flask(__name__)
|
||||
@@ -26,7 +26,7 @@ def graphql_endpoint():
|
||||
token = request.headers.get("Authorization")
|
||||
if token != f"Bearer {API_TOKEN}":
|
||||
mylog('verbose', [f'[graphql_server] Unauthorized access attempt'])
|
||||
|
||||
|
||||
return jsonify({"error": "Unauthorized"}), 401
|
||||
|
||||
data = request.get_json()
|
||||
@@ -42,8 +42,16 @@ def graphql_endpoint():
|
||||
def start_server():
|
||||
"""Function to start the GraphQL server in a background thread."""
|
||||
mylog('verbose', [f'[graphql_server] Starting on port: {GRAPHQL_PORT}'])
|
||||
|
||||
# Start the Flask app in a separate thread
|
||||
thread = threading.Thread(target=lambda: app.run(host="0.0.0.0", port=GRAPHQL_PORT, debug=True, use_reloader=False))
|
||||
thread.start()
|
||||
|
||||
state = updateState("GraphQL: Starting", None, None, None, None)
|
||||
|
||||
if state.graphQLServerStarted == 0:
|
||||
|
||||
# Start the Flask app in a separate thread
|
||||
thread = threading.Thread(target=lambda: app.run(host="0.0.0.0", port=GRAPHQL_PORT, debug=True, use_reloader=False))
|
||||
thread.start()
|
||||
|
||||
# updateState(newState, settingsSaved = None (timestamp), settingsImported = None (timestamp), showSpinner = False (1/0), graphQLServerStarted = False (1/0))
|
||||
# update GraphQL = started
|
||||
state = updateState("Process: Wait", None, None, None, 1)
|
||||
|
||||
|
||||
@@ -57,8 +57,9 @@ def get_timezone_offset():
|
||||
# App state
|
||||
#-------------------------------------------------------------------------------
|
||||
# A class to manage the application state and to provide a frontend accessible API point
|
||||
# To keep an existing value pass None
|
||||
class app_state_class:
|
||||
def __init__(self, currentState, settingsSaved=None, settingsImported=None, showSpinner=False):
|
||||
def __init__(self, currentState, settingsSaved=None, settingsImported=None, showSpinner=False, graphQLServerStarted=0):
|
||||
# json file containing the state to communicate with the frontend
|
||||
stateFile = apiPath + '/app_state.json'
|
||||
previousState = ""
|
||||
@@ -78,19 +79,21 @@ class app_state_class:
|
||||
mylog('none', [f'[app_state_class] Failed to handle app_state.json: {e}'])
|
||||
|
||||
|
||||
# Check if the file exists and init values
|
||||
# Check if the file exists and recover previous values
|
||||
if previousState != "":
|
||||
self.settingsSaved = previousState.get("settingsSaved", 0)
|
||||
self.settingsImported = previousState.get("settingsImported", 0)
|
||||
self.showSpinner = previousState.get("showSpinner", False)
|
||||
self.isNewVersion = previousState.get("isNewVersion", False)
|
||||
self.isNewVersionChecked = previousState.get("isNewVersionChecked", 0)
|
||||
else:
|
||||
self.graphQLServerStarted = previousState.get("graphQLServerStarted", 0)
|
||||
else: # init first time values
|
||||
self.settingsSaved = 0
|
||||
self.settingsImported = 0
|
||||
self.showSpinner = False
|
||||
self.isNewVersion = checkNewVersion()
|
||||
self.isNewVersionChecked = int(timeNow().timestamp())
|
||||
self.graphQLServerStarted = 0
|
||||
|
||||
# Overwrite with provided parameters if supplied
|
||||
if settingsSaved is not None:
|
||||
@@ -99,6 +102,8 @@ class app_state_class:
|
||||
self.settingsImported = settingsImported
|
||||
if showSpinner is not None:
|
||||
self.showSpinner = showSpinner
|
||||
if graphQLServerStarted is not None:
|
||||
self.graphQLServerStarted = graphQLServerStarted
|
||||
|
||||
# check for new version every hour and if currently not running new version
|
||||
if self.isNewVersion is False and self.isNewVersionChecked + 3600 < int(timeNow().timestamp()):
|
||||
@@ -115,7 +120,7 @@ class app_state_class:
|
||||
with open(stateFile, 'w') as json_file:
|
||||
json_file.write(json_data)
|
||||
except (TypeError, ValueError) as e:
|
||||
mylog('none', [f'[app_state_class] Failed to serialize object to JSON: {e}'])
|
||||
mylog('none', [f'[app_state_class] Failed to serialize object to JSON: {e}'])
|
||||
|
||||
|
||||
|
||||
@@ -131,9 +136,9 @@ class app_state_class:
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# method to update the state
|
||||
def updateState(newState, settingsSaved = None, settingsImported = None, showSpinner = False):
|
||||
def updateState(newState, settingsSaved = None, settingsImported = None, showSpinner = False, graphQLServerStarted = None):
|
||||
|
||||
state = app_state_class(newState, settingsSaved, settingsImported, showSpinner)
|
||||
return app_state_class(newState, settingsSaved, settingsImported, showSpinner, graphQLServerStarted)
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
@@ -374,7 +374,12 @@ def importConfigs (db, all_plugins):
|
||||
# Used to determine the next import
|
||||
conf.lastImportedConfFile = os.path.getmtime(config_file)
|
||||
|
||||
updateState("Config imported", conf.lastImportedConfFile, conf.lastImportedConfFile, False)
|
||||
# updateState(newState (text),
|
||||
# settingsSaved = None (timestamp),
|
||||
# settingsImported = None (timestamp),
|
||||
# showSpinner = False (1/0),
|
||||
# graphQLServerStarted = 1 (1/0))
|
||||
updateState("Config imported", conf.lastImportedConfFile, conf.lastImportedConfFile, False, 1)
|
||||
|
||||
msg = '[Config] Imported new settings config'
|
||||
mylog('minimal', msg)
|
||||
|
||||
Reference in New Issue
Block a user