Skip to content

Commit 14ef5e7

Browse files
committed
[PROD-13929] Update Runner.parentId on parent deletion
1 parent 8a7148b commit 14ef5e7

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import java.util.*
5757
import kotlin.test.assertEquals
5858
import kotlin.test.assertNotEquals
5959
import kotlin.test.assertNotNull
60+
import kotlin.test.assertNull
6061
import kotlin.test.assertTrue
6162
import org.junit.jupiter.api.BeforeAll
6263
import org.junit.jupiter.api.BeforeEach
@@ -308,6 +309,58 @@ class RunnerServiceIntegrationTest : CsmRedisTestBase() {
308309
}
309310
}
310311

312+
@Test
313+
fun `update parentId on Runner delete`() {
314+
// Create a 3 level hierarchy: grandParent <- parent <- child
315+
val grandParentCreation =
316+
makeRunner(
317+
organizationSaved.id!!,
318+
workspaceSaved.id!!,
319+
solutionSaved.id!!,
320+
)
321+
val grandParentRunner =
322+
runnerApiService.createRunner(
323+
organizationSaved.id!!, workspaceSaved.id!!, grandParentCreation)
324+
val parentCreation =
325+
makeRunner(
326+
organizationSaved.id!!,
327+
workspaceSaved.id!!,
328+
solutionSaved.id!!,
329+
parentId = grandParentRunner.id)
330+
val parentRunner =
331+
runnerApiService.createRunner(organizationSaved.id!!, workspaceSaved.id!!, parentCreation)
332+
val childCreation =
333+
makeRunner(
334+
organizationSaved.id!!,
335+
workspaceSaved.id!!,
336+
solutionSaved.id!!,
337+
parentId = parentRunner.id)
338+
val childRunner =
339+
runnerApiService.createRunner(organizationSaved.id!!, workspaceSaved.id!!, childCreation)
340+
341+
// Initial parents check
342+
assertEquals(grandParentRunner.id, parentRunner.parentId)
343+
assertEquals(parentRunner.id, childRunner.parentId)
344+
345+
// Delete intermediate parent, child should refer to grandParent
346+
runnerApiService.deleteRunner(organizationSaved.id!!, workspaceSaved.id!!, parentRunner.id!!)
347+
var newChildParentId =
348+
runnerApiService
349+
.getRunner(organizationSaved.id!!, workspaceSaved.id!!, childRunner.id!!)
350+
.parentId
351+
assertEquals(grandParentRunner.id, newChildParentId)
352+
353+
// Delete root grandParent, child should clear its parent
354+
runnerApiService.deleteRunner(
355+
organizationSaved.id!!, workspaceSaved.id!!, grandParentRunner.id!!)
356+
newChildParentId =
357+
runnerApiService
358+
.getRunner(organizationSaved.id!!, workspaceSaved.id!!, childRunner.id!!)
359+
.parentId
360+
assertNull(newChildParentId)
361+
}
362+
363+
@Test
311364
fun `test RBAC RunnerSecurity as Platform Admin`() {
312365
every { getCurrentAuthenticatedRoles(any()) } returns listOf(ROLE_PLATFORM_ADMIN)
313366

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import com.cosmotech.workspace.service.getRbac
4444
import java.time.Instant
4545
import org.springframework.context.annotation.Scope
4646
import org.springframework.data.domain.PageRequest
47+
import org.springframework.data.domain.Pageable
4748
import org.springframework.stereotype.Component
4849

4950
@Component
@@ -93,6 +94,20 @@ class RunnerService(
9394
"Can't delete runner ${runner.id!!}: at least one run is still running")
9495
}
9596

97+
// Update parent references to delete runner to point to grand-parent
98+
var pageRequest: Pageable =
99+
PageRequest.ofSize(csmPlatformProperties.twincache.runner.defaultPageSize)
100+
do {
101+
val pagedRunners =
102+
runnerRepository.findByParentId(
103+
runner.organizationId!!, runner.workspaceId!!, runner.id!!, pageRequest)
104+
pagedRunners.stream().forEach {
105+
it.parentId = runner.parentId
106+
runnerRepository.save(it)
107+
}
108+
pageRequest = pagedRunners.nextPageable()
109+
} while (pagedRunners.hasNext())
110+
96111
// Notify the deletion
97112
val runnerDeleted =
98113
RunnerDeleted(this, runner.organizationId!!, runner.workspaceId!!, runner.id!!)

0 commit comments

Comments
 (0)