Skip to content

Commit

Permalink
eecs: delay lua execution, otherwise it blocks in init()
Browse files Browse the repository at this point in the history
  • Loading branch information
Piglit committed Aug 24, 2024
1 parent 10062f7 commit 4ad7bb7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
8 changes: 7 additions & 1 deletion eecs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from models import crew
from outbound import log
from outbound import stationsComms
from outbound import luaExecutor
from interfaces import eehttp
from interfaces import crew_pyro
import campaign
Expand All @@ -30,12 +31,17 @@
for file in files:
crew.loadCrew(file)

# start luaExecutor
luaExecutor.start()

# host crew edit pyro interface
servers = crew_pyro.Crews()
#Pyro4.util.SerializerBase.register_class_to_dict(crew.Crew, lambda c: c.__dict__)
pyrohelper.host_named_server(servers, "campaign_crews")

# host http interface
uvicorn.run("main:eehttp.app", host="0.0.0.0", reload=False, port=8888)
pyrohelper.cleanup()

# shutdown
pyrohelper.cleanup()
luaExecutor.stop()
24 changes: 14 additions & 10 deletions eecs/models/crew.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import models.scenario
from interfaces import storage
from models.scenario import Scenario
from outbound import luaExecutor
from utils import lua

import os
Expand Down Expand Up @@ -152,7 +153,6 @@ def getRecentScore(self):
}
scenario = current["scenario"]
ss = self.scores[scenario]
pprint(ss)
hi = Crew.getFleetHighscore(scenario)
ret["current_scenario_name"] = current["scenario_name"]
if "time" in current:
Expand Down Expand Up @@ -259,9 +259,11 @@ def sendArtifacts(self):
_OBJECT_=getPlayerShip(-1)
_OBJECT_:addToShipLog("You carry {amount} artifact{"s" if amount > 1 else ""} from previous missions to deliver to the fleet command station.", "green")"""
script += script_artifacts
if lua.exec(script, self.instance_name+":8080"): # instance name must resolv to actual server!
pass
#self.artifacts.clear() TODO
luaExecutor.exec(script, self.instance_name+":8080", 0, Crew._artifactCallback, [self])

def _artifactCallback(self, success):
if success:
self.artifacts.clear()

def sendReputation(self, reduce=False):
"""send reputation bonus to server, granting it the current ship"""
Expand All @@ -273,12 +275,14 @@ def sendReputation(self, reduce=False):
_OBJECT_:addReputationPoints({amount})
_OBJECT_:addToShipLog("Reputation: +{amount} from previous missions", "green")
"""
# TODO: here a timeout happens! Make async!
if lua.exec(script, self.instance_name+":8080"): # instance name must resolv to actual server!
if reduce:
if "delivered" not in self.scores:
self.scores["delivered"] = {"reputation": 0}
self.scores["delivered"]["reputation"] -= amount# TODO test
luaExecutor.exec(script, self.instance_name+":8080", 1, Crew._repCallback, [self, reduce])

def _repCallback(self, reduce, success):
if success and reduce:
if "delivered" not in self.scores:
self.scores["delivered"] = {"reputation": 0}
self.scores["delivered"]["reputation"] -= amount# TODO test


def storeCrew(self):
storage.storeInfo(self, self.instance_name, subdir="crews")
Expand Down
34 changes: 34 additions & 0 deletions eecs/outbound/luaExecutor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""A worker thread, that handles all the lua exec calls asynchronously to prevent deadlocks."""

from multiprocessing import Process, Queue
import time

from utils import lua

p = None
q = Queue()

def exec(script, server="127.0.0.1:8080", delay=0, callback=None, callbackArgs=[]):
q.put(("exec", script, server, delay, callback, callbackArgs))


def work(q):
while task := q.get():
(function, script, server, delay, callback, callbackArgs) = task
if delay:
time.sleep(delay)
ret = None
if function == "exec":
ret = lua.exec(script, server)
if callback:
callback(*callbackArgs, ret)


def start():
global p
p = Process(target=work, args=(q,))
p.start()

def stop():
q.put(None)
p.join()
10 changes: 5 additions & 5 deletions eecs/outbound/stationsComms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This sends activites to the subscribed station"""

from utils import lua
from outbound import luaExecutor
from interfaces import storage
import functools
import json
Expand All @@ -21,7 +22,6 @@ def sanitize_message(crew, what):
def backlog_append(crew, what, **kwargs):
msg, instance_name = sanitize_message(crew, what)
backlog.append((msg, instance_name))
pprint(backlog)
if existing_subscription:
backlog_send()

Expand All @@ -39,11 +39,11 @@ def backlog_send():
script += f"""
_OBJECT_:addToShipLog("{msg}", "{color}")
_OBJECT_:addCustomInfo("ShipLog", "{instance_name}", "{msg}")"""
try:
lua.exec(script, station_server+":8080")
luaExecutor.exec(script, station_server+":8080", 0, _callback)

def _callback(success):
if success:
backlog.clear()
except requests.exceptions.ConnectionError:
pass

def status_update(crew, what, **kwargs):
if not existing_subscription:
Expand Down

0 comments on commit 4ad7bb7

Please sign in to comment.