Skip to content

Commit

Permalink
139 correct the update and delete routines for services (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrptrc authored Nov 22, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents cf7db27 + b983478 commit 8759832
Showing 10 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/webapp.yml
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ jobs:

- name: Install dependencies
working-directory: webapp
run: npm ci --legacy-peer-deps
run: npm ci

- name: Build app for dev
# Only run on main
8 changes: 8 additions & 0 deletions core-engine/src/common/exceptions.py
Original file line number Diff line number Diff line change
@@ -54,3 +54,11 @@ def __init__(self, message, message_to_send, linked_id):
self.message_to_send = message_to_send
self.linked_id = linked_id
super().__init__(self.message)


class ConstraintException(Exception):
"""Exception raised when a constraint is not respected."""

def __init__(self, message):
self.message = message
super().__init__(self.message)
1 change: 1 addition & 0 deletions core-engine/src/pipeline_executions/models.py
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ class PipelineExecution(PipelineExecutionBase, table=True):
pipeline: "Pipeline" = Relationship(back_populates="pipeline_executions")
current_pipeline_step: Union["PipelineStep", None] = Relationship(back_populates="pipeline_executions")
tasks: List[Task] = Relationship(
sa_relationship_kwargs={"cascade": "delete"},
back_populates="pipeline_execution",
)
files: List[FileKeyReference] | None = Field(sa_column=Column(JSON), default=None, nullable=True)
1 change: 1 addition & 0 deletions core-engine/src/pipeline_steps/models.py
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ class PipelineStep(
pipeline_id: UUID | None = Field(foreign_key="pipelines.id")
pipeline: "Pipeline" = Relationship(back_populates="steps") # noqa F821
pipeline_executions: List["PipelineExecution"] = Relationship(
sa_relationship_kwargs={"cascade": "delete"},
back_populates="current_pipeline_step"
) # noqa F821
service_id: UUID = Field(nullable=False, foreign_key="services.id")
5 changes: 4 additions & 1 deletion core-engine/src/pipelines/models.py
Original file line number Diff line number Diff line change
@@ -23,7 +23,10 @@ class Pipeline(PipelineBase, table=True):
__tablename__ = "pipelines"

id: UUID = Field(default_factory=uuid4, primary_key=True)
pipeline_executions: List["PipelineExecution"] = Relationship(back_populates="pipeline") # noqa F821
pipeline_executions: List["PipelineExecution"] = Relationship(
sa_relationship_kwargs={"cascade": "delete"},
back_populates="pipeline"
) # noqa F821
steps: List[PipelineStep] = Relationship(
sa_relationship_kwargs={"cascade": "delete"},
back_populates="pipeline"
2 changes: 1 addition & 1 deletion core-engine/src/pipelines/service.py
Original file line number Diff line number Diff line change
@@ -308,7 +308,7 @@ def update(self, app: FastAPI, pipeline_id: UUID, pipeline: PipelineUpdate):
needs=pipeline_step.needs,
condition=pipeline_step.condition,
inputs=pipeline_step.inputs,
service_id=pipeline_step.service.id,
service_id=service.id,
)
pipeline_step_create = PipelineStep.from_orm(new_pipeline_step)
self.session.add(new_pipeline_step)
4 changes: 3 additions & 1 deletion core-engine/src/services/controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List
from fastapi import APIRouter, Depends, HTTPException, Request
from common.exceptions import NotFoundException, ConflictException
from common.exceptions import NotFoundException, ConflictException, ConstraintException
from execution_units.enums import ExecutionUnitStatus
from services.service import ServicesService
from common.query_parameters import QueryParameters
@@ -144,3 +144,5 @@ def delete(
services_service.delete(service_id, request.app)
except NotFoundException as e:
raise HTTPException(status_code=404, detail=str(e))
except ConstraintException as e:
raise HTTPException(status_code=409, detail=str(e))
5 changes: 4 additions & 1 deletion core-engine/src/services/models.py
Original file line number Diff line number Diff line change
@@ -24,7 +24,10 @@ class Service(ServiceBase, table=True):
__tablename__ = "services"

id: UUID = Field(default_factory=uuid4, primary_key=True)
tasks: List["Task"] = Relationship(back_populates="service") # noqa F821
tasks: List["Task"] = Relationship(
sa_relationship_kwargs={"cascade": "delete"},
back_populates="service"
) # noqa F821
pipeline_steps: List["PipelineStep"] = Relationship(back_populates="service") # noqa F821


13 changes: 9 additions & 4 deletions core-engine/src/services/service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from inspect import Parameter, Signature
from sqlalchemy.exc import IntegrityError
from common_code.common.models import ExecutionUnitTag
from common_code.common.enums import ExecutionUnitTagName, ExecutionUnitTagAcronym
from fastapi import FastAPI, UploadFile, Depends, HTTPException
@@ -15,7 +16,7 @@
from common_code.logger.logger import Logger, get_logger
from config import Settings, get_settings
from services.models import Service, ServiceUpdate, ServiceTask
from common.exceptions import NotFoundException, ConflictException, UnreachableException
from common.exceptions import NotFoundException, ConflictException, UnreachableException, ConstraintException
from http_client import HttpClient
from fastapi.encoders import jsonable_encoder
from httpx import HTTPError
@@ -292,9 +293,13 @@ def delete(self, service_id: UUID, app: FastAPI):
current_service = self.session.get(Service, service_id)
if not current_service:
raise NotFoundException("Service Not Found")
self.session.delete(current_service)
self.remove_route(app, current_service.slug)
self.session.commit()
try:
self.session.delete(current_service)
self.remove_route(app, current_service.slug)
self.session.commit()
except IntegrityError:
raise ConstraintException(
"Service is linked to a pipeline, please update the related step in the pipeline first.")
self.logger.debug(f"Deleted service with id {current_service.id}")

def remove_route(self, app: FastAPI, slug: str):
2 changes: 1 addition & 1 deletion webapp/src/components/EngineStats/EngineStats.tsx
Original file line number Diff line number Diff line change
@@ -98,7 +98,7 @@ export const EngineStats: React.FC<{

const loadStats = async () => {
const stats = await getStats();
if (stats.total) {
if (stats.hasOwnProperty("total")) {
setStats(stats);
} else {
toast(`Error loading engine stats: ${stats.error}`, {type: "error"});

0 comments on commit 8759832

Please sign in to comment.