10000 Refactor EventBridge ArchiveServices to allow restoration without side effects by dfangl · Pull Request #11891 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

Refactor EventBridge ArchiveServices to allow restoration without side effects #11891

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 1 commit into from
Nov 22, 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
58 changes: 32 additions & 26 deletions localstack-core/localstack/services/events/archive.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
from datetime import datetime, timezone
from typing import Self

from botocore.client import BaseClient

Expand Down Expand Up @@ -42,33 +43,42 @@ class ArchiveService:
rule_name: RuleName
target_id: TargetId

def __init__(
self,
def __init__(self, archive: Archive):
self.archive = archive
self.set_state(ArchiveState.CREATING)
self.set_creation_time()
self.client: BaseClient = self._initialize_client()
self.event_bus_name: EventBusName = extract_event_bus_name(archive.event_source_arn)
self.set_state(ArchiveState.ENABLED)
self.rule_name = f"Events-Archive-{self.archive_name}"
self.target_id = f"Events-Archive-{self.archive_name}"

@classmethod
def create_archive_service(
cls,
archive_name: ArchiveName,
region: str,
account_id: str,
event_source_arn: Arn,
description: ArchiveDescription,
e 10000 vent_pattern: EventPattern,
retention_days: RetentionDays,
):
self.archive = Archive(
archive_name,
region,
account_id,
event_source_arn,
description,
event_pattern,
retention_days,
) -> Self:
return cls(
Archive(
archive_name,
region,
account_id,
event_source_arn,
description,
event_pattern,
retention_days,
)
)
self.set_state(ArchiveState.CREATING)
self.set_creation_time()
self.client: BaseClient = self._initialize_client()
self.event_bus_name: EventBusName = extract_event_bus_name(event_source_arn)

self.rule_name: RuleName = self._create_archive_rule()
self.target_id: TargetId = self._create_archive_target()
self.set_state(ArchiveState.ENABLED)
def register_archive_rule_and_targets(self):
self._create_archive_rule()
self._create_archive_target()

def __getattr__(self, name):
return getattr(self.archive, name)
Expand Down Expand Up @@ -133,8 +143,7 @@ def _initialize_client(self) -> BaseClient:

def _create_archive_rule(
self,
) -> RuleName:
rule_name = f"Events-Archive-{self.name}"
):
default_event_pattern = {
"replay-name": [{"exists": False}],
}
Expand All @@ -144,25 +153,22 @@ def _create_archive_rule(
else:
updated_event_pattern = default_event_pattern
self.client.put_rule(
Name=rule_name,
Name=self.rule_name,
EventBusName=self.event_bus_name,
EventPattern=json.dumps(updated_event_pattern),
)
return rule_name

def _create_archive_target(
self,
) -> TargetId:
):
"""Creates a target for the archive rule. The target is required for accessing parameters
from the provider during sending of events to the target but it is not invoked
because events are put to the archive directly to not overload the gateway"""
target_id = f"Events-Archive-{self.name}"
self.client.put_targets(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for separating the initial creation to avoid such boto calls during persistence loading 👍

Rule=self.rule_name,
EventBusName=self.event_bus_name,
Targets=[{"Id": target_id, "Arn": self.arn}],
Targets=[{"Id": self.target_id, "Arn": self.arn}],
)
return target_id

def _normalize_datetime(self, dt: datetime) -> datetime:
return dt.replace(second=0, microsecond=0)
Expand Down
4 changes: 3 additions & 1 deletion localstack-core/localstack/services/events/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,7 @@ def start_replay(
re_formatted_event_to_replay = replay_service.re_format_events_from_archive(
events_to_replay, replay_name
)
# TODO should this really be run synchronously within the request?
self._process_entries(context, re_formatted_event_to_replay)
replay_service.finish()

Expand Down Expand Up @@ -1733,7 +1734,7 @@ def create_archive_service(
event_pattern: EventPattern,
retention_days: RetentionDays,
) -> ArchiveService:
archive_service = ArchiveService(
archive_service = ArchiveService.create_archive_service(
archive_name,
region,
account_id,
Expand All @@ -1742,6 +1743,7 @@ def create_archive_service(
event_pattern,
retention_days,
)
archive_service.register_archive_rule_and_targets()
self._archive_service_store[archive_service.arn] = archive_service
return archive_service

Expand Down
Loading
0