Skip to content

Commit

Permalink
[JSORC]: Sync configuratin via rollout restart
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Sep 15, 2023
1 parent 8e00e3a commit 5c86f4d
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 19 deletions.
5 changes: 5 additions & 0 deletions jaseci_core/jaseci/extens/act_lib/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,8 @@ def log_activity(
activity = elastic.generate_from_meta(meta, log, action)

return elastic.doc_activity(activity, query, suffix)


@jaseci_action()
def clear_cache(all: bool = True, meta: dict = {}):
meta["h"].clear_cache(all)
16 changes: 7 additions & 9 deletions jaseci_core/jaseci/extens/api/jsorc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,14 @@ def service_info(self, name: str):
"""

# will throw exception if not existing
svc = JsOrc.svc(name)
return JsOrc.svc(name).info()

return {
"enabled": svc.enabled,
"automated": svc.automated,
"quiet": svc.quiet,
"state": svc.state.name,
"config": svc.config,
"error": str(svc.error) if svc.error else None,
}
@Interface.admin_api(cli_args=["name"])
def service_config_set(self, name: str, config: dict):
"""
Set a service config
"""
return JsOrc.svc_conf_set(name, config, self._h)

@Interface.admin_api(cli_args=["name"])
def service_refresh(self, name: str):
Expand Down
38 changes: 37 additions & 1 deletion jaseci_core/jaseci/extens/svc/kube_svc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from base64 import b64decode

from datetime import datetime
from kubernetes import config as kubernetes_config
from kubernetes.client import (
ApiClient,
Expand Down Expand Up @@ -394,3 +394,39 @@ def resolve_manifest(
placeholder_resolver(manifest, manifest)

return manifest

def has_replicas(self):
try:
return (
self.read(
"Deployment", "jaseci", self.namespace, quiet=self.quiet
).spec.replicas
> 1
)
except Exception as e:
self.quiet or logger.error(f"Error checking jaseci replica -- {e}")

def restart(self):
try:
return self.api.patch_namespaced_deployment(
name="jaseci",
namespace=self.namespace,
body={
"spec": {
"template": {
"metadata": {
"annotations": {
"kubectl.kubernetes.io/restartedAt": datetime.utcnow().isoformat(
"T"
)
+ "Z"
}
}
}
}
},
)
except ApiException as e:
self.quiet or logger.error(
f"Error triggering jaseci rollout restart -- {e}"
)
7 changes: 5 additions & 2 deletions jaseci_core/jaseci/extens/svc/redis_svc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ def hkeys(self, name):
# CLEANER #
###################################################

def clear(self):
def clear(self, all=True):
if self.is_running():
self.app.flushdb()
if all:
self.app.flushall()
else:
self.app.flushdb()

# ---------------- PROXY EVENTS ----------------- #

Expand Down
28 changes: 27 additions & 1 deletion jaseci_core/jaseci/jsorc/jsorc.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,32 @@ def svc(cls, service: str, cast: T = None) -> Union[T, cs]:

return cls._service_instances[service]

@classmethod
def svc_conf_set(
cls, service: str, config: dict, hook: None, cast: T = None
) -> Union[T, cs]:
from jaseci.extens.svc.kube_svc import KubeService

if service not in cls._services:
raise Exception(f"Service {service} is not existing!")

if cls.db_check():
instance = cls._services[service][0]
hook = hook or cls.hook(use_proxy=instance["proxy"])
hook.save_glob(instance["config"], dumps(config))
hook.commit()

kube = cls.svc("kube", KubeService)
if kube.is_running() and kube.has_replicas():
hook.clear_cache(False)
return (
"Rollout restart commencing..."
if not isinstance(kube.restart(), ApiException)
else "Attempt to rollout restart failed! Please check the logs for further information!"
)

return cls.svc_reset(service).info()

@classmethod
def svc_reset(cls, service, cast: T = None) -> Union[T, cs]:
"""
Expand Down Expand Up @@ -333,7 +359,7 @@ def decorator(service: T) -> T:
target=cls._services,
entry={
"type": service,
"config": config or f"{name.upper()}_CONFIG",
"config": (config or f"{name}_CONFIG").upper(),
"manifest": manifest,
"manifest_type": manifest_type,
"priority": priority,
Expand Down
12 changes: 12 additions & 0 deletions jaseci_core/jaseci/jsorc/jsorc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,15 @@ def __setstate__(self, ignored):
# for build on pickle load
self.state = State.FAILED
del self

# ------------------- COMMON -------------------- #

def info(self):
return {
"enabled": self.enabled,
"automated": self.automated,
"quiet": self.quiet,
"state": self.state.name,
"config": self.config,
"error": str(self.error) if self.error else None,
}
2 changes: 1 addition & 1 deletion jaseci_core/jaseci/jsorc/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def find_class_and_import(self, j_type, mod):

return cls

def clear_cache(self):
def clear_cache(self, all=True):
MemoryHook.__init__(self)

####################################################
Expand Down
8 changes: 3 additions & 5 deletions jaseci_core/jaseci/jsorc/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,9 @@ def decommit_obj_from_cache(self, item):
# CLEANER #
###################################################

def clear_cache(self):
if self.redis.is_running():
self.redis.app.flushdb()

MemoryHook.__init__(self)
def clear_cache(self, all=True):
super().clear_cache(all)
self.redis.clear(all)


# ----------------------------------------------- #

0 comments on commit 5c86f4d

Please sign in to comment.