mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:08:38 +00:00
Keep launcher job status tied to its process
This commit is contained in:
@ -319,8 +319,10 @@ class Launcher:
|
||||
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:
|
||||
@ -341,15 +343,16 @@ class Launcher:
|
||||
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:
|
||||
@ -361,12 +364,14 @@ class Launcher:
|
||||
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:
|
||||
|
||||
@ -12,6 +12,7 @@ from pathlib import Path
|
||||
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")
|
||||
@ -94,6 +95,41 @@ class FetchTests(unittest.TestCase):
|
||||
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):
|
||||
|
||||
Reference in New Issue
Block a user