Skip to content

Commit

Permalink
restarting tmux sessions on page load.
Browse files Browse the repository at this point in the history
  • Loading branch information
pSpitzner committed Jul 29, 2024
1 parent 3489436 commit 0452dca
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- default config: mandatory fields cannot be set in the yaml, or they
might persist although the user sets them. moved to config loading in python.
- tmux session now restarts on page load if it is not alive.
- navbar, tags, inbox are now more friendly for mobile

### Added
- Backend to get cover art from metadata of music files.
- Impoved library view (friendlier for mobile, and a browser header component)
- Impoved library view (mobile friendly, and a browser header component)
- Library search

### Changed
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ To access the tmux from the host:
```
docker exec -it beets-flask /usr/bin/tmux attach-session -t beets-socket-term
```
Beware, you can close the tmux session, and we have not yet implemented a way to restart it. (Just restart the container)

If you use iTerm on macOS and want to create a profile for connecting to the tmux session natively:
```
Expand Down
24 changes: 20 additions & 4 deletions backend/beets_flask/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,23 @@ def register_tmux():
pane = window.active_pane or window.split_window(attach=True)


def is_session_alive():
try:
if len(session.windows) > 0: # type: ignore
# session.windows should raise if the session is not alive.
return True
else:
return False
except:
return False


def emit_output():
try:
# this approach to capture screen content keeps extra whitespaces, but we can
# fix that client side.
current = pane.cmd("capture-pane", "-p", "-N", "-T", "-e").stdout
if is_session_alive():
current = pane.cmd("capture-pane", "-p", "-N", "-T", "-e").stdout
else:
current = ["Session ended. Reload page to restart!"]
sio.emit("ptyOutput", {"output": current}, namespace="/terminal")
except Exception as e:
log.error(f"Error reading from pty: {e}")
Expand All @@ -69,7 +81,10 @@ def emit_output_continuously(sleep_seconds=0.1):
while True:
sio.sleep(sleep_seconds) # type: ignore
try:
current = pane.cmd("capture-pane", "-p", "-N", "-T", "-e").stdout
if is_session_alive():
current = pane.cmd("capture-pane", "-p", "-N", "-T", "-e").stdout
else:
current = ["Session ended. Reload page to restart!"]
if current != prev:
sio.emit("ptyOutput", {"output": current}, namespace="/terminal")
emit_cursor_position()
Expand Down Expand Up @@ -132,6 +147,7 @@ def resend_output(sid):
def connect(sid, environ):
"""new client connected"""
log.debug(f"TerminalSocket new client connected {sid}")
register_tmux()


@sio.on("disconnect", namespace="/terminal") # type: ignore
Expand Down

0 comments on commit 0452dca

Please sign in to comment.