Skip to content

Commit

Permalink
change spawn method
Browse files Browse the repository at this point in the history
  • Loading branch information
itsthejoker committed Aug 9, 2022
1 parent a2dbc27 commit 19e1659
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions bubbles/commands/update_and_restart.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import shlex
import subprocess
import traceback
Expand Down Expand Up @@ -50,15 +51,16 @@ def update(payload) -> None:
# write the new archive to disk
resp = requests.get(url, stream=True)
new_archive = folder / "temp.pyz"
with open(new_archive, 'wb') as new:
with open(new_archive, "wb") as new:
for chunk in resp.iter_content(chunk_size=8192):
new.write(chunk)

subprocess.check_output(shlex.split(f"chmod +x {str(new_archive)}"))

# make sure the new archive passes the internal tests
result = subprocess.run(
shlex.split(f"sh -c 'python3.10 {str(new_archive)} selfcheck'"), stdout=subprocess.DEVNULL
shlex.split(f"sh -c 'python3.10 {str(new_archive)} selfcheck'"),
stdout=subprocess.DEVNULL,
)
if result.returncode != 0:
say(f"Selfcheck failed! Stopping update.")
Expand All @@ -73,18 +75,15 @@ def update(payload) -> None:

say(f"Update to {release_data['name']} complete. Restarting...")

try:
# if this command succeeds, the process dies here
subprocess.check_output(shlex.split(f"sudo systemctl restart {USERNAME}"))
except subprocess.CalledProcessError:
say(f"Update failed, could not restart: \n```\n{traceback.format_exc()}```")
with current_zipfile() as archive:
with open(archive.filename, "wb") as current, open(
backup_archive, "rb"
) as backup:
current.write(backup.read())
say(f"Rolled back to {__version__}. Trying restart again...")
subprocess.check_output(shlex.split(f"sudo systemctl restart {USERNAME}"))
# spawn a new child that is separate from the parent process so that it doesn't
# die immediately as we respawn the parent
# https://stackoverflow.com/a/16928558
subprocess.Popen(
shlex.split(f"sudo systemctl restart {USERNAME}"),
stdout=open("/dev/null", "w"),
stderr=open("logfile.log", "a"),
preexec_fn=os.setpgrp,
)


PLUGIN = Plugin(
Expand Down

0 comments on commit 19e1659

Please sign in to comment.