perf: automation celery task (#9046)

Co-authored-by: feng <1304903146@qq.com>
pull/9048/head
fit2bot 2022-11-11 16:13:16 +08:00 committed by GitHub
parent 1cd551e692
commit cb82b53791
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 5 deletions

View File

@ -5,7 +5,6 @@ from rest_framework import status, mixins, viewsets
from orgs.mixins import generics
from assets import serializers
from assets.const import AutomationTypes
from assets.tasks import execute_automation
from assets.models import BaseAutomation, AutomationExecution
from common.const.choices import Trigger
@ -111,8 +110,7 @@ class AutomationExecutionViewSet(
serializer.is_valid(raise_exception=True)
automation = serializer.validated_data.get('automation')
tp = serializer.validated_data.get('type')
model = AutomationTypes.get_type_model(tp)
task = execute_automation.delay(
pid=automation.pk, trigger=Trigger.manual, model=model
pid=automation.pk, trigger=Trigger.manual, tp=tp
)
return Response({'task': task.id}, status=status.HTTP_201_CREATED)

View File

@ -47,7 +47,7 @@ class BaseAutomation(CommonModelMixin, PeriodTaskModelMixin, OrgModelMixin):
def get_register_task(self):
name = f"automation_{self.type}_strategy_period_{str(self.id)[:8]}"
task = execute_automation.name
args = (str(self.id), Trigger.timing, self._meta.model)
args = (str(self.id), Trigger.timing, self.type)
kwargs = {}
return name, task, args, kwargs

View File

@ -2,12 +2,14 @@ from celery import shared_task
from orgs.utils import tmp_to_root_org, tmp_to_org
from common.utils import get_logger, get_object_or_none
from assets.const import AutomationTypes
logger = get_logger(__file__)
@shared_task(queue='ansible')
def execute_automation(pid, trigger, model):
def execute_automation(pid, trigger, tp):
model = AutomationTypes.get_model(tp)
with tmp_to_root_org():
instance = get_object_or_none(model, pk=pid)
if not instance: