Skip to content

Commit

Permalink
Merge pull request #3 from WasinUddy/Implement-K8S-health-probe
Browse files Browse the repository at this point in the history
Implement Kubernetes health probe
  • Loading branch information
WasinUddy authored Apr 5, 2024
2 parents 697387e + e4b353e commit c211fdd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
17 changes: 17 additions & 0 deletions web/backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ def get_log():
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))


# Kubernetes health checks
@app.get("/health/readiness")
def readiness():
# Ready when FastAPI server is running (ready to accept requests)
return {"message": "ready"}

@app.get("/health/liveness")
def liveness():
# Server is not in Illegal State
if minecraft_server.live_probe():
return {"message": "live"}
else:
raise HTTPException(status_code=500, detail="Server is in Illegal State")



if __name__ == "__main__":
# edit index.html for correct subdirectory
subpath = os.environ.get("SUBPATH", "")
Expand Down
28 changes: 26 additions & 2 deletions web/backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def __init__(self, cwd: str):
self.cwd = cwd
self.server = None
self.config_files = ("server.properties", "allowlist.json", "permissions.json")
self.running = False

def start(self, x86: bool = True):
"""
Expand All @@ -34,6 +35,7 @@ def start(self, x86: bool = True):
# Open the log file in read and write mode.
self.server_log = open("server.log", "r+")


# Start the server process, redirecting stdout and stderr to the log file.
if x86:
self.server = subprocess.Popen(
Expand All @@ -46,6 +48,8 @@ def start(self, x86: bool = True):
bufsize=1,
universal_newlines=True
)

# Work in progress
else:
self.server = subprocess.Popen(
["qemu-x86_64", "./bedrock_server"],
Expand All @@ -57,8 +61,10 @@ def start(self, x86: bool = True):
bufsize=1,
universal_newlines=True
)


# Set the running flag to True.
self.running = True

def stop(self, force: bool = False):
"""
Stops the server process.
Expand All @@ -76,6 +82,10 @@ def stop(self, force: bool = False):
self.server.kill()
self.server = None

# Set the running flag to False.
self.running = False


def command(self, cmd: str):
"""
Sends a command to the server process.
Expand All @@ -88,4 +98,18 @@ def command(self, cmd: str):
self.server.stdin.write(f"{cmd}\n")
self.server.stdin.flush()
else:
raise Exception("Server is not running")
raise Exception("Server is not running")

def live_probe(self):
"""
Check if the server is not in illegal state. to be used with Kubernetes liveness probe
if the process died while flag is still True then the Server is in illegal state
"""

# self.server.poll() is None if the process is still running
if self.running!=(self.server.poll() is None):
return False # Illegal state
else:
return True


0 comments on commit c211fdd

Please sign in to comment.