Skip to content

Commit 6ebbe81

Browse files
ILLDEV-456 Filter only finished events and not current event
1 parent d679a1b commit 6ebbe81

4 files changed

Lines changed: 38 additions & 28 deletions

File tree

broker/scheduler/db/repo.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type SchedRepo interface {
2222
GetScheduledTaskByIdForUpdate(ctx common.ExtendedContext, id string, owners []string) (ScheduledTask, error)
2323
HasActiveBatchActionEvents(ctx common.ExtendedContext, taskID string) (bool, error)
2424
DeleteBatchActionEvents(ctx common.ExtendedContext, taskID string) error
25-
DeleteOldBatchActionRunEvents(ctx common.ExtendedContext, taskID string, retention int32) error
25+
DeleteOldBatchActionRunEvents(ctx common.ExtendedContext, currentEventId string, taskID string, retention int32) error
2626
DeleteScheduledTask(ctx common.ExtendedContext, id string, owners []string) error
2727
GetScheduledTasks(ctx common.ExtendedContext, params GetScheduledTasksParams) ([]ScheduledTask, int64, error)
2828
}
@@ -128,10 +128,11 @@ func (r *PgSchedRepo) DeleteBatchActionEvents(ctx common.ExtendedContext, taskID
128128
return r.eventQueries.DeleteBatchActionEvents(ctx, r.GetConnOrTx(), taskID)
129129
}
130130

