-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: Check for Parallel ECS Tasks (#11)
FEAT: Check for Parallel ECS Tasks Although unlikely because of the configuration in our infrastructure, the dmap import task could be ran more than once in parallel. If they were both writing to the RDS at the same time, chaotic behavior could result. To prevent this, check for parallel tasks before starting the main process using a boto3 ecs client.
- Loading branch information
1 parent
c6bf458
commit 80d3f2a
Showing
3 changed files
with
60 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import os | ||
import boto3 | ||
|
||
from dmap_import.util_logging import ProcessLogger | ||
|
||
|
||
def running_in_aws() -> bool: | ||
""" | ||
return True if running on aws, else False | ||
""" | ||
return bool(os.getenv("AWS_DEFAULT_REGION")) | ||
|
||
|
||
def check_for_parallel_tasks() -> None: | ||
""" | ||
Check that that this task is not already running on ECS | ||
""" | ||
if not running_in_aws(): | ||
return | ||
|
||
process_logger = ProcessLogger("check_for_tasks") | ||
process_logger.log_start() | ||
|
||
client = boto3.client("ecs") | ||
dmap_ecs_cluster = os.environ["ECS_CLUSTER"] | ||
dmap_ecs_task_group = os.environ["ECS_TASK_GROUP"] | ||
|
||
# get all of the tasks running on the cluster | ||
task_arns = client.list_tasks(cluster=dmap_ecs_cluster)["taskArns"] | ||
|
||
# if tasks are running on the cluster, get their descriptions and check to | ||
# count matches the ecs task group. | ||
match_count = 0 | ||
if task_arns: | ||
running_tasks = client.describe_tasks( | ||
cluster=dmap_ecs_cluster, tasks=task_arns | ||
)["tasks"] | ||
|
||
for task in running_tasks: | ||
if dmap_ecs_task_group == task["group"]: | ||
match_count += 1 | ||
|
||
# if the group matches, raise an exception that will terminate the process | ||
if match_count > 1: | ||
exception = Exception("Multiple Tasks Running") | ||
process_logger.log_failure(exception) | ||
raise exception | ||
|
||
process_logger.log_complete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters