Skip to content

Commit

Permalink
feat: + serve force + rerender with fresh session
Browse files Browse the repository at this point in the history
  • Loading branch information
manatlan committed Oct 3, 2023
1 parent bb3f1c5 commit 440a634
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions htagweb/appserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ async def handleHome(request):
return await self.serve(request,obj)
self.add_route( '/', handleHome )

async def serve(self, request, obj ) -> HTMLResponse:
async def serve(self, request, obj, force:bool=False ) -> HTMLResponse:
uid = request.scope["uid"]
fqn=normalize(findfqn(obj))

Expand Down Expand Up @@ -312,7 +312,7 @@ async def serve(self, request, obj ) -> HTMLResponse:
""" % locals()

p = HrClient(uid,fqn,js,self.sesprovider.__name__)
p = HrClient(uid,fqn,js,self.sesprovider.__name__,force=force)

args,kargs = commons.url2ak(str(request.url))
html=await p.start(*args,**kargs)
Expand Down
19 changes: 11 additions & 8 deletions htagweb/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def importClassFromFqn(fqn_norm:str) -> type:



def process(uid,hid,event_response,event_interact,fqn,js,init,sesprovidername):
def process(uid,hid,event_response,event_interact,fqn,js,init,sesprovidername,force):
#''''''''''''''''''''''''''''''''''''''''''''''''''''
if sesprovidername is None:
sesprovidername="MemDict"
Expand Down Expand Up @@ -107,6 +107,7 @@ async def update(actions):
if params.get("cmd") == CMD_RENDER:
# just a false start, just need the current render
print(f">Process {pid} render {hid}")
hr.session = FactorySession(uid) # reload session
assert await bus.publish(event_response,str(hr))
else:
print(f">Process {pid} interact {hid}:")
Expand Down Expand Up @@ -169,18 +170,20 @@ def killall(ps:dict):
if hid in ps and ps[hid]["process"].is_alive():
# process is already running

if key_init == ps[hid]["key"]:
if params["force"] or key_init != ps[hid]["key"]:
# kill itself because it's not the same init params, or force recreate
if params["force"]:
print("Recreate a new process (forced)",hid)
else:
print("Recreate a new process (qp changed)",hid)
ps[hid]["process"].kill()
# and recreate another one later
else:
# it's the same initialization process

# so ask process to send back its render
assert await bus.publish(params["event_interact"],dict(cmd=CMD_RENDER))
continue
else:
# kill itself because it's not the same init params
print("Reload a new process",hid)
ps[hid]["process"].kill()

# and recreate another one later

# create the process
p=multiprocessing.Process(target=process, args=[],kwargs=params)
Expand Down
4 changes: 3 additions & 1 deletion htagweb/server/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
TIMEOUT=20 # sec to wait answer from redys server #TODO: set better

class HrClient:
def __init__(self,uid:str,fqn:str,js:str=None,sesprovidername=None):
def __init__(self,uid:str,fqn:str,js:str=None,sesprovidername=None,force=False):
""" !!!!!!!!!!!!!!!!!!!! if js|sesprovidername is None : can't do a start() !!!!!!!!!!!!!!!!!!!!!!"""
self.uid=uid
self.fqn=fqn
self.js=js
self.bus = redys.v2.AClient()
self.sesprovidername=sesprovidername
self.force=force

self.hid=f"{uid}_{fqn}"
self.event_response = f"response_{self.hid}"
Expand Down Expand Up @@ -56,6 +57,7 @@ async def start(self,*a,**k) -> str:
js=self.js,
init= (a,k),
sesprovidername=self.sesprovidername,
force=self.force,
))

# wait 1st rendering
Expand Down
4 changes: 4 additions & 0 deletions test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ async def test_base( server ):
html=await p.start()
assert html.startswith("<!DOCTYPE html><html>")

p=HrClient(uid,"test_hr:App","//",force=True)
html=await p.start()
assert html.startswith("<!DOCTYPE html><html>")

actions=await p.interact( oid="ut", method_name="doit", args=[], kargs={}, event={} )
assert "update" in actions

Expand Down

0 comments on commit 440a634

Please sign in to comment.