Skip to content

Commit e1083dc

Browse files
committed
Handle RunnerDeleted event to delete associated datasets and parts
- Added `datasetParameterId` field to `RunnerDeleted` event. - Implemented `onRunnerDeleted` listener in `DatasetServiceImpl` to handle cleanup of datasets and parts linked to a runner. - Updated `RunnerService` to trigger `RunnerDeleted` with `datasetParameterId`.
1 parent 2e850be commit e1083dc

File tree

6 files changed

+45
-22
lines changed

6 files changed

+45
-22
lines changed

common/src/main/kotlin/com/cosmotech/common/events/RunnerEvents.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class RunnerDeleted(
1313
publisher: Any,
1414
val organizationId: String,
1515
val workspaceId: String,
16-
val runnerId: String
16+
val runnerId: String,
17+
val datasetParameterId: String
1718
) : CsmEvent(publisher)
1819

1920
class UpdateRunnerStatus(
@@ -24,7 +25,7 @@ class UpdateRunnerStatus(
2425
val lastRunId: String,
2526
) : CsmRequestResponseEvent<String>(publisher)
2627

27-
class GetAttachedRunnerToDataset(
28+
class GetRunnerAttachedToDataset(
2829
publisher: Any,
2930
val organizationId: String,
3031
val workspaceId: String,

dataset/src/integrationTest/kotlin/com/cosmotech/dataset/service/DatasetServiceIntegrationTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package com.cosmotech.dataset.service
55
import com.cosmotech.common.config.CsmPlatformProperties
66
import com.cosmotech.common.config.existTable
77
import com.cosmotech.common.events.CsmEventPublisher
8-
import com.cosmotech.common.events.GetAttachedRunnerToDataset
8+
import com.cosmotech.common.events.GetRunnerAttachedToDataset
99
import com.cosmotech.common.exceptions.CsmAccessForbiddenException
1010
import com.cosmotech.common.exceptions.CsmResourceNotFoundException
1111
import com.cosmotech.common.rbac.ROLE_ADMIN
@@ -751,7 +751,7 @@ class DatasetServiceIntegrationTest() : CsmTestBase() {
751751
val fakeRunnerId = "r-XXXXXX"
752752
every { eventPublisher.publishEvent(any()) } answers
753753
{
754-
firstArg<GetAttachedRunnerToDataset>().response = fakeRunnerId
754+
firstArg<GetRunnerAttachedToDataset>().response = fakeRunnerId
755755
}
756756

757757
val exception =

dataset/src/main/kotlin/com/cosmotech/dataset/service/DatasetServiceImpl.kt

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ package com.cosmotech.dataset.service
44

55
import com.cosmotech.common.CsmPhoenixService
66
import com.cosmotech.common.config.DATASET_INPUTS_SCHEMA
7-
import com.cosmotech.common.events.GetAttachedRunnerToDataset
7+
import com.cosmotech.common.events.GetRunnerAttachedToDataset
8+
import com.cosmotech.common.events.RunnerDeleted
89
import com.cosmotech.common.exceptions.CsmResourceNotFoundException
910
import com.cosmotech.common.id.generateId
1011
import com.cosmotech.common.rbac.CsmRbac
@@ -49,6 +50,7 @@ import org.apache.commons.lang3.StringUtils
4950
import org.postgresql.copy.CopyManager
5051
import org.postgresql.core.BaseConnection
5152
import org.postgresql.util.PSQLException
53+
import org.springframework.context.event.EventListener
5254
import org.springframework.core.io.ByteArrayResource
5355
import org.springframework.core.io.Resource
5456
import org.springframework.jdbc.core.JdbcTemplate
@@ -195,12 +197,12 @@ class DatasetServiceImpl(
195197
override fun deleteDataset(organizationId: String, workspaceId: String, datasetId: String) {
196198
val dataset = getVerifiedDataset(organizationId, workspaceId, datasetId, PERMISSION_DELETE)
197199

198-
val getAttachedRunnerToDatasetEvent =
199-
GetAttachedRunnerToDataset(this, organizationId, workspaceId, datasetId)
200+
val getRunnerAttachedToDatasetEvent =
201+
GetRunnerAttachedToDataset(this, organizationId, workspaceId, datasetId)
200202

201-
eventPublisher.publishEvent(getAttachedRunnerToDatasetEvent)
203+
eventPublisher.publishEvent(getRunnerAttachedToDatasetEvent)
202204

203-
val datasetAttachedToRunnerId = getAttachedRunnerToDatasetEvent.response
205+
val datasetAttachedToRunnerId = getRunnerAttachedToDatasetEvent.response
204206
require(datasetAttachedToRunnerId == null || datasetAttachedToRunnerId.isEmpty()) {
205207
"Dataset $datasetId is defined as a runner dataset ($datasetAttachedToRunnerId). It cannot be deleted"
206208
}
@@ -947,6 +949,25 @@ class DatasetServiceImpl(
947949
return datasetList
948950
}
949951

952+
@EventListener(RunnerDeleted::class)
953+
fun onRunnerDeleted(runnerDeletedEvent: RunnerDeleted) {
954+
val organizationId = runnerDeletedEvent.organizationId
955+
val workspaceId = runnerDeletedEvent.workspaceId
956+
val datasetParameterId = runnerDeletedEvent.datasetParameterId
957+
958+
val dataset =
959+
datasetRepository.findBy(organizationId, workspaceId, datasetParameterId).orElseThrow {
960+
CsmResourceNotFoundException(
961+
"Dataset $datasetParameterId not found in organization $organizationId and workspace $workspaceId")
962+
}
963+
964+
datasetRepository.delete(dataset)
965+
dataset.parts.forEach {
966+
datasetPartRepository.delete(it)
967+
datasetPartManagementFactory.removeData(it)
968+
}
969+
}
970+
950971
private fun validDatasetPartCreateRequest(
951972
datasetPartCreateRequest: DatasetPartCreateRequest,
952973
file: MultipartFile

runner/src/integrationTest/kotlin/com/cosmotech/runner/service/RunnerServiceIntegrationTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package com.cosmotech.runner.service
55
import com.cosmotech.common.config.CsmPlatformProperties
66
import com.cosmotech.common.containerregistry.ContainerRegistryService
77
import com.cosmotech.common.events.CsmEventPublisher
8-
import com.cosmotech.common.events.GetAttachedRunnerToDataset
8+
import com.cosmotech.common.events.GetRunnerAttachedToDataset
99
import com.cosmotech.common.events.HasRunningRuns
1010
import com.cosmotech.common.events.RunStart
1111
import com.cosmotech.common.events.UpdateRunnerStatus
@@ -2159,7 +2159,7 @@ class RunnerServiceIntegrationTest : CsmTestBase() {
21592159
}
21602160

21612161
@Test
2162-
fun `test onGetAttachedRunnerToDataset behaviour`() {
2162+
fun `test onGetRunnerAttachedToDataset behaviour`() {
21632163

21642164
logger.info(
21652165
"should create a new Runner and retrieve parameter varType from solution ignoring the one declared")
@@ -2179,7 +2179,7 @@ class RunnerServiceIntegrationTest : CsmTestBase() {
21792179
val datasetParameterId = newRunnerSaved.datasets.parameter
21802180

21812181
val getAttachedRunnerToDataset =
2182-
GetAttachedRunnerToDataset(
2182+
GetRunnerAttachedToDataset(
21832183
this, organizationSaved.id, workspaceSaved.id, datasetParameterId)
21842184
eventPublisher.publishEvent(getAttachedRunnerToDataset)
21852185

runner/src/main/kotlin/com/cosmotech/runner/service/RunnerApiServiceImpl.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package com.cosmotech.runner.service
44

55
import com.cosmotech.common.config.CsmPlatformProperties
6-
import com.cosmotech.common.events.GetAttachedRunnerToDataset
6+
import com.cosmotech.common.events.GetRunnerAttachedToDataset
77
import com.cosmotech.common.events.RunDeleted
88
import com.cosmotech.common.rbac.PERMISSION_CREATE_CHILDREN
99
import com.cosmotech.common.rbac.PERMISSION_DELETE
@@ -271,16 +271,16 @@ internal class RunnerApiServiceImpl(
271271
}
272272
}
273273

274-
@EventListener(GetAttachedRunnerToDataset::class)
275-
fun onGetAttachedRunnerToDataset(getAttachedRunnerToDataset: GetAttachedRunnerToDataset) {
276-
val organizationId = getAttachedRunnerToDataset.organizationId
277-
val workspaceId = getAttachedRunnerToDataset.workspaceId
278-
val datasetId = getAttachedRunnerToDataset.datasetId
274+
@EventListener(GetRunnerAttachedToDataset::class)
275+
fun onGetAttachedRunnerToDataset(getRunnerAttachedToDataset: GetRunnerAttachedToDataset) {
276+
val organizationId = getRunnerAttachedToDataset.organizationId
277+
val workspaceId = getRunnerAttachedToDataset.workspaceId
278+
val datasetId = getRunnerAttachedToDataset.datasetId
279279
val runnerService = getRunnerService().inOrganization(organizationId).inWorkspace(workspaceId)
280280

281281
val runnerId =
282282
runnerService.findRunnerByDatasetParameter(organizationId, workspaceId, datasetId)?.id
283283

284-
getAttachedRunnerToDataset.response = runnerId
284+
getRunnerAttachedToDataset.response = runnerId
285285
}
286286
}

runner/src/main/kotlin/com/cosmotech/runner/service/RunnerService.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ class RunnerService(
141141
newRoots.forEach { updateChildrenRootId(parent = it, newRootId = it.id) }
142142

143143
// Notify the deletion
144-
val runnerDeleted = RunnerDeleted(this, runner.organizationId, runner.workspaceId, runner.id)
144+
val runnerDeleted =
145+
RunnerDeleted(
146+
this, runner.organizationId, runner.workspaceId, runner.id, runner.datasets.parameter)
145147
this.eventPublisher.publishEvent(runnerDeleted)
146-
datasetApiService.deleteDataset(
147-
runner.organizationId, runner.workspaceId, runner.datasets.parameter)
148+
148149
return runnerRepository.delete(runnerInstance.getRunnerDataObjet())
149150
}
150151

0 commit comments

Comments
 (0)