Skip to content

Commit b1bc493

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 18c4e39 commit b1bc493

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
@@ -753,7 +753,7 @@ class DatasetServiceIntegrationTest() : CsmTestBase() {
753753
val fakeRunnerId = "r-XXXXXX"
754754
every { eventPublisher.publishEvent(any()) } answers
755755
{
756-
firstArg<GetAttachedRunnerToDataset>().response = fakeRunnerId
756+
firstArg<GetRunnerAttachedToDataset>().response = fakeRunnerId
757757
}
758758

759759
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
}
@@ -948,6 +950,25 @@ class DatasetServiceImpl(
948950
return datasetList
949951
}
950952

953+
@EventListener(RunnerDeleted::class)
954+
fun onRunnerDeleted(runnerDeletedEvent: RunnerDeleted) {
955+
val organizationId = runnerDeletedEvent.organizationId
956+
val workspaceId = runnerDeletedEvent.workspaceId
957+
val datasetParameterId = runnerDeletedEvent.datasetParameterId
958+
959+
val dataset =
960+
datasetRepository.findBy(organizationId, workspaceId, datasetParameterId).orElseThrow {
961+
CsmResourceNotFoundException(
962+
"Dataset $datasetParameterId not found in organization $organizationId and workspace $workspaceId")
963+
}
964+
965+
datasetRepository.delete(dataset)
966+
dataset.parts.forEach {
967+
datasetPartRepository.delete(it)
968+
datasetPartManagementFactory.removeData(it)
969+
}
970+
}
971+
951972
private fun validDatasetPartCreateRequest(
952973
datasetPartCreateRequest: DatasetPartCreateRequest,
953974
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
@@ -2161,7 +2161,7 @@ class RunnerServiceIntegrationTest : CsmTestBase() {
21612161
}
21622162

21632163
@Test
2164-
fun `test onGetAttachedRunnerToDataset behaviour`() {
2164+
fun `test onGetRunnerAttachedToDataset behaviour`() {
21652165

21662166
logger.info(
21672167
"should create a new Runner and retrieve parameter varType from solution ignoring the one declared")
@@ -2181,7 +2181,7 @@ class RunnerServiceIntegrationTest : CsmTestBase() {
21812181
val datasetParameterId = newRunnerSaved.datasets.parameter
21822182

21832183
val getAttachedRunnerToDataset =
2184-
GetAttachedRunnerToDataset(
2184+
GetRunnerAttachedToDataset(
21852185
this, organizationSaved.id, workspaceSaved.id, datasetParameterId)
21862186
eventPublisher.publishEvent(getAttachedRunnerToDataset)
21872187

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)