Thanks to visit codestin.com
Credit goes to github.com

Skip to content

remove legacy runtime in localstack/service/infra.py #11202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions localstack-core/localstack/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,6 @@ def populate_edge_configuration(
# the gateway server that should be used (supported: hypercorn, twisted dev: werkzeug)
GATEWAY_SERVER = os.environ.get("GATEWAY_SERVER", "").strip() or "twisted"

# whether to use the legacy runtime (``localstack.service.infra``)
LEGACY_RUNTIME = is_env_true("LEGACY_RUNTIME")

# IP of the docker bridge used to enable access between containers
DOCKER_BRIDGE_IP = os.environ.get("DOCKER_BRIDGE_IP", "").strip()

Expand Down Expand Up @@ -1211,7 +1208,6 @@ def use_custom_dns():
"LAMBDA_SQS_EVENT_SOURCE_MAPPING_INTERVAL",
"LEGACY_DOCKER_CLIENT",
"LEGACY_SNS_GCM_PUBLISHING",
"LEGACY_RUNTIME",
"LOCALSTACK_API_KEY",
"LOCALSTACK_AUTH_TOKEN",
"LOCALSTACK_HOST",
Expand Down
27 changes: 2 additions & 25 deletions localstack-core/localstack/runtime/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,14 @@
import logging
import os
import signal
import threading

from localstack.runtime import events
from localstack.utils import objects

LOG = logging.getLogger(__name__)

# event flag indicating the infrastructure has been started and that the ready marker has been printed
# TODO: deprecated, use events.infra_ready
INFRA_READY = events.infra_ready

# event flag indicating that the infrastructure has been shut down
SHUTDOWN_INFRA = threading.Event()

# can be set
EXIT_CODE: objects.Value[int] = objects.Value(0)


def signal_supervisor_restart():
# TODO: we should think about moving the localstack-supervisor into a script in the runtime,
# and make `signal_supervisor_restart` part of the supervisor code.
if pid := os.environ.get("SUPERVISOR_PID"):
os.kill(int(pid), signal.SIGUSR1)
else:
LOG.warning("could not signal supervisor to restart localstack")


def exit_infra(code: int):
"""
Triggers an orderly shutdown of the localstack infrastructure and sets the code the main process should
exit with to a specific value.

:param code: the exit code the main process should return with
"""
EXIT_CODE.set(code)
SHUTDOWN_INFRA.set()
27 changes: 1 addition & 26 deletions localstack-core/localstack/runtime/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,6 @@
from localstack.runtime.exceptions import LocalstackExit


def main_legacy():
from localstack.services import infra

# signal handler to make sure SIGTERM properly shuts down localstack
def _terminate_localstack(sig: int, frame):
infra.exit_infra(0)

# SIGINT is currently managed implicitly in start_infra via `except KeyboardInterrupt`
signal.signal(signal.SIGTERM, _terminate_localstack)

try:
infra.start_infra(asynchronous=False)
except LocalstackExit as e:
sys.exit(e.code)

sys.exit(infra.EXIT_CODE.get())


def print_runtime_information(in_docker: bool = False):
# FIXME: this is legacy code from the old CLI, reconcile with new CLI and runtime output
from localstack.utils.container_networking import get_main_container_name
Expand Down Expand Up @@ -70,7 +52,7 @@ def print_runtime_information(in_docker: bool = False):
print()


def main_v2():
def main():
from localstack.logging.setup import setup_logging_from_config
from localstack.runtime import current

Expand Down Expand Up @@ -107,12 +89,5 @@ def _terminate_localstack(sig: int, frame):
sys.exit(runtime.exit_code)


def main():
if config.LEGACY_RUNTIME:
main_legacy()
else:
main_v2()


if __name__ == "__main__":
main()
26 changes: 1 addition & 25 deletions localstack-core/localstack/runtime/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, components: Components):
self.ready = events.infra_ready
self.stopping = events.infra_stopping
self.stopped = events.infra_stopped
self.exit_code = 0
self._lifecycle_lock = threading.RLock()

def run(self):
Expand All @@ -63,8 +64,6 @@ def run(self):
# TODO: ideally we pass down a `shutdown` event that can be waited on so we can cancel the thread
# if the runtime shuts down beforehand
threading.Thread(target=self._run_ready_monitor, daemon=True).start()
# FIXME: legacy compatibility code
threading.Thread(target=self._run_shutdown_monitor, daemon=True).start()

# run the main control loop of the server and block execution
try:
Expand Down Expand Up @@ -168,29 +167,6 @@ def _cleanup_resources(self):
threads.cleanup_threads_and_processes()
self._clear_tmp_directory()

# more legacy compatibility code
@property
def exit_code(self):
# FIXME: legacy compatibility code
from localstack.runtime import legacy

return legacy.EXIT_CODE.get()

@exit_code.setter
def exit_code(self, value):
# FIXME: legacy compatibility code
from localstack.runtime import legacy

legacy.EXIT_CODE.set(value)

def _run_shutdown_monitor(self):
# FIXME: legacy compatibility code. this can be removed once we replace access to the
# ``SHUTDOWN_INFRA`` event with ``get_current_runtime().shutdown()``.
from localstack.runtime import legacy

legacy.SHUTDOWN_INFRA.wait()
self.shutdown()


def create_from_environment() -> LocalstackRuntime:
"""
Expand Down
Loading
Loading