Say at the next start which job the launcher was stopped in.

A job that writes into the game folder leaves data/job-running.json while it runs and removes it when it ends. A
launcher closed or killed in the middle leaves it, and the next start logs it and tells the player which job did not
finish and that Rebuild the Game or Begin Installation goes on from the steps that were done.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-12 22:10:48 -05:00
parent 2a7fda9506
commit cc0e2c4bf4
4 changed files with 132 additions and 0 deletions

View File

@@ -3,6 +3,8 @@ staging of an update. Each job is a list of steps the window shows as rows; the
timer (rev changes on every change) and never blocks on it. The same jobs run headless from launcher/cli.py.
BackgroundUpdate decides when a staged update installs."""
import collections
import json
import os
import threading
import time
import traceback
@@ -18,6 +20,29 @@ PENDING, RUNNING, DONE, SKIPPED, FAILED = 'pending', 'running', 'done', 'skipped
PLAIN_FAILURES = (feed.FeedError, build.BuildError, locks.Busy)
# data/job-running.json: the job that is writing into the game folder, from its start to its end (Job._main). A job
# that ends, also by Cancel or an error, removes it and has told its result on screen. One the launcher was stopped
# in (closed by the system, a power cut, a crash) leaves it, and the next start says so (interrupted).
MARKER = 'job-running.json'
def interrupted(ctx):
"""The job the launcher was stopped in, as it wrote it ({'task', 'kind', 'game_dir', 'started'}), or None. The
marker is removed: it is told once. Call it while this process holds the launcher folder's lock and runs no
job, or the marker read is a running job's."""
path = ctx.layout.data / MARKER
try:
with open(path, encoding='utf-8') as f:
left = json.load(f)
except (OSError, ValueError):
left = None
try:
path.unlink(missing_ok=True)
except OSError:
pass
return left if isinstance(left, dict) and left.get('task') else None
def failure_line(e):
"""A failure in one line for the log: its kind, its message and what it came from."""
cause = e.__cause__
@@ -256,10 +281,27 @@ class Job:
file.release()
raise
def _mark(self):
"""Leave MARKER for the time the job writes into the game folder; True when it was written."""
if 'game' not in self.claims:
return False
try:
path = self.ctx.layout.data / MARKER
tmp = path.with_name(path.name + '.tmp')
with open(tmp, 'w', encoding='utf-8') as f:
json.dump({'task': self.doing(), 'kind': self.kind, 'game_dir': str(getattr(self, 'game_dir', '')),
'started': time.time()}, f)
os.replace(tmp, path)
return True
except OSError:
return False
def _main(self):
file = claimed = None
marked = False
try:
file, claimed = self._hold()
marked = self._mark()
self.run()
self.result = 'ok'
except net.Cancelled:
@@ -275,6 +317,11 @@ class Job:
if s.state == RUNNING:
s.state = FAILED if self.result == 'failed' else PENDING
s.value = 'Stopped' if self.result == 'failed' else 'Cancelled'
if marked:
try:
(self.ctx.layout.data / MARKER).unlink(missing_ok=True)
except OSError:
pass
locks.release_claim(claimed)
if file:
file.release()

View File

@@ -755,6 +755,11 @@ class App:
self.settings.save()
if self.ctx.zstd_missing: # told at once, not when Begin Installation stops at the build tools
self.message = self.ctx.zstd_missing
left = None if a.selftest else jobs.interrupted(self.ctx) # before any job of this start leaves its own
if left:
self.ctx.log(f'the launcher was stopped during {left["task"]} (started '
f'{time.strftime("%Y-%m-%d %H:%M", time.localtime(left.get("started") or 0))}, game folder '
f'{left.get("game_dir") or "none"})')
for old in self.layout.root.glob('python.old*'): # the Python an update replaced (launcher/native/nx2navi.c)
shutil.rmtree(old, ignore_errors=True)
if gd or self.settings.get('dump'):
@@ -771,6 +776,12 @@ class App:
# build from before the update and Rebuild the Game finishes it, not the first-run screen and its dump
self.show('home')
self.after_update_notice()
if left and not self.message:
# make_game.py keeps the old exe until the new one is linked and records each finished step, so the
# game folder is playable and a rebuild goes on from there; the player is told that it is due
self.message = (f'NX2navi was closed or stopped during {left["task"]}, which did not finish. Play '
'starts the last finished build; Rebuild the Game finishes what was left.')
self.render()
if self.install_staged('a staged update waited for the launcher to start'):
return
if a.play: # Play from Steam with a mod's code to build in first (play_only)
@@ -779,6 +790,9 @@ class App:
if self.settings.get('auto_check'):
self.check_now()
else:
if left and not self.message:
self.message = (f'NX2navi was closed or stopped during {left["task"]}, which did not finish. Begin '
'Installation goes on from the steps that were done.')
self.show('first_run')
self.find_dumps()