Skip to content

Commit

Permalink
fix: return exit code from download subprocess
Browse files Browse the repository at this point in the history
Use a multiprocessing queue to return the exit code from the download.run() function; if the queue is empty then fall back to the process exit code
  • Loading branch information
qx6ghqkz committed Jan 11, 2025
1 parent c9e660e commit d351dfe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
8 changes: 6 additions & 2 deletions gallery_dl_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ async def lifespan(app):
def download_task(url, options):
"""Initiate download as a subprocess and log output."""
log_queue = multiprocessing.Queue()
return_status = multiprocessing.Queue()

process = multiprocessing.Process(
target=download.run, args=(url, options, log_queue)
target=download.run, args=(url, options, log_queue, return_status)
)
process.start()

Expand All @@ -139,7 +140,10 @@ def download_task(url, options):

process.join()

exit_code = process.exitcode
try:
exit_code = return_status.get(block=False)
except queue.Empty:
exit_code = process.exitcode

if exit_code == 0:
log.info("Download job completed with exit code: 0")
Expand Down
4 changes: 2 additions & 2 deletions gallery_dl_server/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
log = output.initialise_logging(__name__)


def run(url, options, log_queue):
def run(url, options, log_queue, return_status):
"""Set gallery-dl configuration, set up logging and run download job."""
config.clear()

Expand Down Expand Up @@ -43,7 +43,7 @@ def run(url, options, log_queue):
status = -1
log.error(f"Exception: {e}")

return status
return_status.put(status)


def config_update(options):
Expand Down
4 changes: 2 additions & 2 deletions gallery_dl_server/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def write(self, msg):
return

if msg.startswith("# "):
msg = f"File already exists or its ID is in a download archive: {msg.removeprefix("# ")}"
msg = f"File already exists or its ID is in a download archive: {msg.removeprefix('# ')}"
self.level = logging.WARNING

self.logger.log(self.level, msg.strip())
Expand All @@ -213,7 +213,7 @@ def flush(self):
class NullWriter:
"""Suppress writes to stdout or stderr."""

def write(self, message):
def write(self, msg):
pass

def flush(self):
Expand Down

0 comments on commit d351dfe

Please sign in to comment.