Upgraded lifecycles by putting session and thread registration inside the registry and handling initialization at the manageLifecycle level
This commit is contained in:
@@ -1,30 +1,15 @@
|
|||||||
import threading
|
import threading
|
||||||
from .lifecyclemanager import registerLifecycle
|
|
||||||
from .lifecyclemanager import manageLifecycle
|
from .lifecyclemanager import manageLifecycle
|
||||||
|
|
||||||
#threadlist = []
|
def buildThread(module):
|
||||||
#threadnum = 0
|
|
||||||
|
|
||||||
def buildThread(module, lifecycles):
|
|
||||||
#global threadnum
|
|
||||||
#threadnum += 1
|
|
||||||
|
|
||||||
#threadname = f"thread.{threadnum}"
|
|
||||||
|
|
||||||
thread = threading.Thread(
|
thread = threading.Thread(
|
||||||
target=manageLifecycle,
|
target=manageLifecycle,
|
||||||
args=(module, lifecycles),
|
args=(module,),
|
||||||
name=module.name
|
name=module.name
|
||||||
)
|
)
|
||||||
|
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
#print(lifecycles)
|
return thread
|
||||||
#registryname = thread.name
|
|
||||||
#registryvalue = thread
|
|
||||||
|
|
||||||
#lifecycles = registerLifecycle (module, lifecycles, thread.name, thread)
|
|
||||||
|
|
||||||
#threadlist.append(thread)
|
|
||||||
|
|
||||||
return thread, lifecycles
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from core.execution.action import buildAction
|
from core.execution.action import buildAction
|
||||||
from core.execution.operation import buildOperation
|
from core.execution.operation import buildOperation
|
||||||
from core.execution.monitor import buildMonitor
|
from core.execution.monitor import buildMonitor
|
||||||
from core.runtime import manageFailure
|
#from core.runtime import manageFailure
|
||||||
from core.execution.recursive import buildRecursive
|
from core.execution.recursive import buildRecursive
|
||||||
|
|
||||||
def executeContract(module, outputs, process):
|
def executeContract(module, outputs, process):
|
||||||
|
|||||||
@@ -1,30 +1,42 @@
|
|||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from core.execution.operation import buildOperation
|
|
||||||
from core.execution.action import buildAction
|
|
||||||
from core.execution.executioncontract import executeContract
|
from core.execution.executioncontract import executeContract
|
||||||
|
from core.apirequest.execute import getSession
|
||||||
|
from core.execution.lifecyclemanager.lifecycleregistry import registerLifecycle
|
||||||
|
|
||||||
#sleeptime = int(os.environ.get("REFRESH_INTERVAL", 180))
|
lifecycles = {}
|
||||||
|
|
||||||
def manageLifecycle(module, lifecycles): #lifecycles, state):
|
def manageLifecycle(module):
|
||||||
|
lifecycle_log = os.environ.get("LOGS_LIFECYCLE", "false").lower() == "true"
|
||||||
|
|
||||||
outputs = {}
|
outputs = {}
|
||||||
|
|
||||||
|
registerLifecycle (module, lifecycles)
|
||||||
|
|
||||||
if lifecycles[module.name]["mode"] == "continuous":
|
if lifecycles[module.name]["mode"] == "continuous":
|
||||||
while lifecycles[module.name]["mode"] == "continuous":
|
while lifecycles[module.name]["mode"] == "continuous":
|
||||||
|
lifecycles[module.name]["state"] = "active"
|
||||||
|
|
||||||
outputs, response = executeContract(module, outputs)
|
outputs, response = executeContract(module, outputs, None)
|
||||||
|
|
||||||
lifecycle_log = bool(os.environ.get("LOGS_LIFECYCLE", False))
|
|
||||||
if lifecycle_log:
|
if lifecycle_log:
|
||||||
print(f"{module.name} thread {lifecycles[module.name][module.name]} running in {lifecycles[module.name]['mode']} mode")
|
print(f"{module.name} thread {lifecycles[module.name][module.name]} running in {lifecycles[module.name]['mode']} mode")
|
||||||
|
print(lifecycles)
|
||||||
|
|
||||||
time.sleep(module.sleeptime)
|
lifecycles[module.name]["state"] = "sleep"
|
||||||
|
time.sleep(lifecycles[module.name]["refresh_rate"])
|
||||||
|
lifecycles[module.name]["state"] = "stopped"
|
||||||
|
|
||||||
elif lifecycles[module.name]["mode"] == "oneshot":
|
elif lifecycles[module.name]["mode"] == "oneshot":
|
||||||
|
lifecycles[module.name]["state"] = "active"
|
||||||
|
|
||||||
|
if lifecycle_log:
|
||||||
|
print(f"{module.name} thread {lifecycles[module.name][module.name]} running in {lifecycles[module.name]['mode']} mode")
|
||||||
print(lifecycles)
|
print(lifecycles)
|
||||||
outputs, response = executeContract(module)
|
|
||||||
|
outputs, response = executeContract(module, outputs, None)
|
||||||
|
|
||||||
|
lifecycles[module.name]["state"] = "stopped"
|
||||||
|
|
||||||
#print(lifecycles)
|
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,26 +1,38 @@
|
|||||||
import threading
|
import threading
|
||||||
import time
|
|
||||||
|
|
||||||
def registerLifecycle(module, lifecycles, registryname, registryvalue):
|
from core.apirequest.execute import getSession
|
||||||
|
|
||||||
|
def registerLifecycle(module, lifecycles):
|
||||||
if module.name not in lifecycles:
|
if module.name not in lifecycles:
|
||||||
lifecycles[module.name] = {}
|
lifecycles[module.name] = {}
|
||||||
|
|
||||||
thread = threading.current_thread()
|
thread = threading.current_thread()
|
||||||
|
|
||||||
lifecycles[module.name][thread.name] = thread
|
lifecycles[module.name][thread.name] = thread
|
||||||
|
|
||||||
lifecycles[module.name][registryname] = registryvalue
|
session = getSession()
|
||||||
print(lifecycles)
|
|
||||||
|
session_data = {
|
||||||
|
"created_at": session.created_at,
|
||||||
|
"lifetime": 1800,
|
||||||
|
"valid": True
|
||||||
|
}
|
||||||
|
|
||||||
|
session.name = f"{module.name} Session"
|
||||||
|
|
||||||
|
lifecycles[module.name][session.name] = session_data
|
||||||
|
#print(lifecycles)
|
||||||
|
|
||||||
if "mode" not in lifecycles[module.name]:
|
if "mode" not in lifecycles[module.name]:
|
||||||
lifecycles[module.name]["mode"] = {}
|
mode = getattr(module, "mode")
|
||||||
elif lifecycles[module.name]["mode"] == "continuous":
|
lifecycles[module.name]["mode"] = mode
|
||||||
|
|
||||||
|
if lifecycles[module.name]["mode"] == "continuous":
|
||||||
lifecycles[module.name]["refresh_rate"] = module.sleeptime
|
lifecycles[module.name]["refresh_rate"] = module.sleeptime
|
||||||
|
|
||||||
elif not lifecycles[module.name]["mode"]:
|
if not lifecycles[module.name]["mode"]:
|
||||||
lifecycles[module.name]["mode"] = "oneshot"
|
lifecycles[module.name]["mode"] = "oneshot"
|
||||||
|
|
||||||
lifecycles[module.name][registryname] = registryvalue
|
#lifecycles[module.name][registryname] = registryvalue
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user