Skip to content

Commit

Permalink
bugfix: 修复指定多配置模板检查时部分检查结果未展示的问题 (fixed #254)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Apr 21, 2023
1 parent 686802f commit db8918e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CodeCov
on: [push, pull_request]
jobs:
run:
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
env:
OS: ubuntu-18.04
OS: ubuntu-20.04
PYTHON: "3.6"
DJANGO_SETTINGS_MODULE: "settings"
DEBUG: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ on:
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
permissions:
actions: read
contents: read
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on: [push, pull_request]
jobs:
build:

runs-on: ubuntu-latest
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release_1_push_rc_tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
env:
# 构建环境的Python版本
PYTHON_VERSION: "3.7"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release_2_create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
env:
# 构建环境的Python版本
PYTHON_VERSION: "3.7"
Expand Down
8 changes: 7 additions & 1 deletion apps/gsekit/job/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import List, Dict

from django.conf import settings
from django.db import models
from django.db import models, transaction
from django.db.models import QuerySet, Q
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -170,6 +170,12 @@ class JobTask(models.Model):
pipeline_id = models.CharField(_("PIPELINE ID"), max_length=33, db_index=True)
extra_data = models.JSONField(_("额外数据"), default=dict)

@classmethod
def set_status_by_id(cls, job_task_id: int, status, extra_data=None):
with transaction.atomic():
job_task = cls.objects.get(id=job_task_id)
job_task.set_status(status, extra_data)

def set_status(self, status, extra_data=None):
if extra_data is None:
extra_data = {}
Expand Down
42 changes: 25 additions & 17 deletions apps/gsekit/pipeline_plugins/components/collections/configfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from collections import defaultdict
from typing import List, Dict, Any, Optional, Set

from django.db import OperationalError
from django.db import OperationalError, transaction
from django.db.models import Max
from django.utils.translation import ugettext as _

Expand Down Expand Up @@ -987,13 +987,16 @@ def handle_succeeded_job_tasks(
config_inst_ids: List[int] = config_inst_ids_gby_job_task_id.get(job_task.id)
config_inst_id: int = config_inst_ids[0]
target_config_instance: Optional[Dict[str, Any]] = None
for config_instance in job_task.extra_data.get("config_instances") or []:
target_config_instance_index: Optional[int] = None
for index, config_instance in enumerate(job_task.extra_data.get("config_instances") or []):
if config_instance["id"] == config_inst_id:
target_config_instance_index = index
target_config_instance = config_instance
break

if not target_config_instance:
job_task.set_status(
JobTask.set_status_by_id(
job_task.id,
JobStatus.FAILED,
extra_data={
"err_code": exceptions.ConfigInstanceDoseNotExistException().code,
Expand All @@ -1004,16 +1007,17 @@ def handle_succeeded_job_tasks(

config_snapshot_content = job_instance_ip_log["log_content"]
if config_snapshot_content == "GSEKIT-404":
job_task.set_status(
JobStatus.FAILED,
extra_data={
with transaction.atomic():
newest_job_task: JobTask = JobTask.objects.get(id=job_task.id)
err = {
"err_code": exceptions.ConfigSnapshotDoseNotExistException().code,
"failed_reason": _("现网配置不存在:{path}").format(
path=f"{target_config_instance['path']}/{target_config_instance['file_name']}"
),
},
)
continue
}
newest_job_task.extra_data["config_instances"][target_config_instance_index]["err"] = err
newest_job_task.set_status(JobStatus.FAILED, extra_data=err)
continue

sha256 = hashlib.sha256()
sha256.update(config_snapshot_content.encode())
Expand All @@ -1039,16 +1043,20 @@ def handle_succeeded_job_tasks(
"config_instance_id": config_inst_id,
}

if target_config_instance["sha256"] != sha256sum:
job_task.set_status(
JobStatus.FAILED,
extra_data={
# 配置检查为并行逻辑,且会对 extra 更新,从而有可能存在检查不同配置写入结果时相互覆盖的风险
# 通过事务 + 查取最新快照后再进行更新,避免以上风险
with transaction.atomic():
newest_job_task: JobTask = JobTask.objects.get(id=job_task.id)
newest_job_task.extra_data["config_instances"][target_config_instance_index] = target_config_instance
if target_config_instance["sha256"] != sha256sum:
err = {
"err_code": exceptions.ConfigChangeException().code,
"failed_reason": _("现网配置与最近一次下发的配置不一致"),
},
)
else:
job_task.save(update_fields=["extra_data"])
}
newest_job_task.extra_data["config_instances"][target_config_instance_index]["err"] = err
newest_job_task.set_status(JobStatus.FAILED, extra_data=err)
else:
newest_job_task.save(update_fields=["extra_data"])

ConfigSnapshot.objects.bulk_create(to_be_created_config_snapshots)
ConfigSnapshot.objects.bulk_update(
Expand Down

0 comments on commit db8918e

Please sign in to comment.