Island: Add locks to remote_run_aws.py

Locks will avoid the situation where is_running_on_aws is called before this service finished initializing
This commit is contained in:
vakarisz 2022-04-27 18:20:36 +03:00
parent d3c1ff89e9
commit 8535118e4f
1 changed files with 6 additions and 10 deletions

View File

@ -1,4 +1,5 @@
import logging
from threading import Lock
from common.cloud.aws.aws_instance import AwsInstance
from common.cloud.aws.aws_service import AwsService
@ -7,6 +8,7 @@ from common.cmd.cmd import Cmd
from common.cmd.cmd_runner import CmdRunner
logger = logging.getLogger(__name__)
aws_lock = Lock()
class RemoteRunAwsService:
@ -23,15 +25,8 @@ class RemoteRunAwsService:
:return: None
"""
if RemoteRunAwsService.aws_instance is None:
RemoteRunAwsService.try_init_aws_instance()
@staticmethod
def try_init_aws_instance():
# noinspection PyBroadException
try:
with aws_lock:
RemoteRunAwsService.aws_instance = AwsInstance()
except Exception:
logger.error("Failed init aws instance. Exception info: ", exc_info=True)
@staticmethod
def run_aws_monkeys(instances, island_ip):
@ -53,6 +48,7 @@ class RemoteRunAwsService:
@staticmethod
def is_running_on_aws():
with aws_lock:
return RemoteRunAwsService.aws_instance.is_instance()
@staticmethod