Skip to content

Commit

Permalink
Actually cancel jobs when receiving SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
Eijebong committed Dec 18, 2024
1 parent d4c3454 commit 46bf555
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion bitrisescript/src/bitrisescript/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import asyncio
import logging
import os
import signal

from bitrisescript.bitrise import BitriseClient, find_running_build, get_running_builds, run_build, wait_and_download_workflow_log
from bitrisescript.task import get_artifact_dir, get_bitrise_app, get_bitrise_workflows, get_build_params
Expand All @@ -12,6 +13,11 @@
log = logging.getLogger(__name__)


def handle_sigterm(futures):
log.info("SIGTERM received, cancelling futures")
futures.cancel()


async def async_main(config, task):
app = get_bitrise_app(config, task)
log.info(f"Bitrise app: '{app}'")
Expand All @@ -36,7 +42,10 @@ async def async_main(config, task):
futures.append(run_build(artifact_dir, **build_params))

await client.set_app_prefix(app)
await asyncio.gather(*futures)
future_group = asyncio.gather(*futures)
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGTERM, handle_sigterm, future_group)
await future_group
finally:
if client:
await client.close()
Expand Down

0 comments on commit 46bf555

Please sign in to comment.