Skip to content

Commit

Permalink
fix: handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiionutdamian committed Feb 7, 2024
1 parent e7b9d01 commit 0d85a44
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 36 deletions.
6 changes: 5 additions & 1 deletion demo-basic-fastapi/nodeport_example/delete.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/bin/bash

source ../utils.sh

APP_NAME="basic-test-py"
NAMESPACE="basic-ns11"
kubectl delete -f deploy_nodeport.yaml
Expand All @@ -7,7 +11,7 @@ while true; do
OUTPUT=$(kubectl get pods -l run=$APP_NAME -n $NAMESPACE 2>&1)
echo "$OUTPUT"
if [[ "$OUTPUT" == *"No resources found"* ]]; then
echo "All pods terminated."
log_with_color "All pods terminated." green
break
fi
sleep 5
Expand Down
32 changes: 0 additions & 32 deletions demo-basic-fastapi/nodeport_example/full_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,6 @@ CHECK_INTERVAL=5
COUNT=5


log_with_color() {
local text="$1"
local color="$2"
local color_code=""

case $color in
red)
color_code="0;31" # Red
;;
green)
color_code="0;32" # Green
;;
blue)
color_code="0;34" # Blue
;;
yellow)
color_code="0;33" # Yellow
;;
light)
color_code="1;37" # Light (White)
;;
gray)
color_code="2;37" # Gray (White)
;;
*)
color_code="0" # Default color
;;
esac

echo -e "\e[${color_code}m${text}\e[0m"
}

kubectl apply -f $MANIFEST_FILENAME

# Function to get the deployment's ready replicas
Expand Down
26 changes: 26 additions & 0 deletions demo-basic-fastapi/nodeport_example/start.sh
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
kubectl apply -f deploy_nodeport.yaml

get_ready_replicas() {
kubectl get deployment "$DEPLOYMENT_NAME" -n "$NAMESPACE" -o jsonpath='{.status.readyReplicas}'
}

# Wait until all pods are ready
log_with_color "Waiting for pods of deployment '$DEPLOYMENT_NAME' in namespace '$NAMESPACE' to become ready..." yellow
while true; do
READY_REPLICAS=$(get_ready_replicas)

# Check if the ready replicas count is non-empty and greater than 0
if [[ "$READY_REPLICAS" != "" && "$READY_REPLICAS" -gt 0 ]]; then
TOTAL_REPLICAS=$(kubectl get deployment "$DEPLOYMENT_NAME" -n "$NAMESPACE" -o jsonpath='{.spec.replicas}')

if [[ "$READY_REPLICAS" == "$TOTAL_REPLICAS" ]]; then
log_with_color "All $READY_REPLICAS pods are ready." green
break
else
log_with_color "$READY_REPLICAS out of $TOTAL_REPLICAS pods are ready. Waiting..." yellow
fi
else
log_with_color "No pods are ready yet. Waiting..." red
fi

sleep "$CHECK_INTERVAL"
done
4 changes: 3 additions & 1 deletion demo-basic-fastapi/nodeport_example/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ ADDR_PORT="192.168.1.55:30050"
for i in $(seq 1 10); do
curl -L $ADDR_PORT
echo " "
done
done

curl -s $ADDR_PORT/stats | jq .
26 changes: 24 additions & 2 deletions demo-basic-fastapi/src/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from app_utils import safe_jsonify

__VER__ = '0.3.0'
__VER__ = '0.3.2'


class AppPaths:
Expand All @@ -19,6 +19,7 @@ def __init__(self):
self.__setup()
return


def P(self, s, **kwargs):
if vars(self).get('log') is not None :
self.log.P(s, **kwargs)
Expand All @@ -27,9 +28,26 @@ def P(self, s, **kwargs):
print('[APH][{}]'.format(str_date) + s, flush=True, **kwargs)
return


def __check_handlers(self):
requested_funcs = ["_handle_" + x for x in self.__path_to_func.values()]
for func in requested_funcs:
if not hasattr(self, func):
msg = "Handler function '{}' not found in class '{}'. Available handlers: {}, Paths: {}".format(
func, self.__class__.__name__,
[x for x in dir(self) if x.startswith("_handle_")],
{k:v for k, v in vars(AppPaths).items() if k.startswith("PATH_")},
)
raise Exception(msg)
#end if not hasattr
#end check for handlers
return


def __setup(self):
self.__avail_paths = [v['PATH'] for k, v in vars(AppPaths).items() if k.startswith("PATH_")]
self.__path_to_func = {v['PATH']: v['FUNC'] for k, v in vars(AppPaths).items() if k.startswith("PATH_")}
self.__check_handlers()
self.str_local_id = "test_" + str(uuid4())[:5]
self.__local_count = 0
self.__has_redis = False
Expand All @@ -42,6 +60,7 @@ def __setup(self):
self.P("Environement:\n{}".format(safe_jsonify(dct_env, indent=2)))
self.__maybe_setup_redis()
return


def __maybe_setup_redis(self):
dct_redis = {k : v for k, v in os.environ.items() if k.startswith("REDIS_")}
Expand Down Expand Up @@ -74,14 +93,17 @@ def __maybe_setup_redis(self):
self.__has_redis = False
return


def _pack_result(self, message):
return {"result": message}


def _inc_cluster_count(self):
if self.__has_redis:
self.__redis.incr("cluster_count")
return


def get_cluster_count(self):
result = self.__local_count
if self.__has_redis:
Expand Down Expand Up @@ -122,7 +144,7 @@ def _handle_root(self, **kwargs):
)
return msg

def _handle_stat(self, **kwargs):
def _handle_stats(self, **kwargs):
dct_result = {
'info' : "Handler '{}', Worker HOSTNAME: '{}', ID: '{}'".format(
kwargs['path'], self.hostname, self.str_local_id
Expand Down
32 changes: 32 additions & 0 deletions demo-basic-fastapi/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

log_with_color() {
local text="$1"
local color="$2"
local color_code=""

case $color in
red)
color_code="0;31" # Red
;;
green)
color_code="0;32" # Green
;;
blue)
color_code="0;34" # Blue
;;
yellow)
color_code="0;33" # Yellow
;;
light)
color_code="1;37" # Light (White)
;;
gray)
color_code="2;37" # Gray (White)
;;
*)
color_code="0" # Default color
;;
esac

echo -e "\e[${color_code}m${text}\e[0m"
}

0 comments on commit 0d85a44

Please sign in to comment.