Skip to content

Commit

Permalink
feat: release history view add publish event and fix release log (#113)
Browse files Browse the repository at this point in the history
* feat: release history view add publish event and fix release log
  • Loading branch information
Han-Ya-Jun authored Jul 21, 2023
1 parent 9c138ac commit 0b76528
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def _create_release_task_for_shared_gateway(self, release: Release, release_hist
release_id=release.pk,
micro_gateway_release_history_id=history.pk,
micro_gateway_id=shared_gateway.pk,
publish_id=release_history.pk,
) # type: ignore

def _create_release_task_for_micro_gateway(self, release: Release, release_history: ReleaseHistory):
Expand Down
17 changes: 16 additions & 1 deletion src/dashboard/apigateway/apigateway/apps/release/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from apigateway.apps.release.releasers import ReleaseBatchManager, ReleaseError
from apigateway.apps.support.models import ReleasedResourceDoc
from apigateway.biz.released_resource import ReleasedResourceData
from apigateway.core.models import Release, ReleasedResource, ReleaseHistory
from apigateway.core.models import PublishEvent, Release, ReleasedResource, ReleaseHistory
from apigateway.utils.access_token import get_user_access_token_from_request
from apigateway.utils.responses import FailJsonResponse, OKJsonResponse
from apigateway.utils.swagger import PaginatedResponseSwaggerAutoSchema
Expand Down Expand Up @@ -150,6 +150,21 @@ def list(self, request, *args, **kwargs):
)
page = self.paginate_queryset(queryset)

# 查询发布事件,填充message为最后一个发布事件的状态信息
publish_ids = [release_history.id for release_history in page]

# 发布事件dict:key:publish_id,value: 最后一个事件
# 需要按照 "publish_id", "step", "status" 升序(django默认 ASC)排列,正确排列每个事件节点的不同状态事件
publish_events = PublishEvent.objects.filter(publish_id__in=publish_ids).order_by(
"publish_id", "step", "status"
)
publish_last_event = dict((event.publish_id, event) for event in publish_events)

for history in page:
event = publish_last_event.get(history.id)
if event:
history.message = f"{event.name}:{event.status}"

serializer = self.get_serializer(page, many=True)
return OKJsonResponse("OK", data=self.paginator.get_paginated_data(serializer.data))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ def distribute(
release: Release,
micro_gateway: MicroGateway,
release_task_id: Optional[str] = None,
release_history_id: Optional[int] = None,
publish_id: Optional[int] = None,
) -> bool:
"""将 release 发布到 micro-gateway 对应的 registry 中"""
convertor = CustomResourceConvertor(
release=release,
publish_id=release_history_id,
publish_id=publish_id,
micro_gateway=micro_gateway,
include_config=self.include_gateway_global_config,
include_plugin_metadata=self.include_gateway_global_config,
Expand All @@ -69,6 +69,7 @@ def distribute(
stage=release.stage,
micro_gateway=micro_gateway,
release_task_id=release_task_id,
publish_id=publish_id,
)

try:
Expand All @@ -94,7 +95,7 @@ def revoke(
stage: Stage,
micro_gateway: MicroGateway,
release_task_id: Optional[str] = None,
release_history_id: Optional[int] = None,
publish_id: Optional[int] = None,
) -> bool:
"""撤销已发布到 micro-gateway 对应的 registry 中的配置"""
registry = self._get_registry(stage.api, stage, micro_gateway)
Expand All @@ -105,6 +106,7 @@ def revoke(
stage=stage,
micro_gateway=micro_gateway,
release_task_id=release_task_id,
publish_id=publish_id,
)

try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
stage: Optional[Stage] = None,
micro_gateway: Optional[MicroGateway] = None,
release_task_id: Optional[str] = None,
publish_id: Optional[int] = None,
):
"""
:param release_task_id: 发布任务ID,触发发布任务时,可以设置一个 uuid 字符串,其将会打印到日志中,便于过滤日志
Expand All @@ -43,6 +44,7 @@ def __init__(
self._stage = stage
self._micro_gateway = micro_gateway
self.release_task_id = release_task_id or str(uuid.uuid4())
self._publish_id = publish_id

@property
def _message_prefix(self):
Expand All @@ -56,6 +58,8 @@ def _message_prefix(self):
if self._micro_gateway:
parts.append(f"micro_gateway={self._micro_gateway.pk}")

if self._publish_id:
parts.append(f"publish_id={self._publish_id}")
parts.append(f"release_task_id={self.release_task_id}")

return f"procedure {self.name}: {', '.join(parts)}"
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# to the current version of the project delivered to anyone in the future.
#
import logging
from typing import List
from typing import List, Optional

from celery import shared_task

Expand Down Expand Up @@ -161,7 +161,9 @@ def release_gateway_by_helm(access_token: str, username, release_id, micro_gatew


@shared_task(ignore_result=True)
def release_gateway_by_registry(micro_gateway_id, release_id, micro_gateway_release_history_id):
def release_gateway_by_registry(
micro_gateway_id, release_id, micro_gateway_release_history_id, publish_id: Optional[int] = None
):
"""发布资源到共享网关,为了使得类似环境变量等引用生效,同时会将所有配置都进行同步"""
logger.info(
"release_gateway_by_etcd: release_id=%s, micro_gateway_id=%s, micro_gateway_release_history_id=%s",
Expand All @@ -179,6 +181,8 @@ def release_gateway_by_registry(micro_gateway_id, release_id, micro_gateway_rele
gateway=release.api,
stage=release.stage,
micro_gateway=micro_gateway,
release_task_id=micro_gateway_release_history_id,
publish_id=publish_id,
)
return _release_gateway(
distributor=EtcdDistributor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ def test_do_release_shared_gateway(
micro_gateway_id=fake_shared_gateway.id,
release_id=fake_release.id,
micro_gateway_release_history_id=mocker.ANY,
publish_id=fake_release_history.id,
)

assert ReleaseHistory.objects.filter(
Expand Down

0 comments on commit 0b76528

Please sign in to comment.