131-
func (r *PgSchedRepo) DeleteOldBatchActionRunEvents(ctx common.ExtendedContext, taskID string, retention int32) error {
131+
func (r *PgSchedRepo) DeleteOldBatchActionRunEvents(ctx common.ExtendedContext, currentEventId string, taskID string, retention int32) error {
132132
return r.eventQueries.DeleteOldBatchActionRunEvents(ctx, r.GetConnOrTx(), events.DeleteOldBatchActionRunEventsParams{
133-
TaskID: taskID,
134-
Retention: retention,
133+
CurrentEventID: currentEventId,
134+
TaskID: taskID,
135+
Retention: retention,
135136
})
136137
}
137138

broker/scheduler/service/batch_action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (s *BatchActionService) cleanupOldRuns(ctx common.ExtendedContext, event ev
8888
if taskID == "" {
8989
return
9090
}
91-
if err := s.schedRepo.DeleteOldBatchActionRunEvents(ctx, taskID, BATCH_ACTION_RUN_RETENTION); err != nil {
91+
if err := s.schedRepo.DeleteOldBatchActionRunEvents(ctx, event.ID, taskID, BATCH_ACTION_RUN_RETENTION); err != nil {
9292
ctx.Logger().Error("failed to cleanup old batch action runs", "taskId", taskID, "retention", BATCH_ACTION_RUN_RETENTION, "error", err)
9393
}
9494
}

broker/scheduler/service/batch_action_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ type mockBatchActionEventBus struct {
4242

4343
type mockBatchActionCleanupRepo struct {
4444
sched_db.PgSchedRepo
45-
called bool
46-
gotTaskID string
47-
gotRetention int32
48-
err error
45+
called bool
46+
gotCurrentEventId string
47+
gotTaskID string
48+
gotRetention int32
49+
err error
4950
}
5051

51-
func (m *mockBatchActionCleanupRepo) DeleteOldBatchActionRunEvents(_ common.ExtendedContext, taskID string, retention int32) error {
52+
func (m *mockBatchActionCleanupRepo) DeleteOldBatchActionRunEvents(_ common.ExtendedContext, currentEventId string, taskID string, retention int32) error {
5253
m.called = true
54+
m.gotCurrentEventId = currentEventId
5355
m.gotTaskID = taskID
5456
m.gotRetention = retention
5557
return m.err
@@ -165,6 +167,7 @@ func TestBatchAction_CleansOldRunsBeforeDispatch(t *testing.T) {
165167
assert.Equal(t, events.EventStatusSuccess, status)
166168
assert.NotNil(t, result)
167169
assert.True(t, cleanupRepo.called)
170+
assert.Equal(t, "batch-event-1", cleanupRepo.gotCurrentEventId)
168171
assert.Equal(t, "task-1", cleanupRepo.gotTaskID)
169172
assert.Equal(t, int32(5), cleanupRepo.gotRetention)
170173
assert.True(t, repo.listCalled)
@@ -175,6 +178,7 @@ func TestBatchAction_CleansOldRunsBeforeDispatch_WhenRetentionIsZero(t *testing.
175178
eventBus := &mockBatchActionEventBus{}
176179
cleanupRepo := &mockBatchActionCleanupRepo{}
177180
svc := NewBatchActionService(eventBus, repo, cleanupRepo, nil)
181+
prevValue := BATCH_ACTION_RUN_RETENTION
178182
BATCH_ACTION_RUN_RETENTION = 0
179183

180184
status, result := svc.batchAction(testCtx, requestAgingEvent("cql.allRecords=1", map[string]any{"interval": "24h"}))
@@ -183,7 +187,7 @@ func TestBatchAction_CleansOldRunsBeforeDispatch_WhenRetentionIsZero(t *testing.
183187
assert.NotNil(t, result)
184188
assert.False(t, cleanupRepo.called)
185189
assert.True(t, repo.listCalled)
186-
BATCH_ACTION_RUN_RETENTION = 5
190+
BATCH_ACTION_RUN_RETENTION = prevValue
187191
}
188192

189193
func TestBatchAction_CleanupErrorDoesNotBlockDispatch(t *testing.T) {

broker/sqlc/event_query.sql

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,28 @@ WHERE event_name IN ('invoke-batch-action', 'invoke-background-action')
115115
AND event_data -> 'batchActionData' ->> 'taskId' = sqlc.arg(task_id)::text;
116116

117117
-- name: DeleteOldBatchActionRunEvents :exec
118-
WITH retained_runs AS (
119-
SELECT id
120-
FROM event
121-
WHERE event_name = 'invoke-batch-action'
122-
AND event_data -> 'batchActionData' ->> 'taskId' = sqlc.arg(task_id)::text
123-
ORDER BY timestamp DESC, id DESC
124-
LIMIT sqlc.arg(retention)::int
125-
),
126-
old_runs AS (
127-
SELECT id
128-
FROM event
129-
WHERE event_name = 'invoke-batch-action'
130-
AND event_data -> 'batchActionData' ->> 'taskId' = sqlc.arg(task_id)::text
131-
AND id NOT IN (SELECT id FROM retained_runs)
132-
)
133-
DELETE FROM event
134-
WHERE id IN (SELECT id FROM old_runs);
118+
WITH retained_runs AS (
119+
SELECT id
120+
FROM event
121+
WHERE event_name = 'invoke-batch-action'
122+
AND event_status IN ('SUCCESS', 'ERROR', 'PROBLEM')
123+
AND id <> sqlc.arg(current_event_id)::text
124+
AND event_data -> 'batchActionData' ->> 'taskId' = sqlc.arg(task_id)::text
125+
ORDER BY timestamp DESC, id DESC
126+
LIMIT sqlc.arg(retention)::int
127+
),
128+
old_runs AS (
129+
SELECT id
130+
FROM event
131+
WHERE event_name = 'invoke-batch-action'
132+
AND event_status IN ('SUCCESS', 'ERROR', 'PROBLEM')
133+
AND id <> sqlc.arg(current_event_id)::text
134+
AND event_data -> 'batchActionData' ->> 'taskId' = sqlc.arg(task_id)::text
135+
AND id NOT IN (SELECT id FROM retained_runs)
136+
)
137+
DELETE FROM event
138+
WHERE id IN (SELECT id FROM old_runs) OR
139+
parent_id IN (SELECT id FROM old_runs);
135140

136141
-- name: UpdateEventLifecycle :one
137142
UPDATE event SET last_signal = $3, event_status = $2

0 commit comments

Comments
 (0)