Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions launcher/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,10 @@ def say(self, *lines: str) -> None:
self.log.extend(lines)
self._send("log", list(lines))

def set_state(self, text: str) -> None:
def set_state(self, process: subprocess.Popen, text: str) -> None:
with self.lock:
if self.process is not process:
return
changed = text != self.job_state
self.job_state = text
if changed:
Expand All @@ -341,15 +343,16 @@ def start(self, kind: str, command: list[str]) -> str:
self.say("Preview (--no-run): nothing was started. This would run:", " " + shown)
return "Preview: nothing was started. The command is in the log on the Play page."
try:
self.process = core.spawn(command, cwd=self.repo)
process = core.spawn(command, cwd=self.repo)
except OSError as err:
self.say(f"Could not start: {err}")
return f"Could not start: {err}"
self.process = process
self.kind = kind
self.job_state = "Starting"
self.say("> " + shown)
self._send("state", None)
threading.Thread(target=self._follow, args=(self.process, kind), daemon=True).start()
threading.Thread(target=self._follow, args=(process, kind), daemon=True).start()
return ""

def _follow(self, process: subprocess.Popen, kind: str) -> None:
Expand All @@ -361,12 +364,14 @@ def _follow(self, process: subprocess.Popen, kind: str) -> None:
new = core.state_for_line(state, line + "\n")
if new != state:
state = new
self.set_state(state)
self.set_state(process, state)
code = process.wait()
self.say(f"Exited with code {code}.")
with self.lock:
if self.process is not process:
return
self.kind = ""
self.set_state("Ready" if code == 0 else f"Exited with code {code}")
self.job_state = "Ready" if code == 0 else f"Exited with code {code}"
self._send("state", None)

def play(self) -> str:
Expand Down
36 changes: 36 additions & 0 deletions tests/test_launcher_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import tempfile
import threading
import unittest
from unittest.mock import Mock, patch

ROOT = Path(__file__).resolve().parents[1]
_spec = importlib.util.spec_from_file_location("leonida_launcher_serve", ROOT / "launcher" / "serve.py")
Expand Down Expand Up @@ -94,6 +95,41 @@ def test_anything_else_is_not_kept(self):
self.assertEqual(os.listdir(dst), [])


class JobTests(unittest.TestCase):
def test_old_follower_leaves_the_new_job_state_intact(self):
with tempfile.TemporaryDirectory() as tmp:
launcher = serve.Launcher(settings_path=str(Path(tmp) / "settings.json"))
old = Mock(stdout=[b"loading\n"])
old.wait.return_value = 7
active = Mock()
active.poll.return_value = None
launcher.process = active
launcher.kind = "play"
launcher.job_state = "Starting"

with patch.object(serve.core, "state_for_line", return_value="Loading"):
launcher._follow(old, "play")

self.assertTrue(launcher.running())
self.assertEqual(launcher.kind, "play")
self.assertEqual(launcher.job_state, "Starting")

def test_current_follower_publishes_its_exit_status(self):
with tempfile.TemporaryDirectory() as tmp:
launcher = serve.Launcher(settings_path=str(Path(tmp) / "settings.json"))
process = Mock(stdout=[])
process.wait.return_value = 7
process.poll.return_value = 7
launcher.process = process
launcher.kind = "play"

launcher._follow(process, "play")

self.assertFalse(launcher.running())
self.assertEqual(launcher.kind, "")
self.assertEqual(launcher.job_state, "Exited with code 7")


class ServerTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
Expand Down