From ef637e91b9b938af0555147aeb27978a141d2d68 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 15 Nov 2022 16:01:01 +0800 Subject: [PATCH] perf: add host deployment task --- apps/terminal/api/applet/host.py | 2 ++ .../0059_applethostdeployment_task.py | 18 ++++++++++++++++++ apps/terminal/models/applet/host.py | 5 +++++ apps/terminal/serializers/applet_host.py | 2 +- 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 apps/terminal/migrations/0059_applethostdeployment_task.py diff --git a/apps/terminal/api/applet/host.py b/apps/terminal/api/applet/host.py index 72321cc37..4542f3b8d 100644 --- a/apps/terminal/api/applet/host.py +++ b/apps/terminal/api/applet/host.py @@ -49,6 +49,7 @@ class AppletHostDeploymentViewSet(viewsets.ModelViewSet): serializer.is_valid(raise_exception=True) instance = serializer.save() task = run_applet_host_deployment.delay(instance.id) + instance.save_task(task.id) return Response({'task': str(task.id)}, status=201) @action(methods=['post'], detail=False, serializer_class=AppletHostDeployAppletSerializer) @@ -58,4 +59,5 @@ class AppletHostDeploymentViewSet(viewsets.ModelViewSet): applet_id = serializer.validated_data.get('applet_id') instance = serializer.save() task = run_applet_host_deployment_install_applet.delay(instance.id, applet_id) + instance.save_task(task.id) return Response({'task': str(task.id)}, status=201) diff --git a/apps/terminal/migrations/0059_applethostdeployment_task.py b/apps/terminal/migrations/0059_applethostdeployment_task.py new file mode 100644 index 000000000..5f455c9c6 --- /dev/null +++ b/apps/terminal/migrations/0059_applethostdeployment_task.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.14 on 2022-11-15 05:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('terminal', '0058_auto_20221103_1624'), + ] + + operations = [ + migrations.AddField( + model_name='applethostdeployment', + name='task', + field=models.UUIDField(null=True, verbose_name='Task'), + ), + ] diff --git a/apps/terminal/models/applet/host.py b/apps/terminal/models/applet/host.py index 3e510a3e3..83d9e3a41 100644 --- a/apps/terminal/models/applet/host.py +++ b/apps/terminal/models/applet/host.py @@ -105,6 +105,7 @@ class AppletHostDeployment(JMSBaseModel): date_start = models.DateTimeField(null=True, verbose_name=_('Date start'), db_index=True) date_finished = models.DateTimeField(null=True, verbose_name=_("Date finished")) comment = models.TextField(default='', blank=True, verbose_name=_('Comment')) + task = models.UUIDField(null=True, verbose_name=_('Task')) def start(self, **kwargs): from ...automations.deploy_applet_host import DeployAppletHostManager @@ -120,3 +121,7 @@ class AppletHostDeployment(JMSBaseModel): applet = None manager = DeployAppletHostManager(self, applet=applet) manager.install_applet(**kwargs) + + def save_task(self, task): + self.task = task + self.save(update_fields=['task']) diff --git a/apps/terminal/serializers/applet_host.py b/apps/terminal/serializers/applet_host.py index 2a291da73..c81258892 100644 --- a/apps/terminal/serializers/applet_host.py +++ b/apps/terminal/serializers/applet_host.py @@ -86,7 +86,7 @@ class HostAppletSerializer(AppletSerializer): class AppletHostDeploymentSerializer(serializers.ModelSerializer): class Meta: model = AppletHostDeployment - fields_mini = ['id', 'host', 'status'] + fields_mini = ['id', 'host', 'status', 'task'] read_only_fields = [ 'status', 'date_created', 'date_updated', 'date_start', 'date_finished'