-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(core): Fix pruning of non-finished executions #7333
Merged
tomi
merged 4 commits into
master
from
ado-896-waiting-execution-data-should-not-be-pruned
Oct 4, 2023
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
209 changes: 209 additions & 0 deletions
209
packages/cli/test/integration/repositories/execution.repository.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import config from '@/config'; | ||
import * as Db from '@/Db'; | ||
|
||
import * as testDb from '../shared/testDb'; | ||
import type { ExecutionStatus } from 'n8n-workflow'; | ||
import { LoggerProxy } from 'n8n-workflow'; | ||
import { getLogger } from '@/Logger'; | ||
import type { ExecutionRepository } from '../../../src/databases/repositories'; | ||
import type { ExecutionEntity } from '../../../src/databases/entities/ExecutionEntity'; | ||
|
||
describe('ExecutionRepository.prune()', () => { | ||
const now = new Date(); | ||
const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000); | ||
tomi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let executionRepository: ExecutionRepository; | ||
let workflow: Awaited<ReturnType<typeof testDb.createWorkflow>>; | ||
|
||
beforeAll(async () => { | ||
LoggerProxy.init(getLogger()); | ||
await testDb.init(); | ||
|
||
const { Execution } = Db.collections; | ||
|
||
executionRepository = Execution; | ||
workflow = await testDb.createWorkflow(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await testDb.truncate(['Execution']); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testDb.terminate(); | ||
}); | ||
|
||
async function findAllExecutions() { | ||
return Db.collections.Execution.find({ | ||
order: { id: 'asc' }, | ||
withDeleted: true, | ||
}); | ||
} | ||
|
||
describe('when EXECUTIONS_DATA_PRUNE_MAX_COUNT is set', () => { | ||
beforeAll(() => { | ||
config.set('executions.pruneDataMaxCount', 1); | ||
config.set('executions.pruneDataMaxAge', 336); | ||
}); | ||
tomi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
test('should mark as deleted based on EXECUTIONS_DATA_PRUNE_MAX_COUNT', async () => { | ||
const executions = [ | ||
await testDb.createSuccessfulExecution(workflow), | ||
await testDb.createSuccessfulExecution(workflow), | ||
await testDb.createSuccessfulExecution(workflow), | ||
]; | ||
|
||
await executionRepository.prune(); | ||
|
||
const result = await findAllExecutions(); | ||
expect(result).toEqual([ | ||
expect.objectContaining({ id: executions[0].id, deletedAt: expect.any(Date) }), | ||
expect.objectContaining({ id: executions[1].id, deletedAt: expect.any(Date) }), | ||
expect.objectContaining({ id: executions[2].id, deletedAt: null }), | ||
]); | ||
}); | ||
|
||
test('should not re-mark already marked executions', async () => { | ||
tomi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const executions = [ | ||
await testDb.createExecution( | ||
{ status: 'success', finished: true, startedAt: now, stoppedAt: now, deletedAt: now }, | ||
workflow, | ||
), | ||
await testDb.createSuccessfulExecution(workflow), | ||
]; | ||
|
||
await executionRepository.prune(); | ||
|
||
const result = await findAllExecutions(); | ||
expect(result).toEqual([ | ||
expect.objectContaining({ id: executions[0].id, deletedAt: now }), | ||
expect.objectContaining({ id: executions[1].id, deletedAt: null }), | ||
]); | ||
}); | ||
|
||
test.each<[ExecutionStatus, Partial<ExecutionEntity>]>([ | ||
['canceled', { startedAt: now, stoppedAt: now }], | ||
['crashed', { startedAt: now, stoppedAt: now }], | ||
['failed', { startedAt: now, stoppedAt: now }], | ||
['success', { finished: true, startedAt: now, stoppedAt: now }], | ||
])('should prune %s executions', async (status, attributes) => { | ||
const executions = [ | ||
await testDb.createExecution({ status, ...attributes }, workflow), | ||
await testDb.createSuccessfulExecution(workflow), | ||
]; | ||
|
||
await executionRepository.prune(); | ||
|
||
const result = await findAllExecutions(); | ||
expect(result).toEqual([ | ||
expect.objectContaining({ id: executions[0].id, deletedAt: expect.any(Date) }), | ||
expect.objectContaining({ id: executions[1].id, deletedAt: null }), | ||
]); | ||
}); | ||
|
||
test.each<[ExecutionStatus, Partial<ExecutionEntity>]>([ | ||
['new', {}], | ||
['running', { startedAt: now }], | ||
['waiting', { startedAt: now, stoppedAt: now, waitTill: now }], | ||
])('should not prune %s executions', async (status, attributes) => { | ||
const executions = [ | ||
await testDb.createExecution({ status, ...attributes }, workflow), | ||
await testDb.createSuccessfulExecution(workflow), | ||
]; | ||
|
||
await executionRepository.prune(); | ||
|
||
const result = await findAllExecutions(); | ||
expect(result).toEqual([ | ||
expect.objectContaining({ id: executions[0].id, deletedAt: null }), | ||
expect.objectContaining({ id: executions[1].id, deletedAt: null }), | ||
]); | ||
}); | ||
}); | ||
|
||
describe('when EXECUTIONS_DATA_MAX_AGE is set', () => { | ||
beforeAll(() => { | ||
config.set('executions.pruneDataMaxAge', 1); // 1h | ||
config.set('executions.pruneDataMaxCount', 0); | ||
}); | ||
|
||
test('should mark as deleted based on EXECUTIONS_DATA_MAX_AGE', async () => { | ||
const executions = [ | ||
await testDb.createExecution( | ||
{ finished: true, startedAt: yesterday, stoppedAt: yesterday, status: 'success' }, | ||
workflow, | ||
), | ||
await testDb.createExecution( | ||
{ finished: true, startedAt: now, stoppedAt: now, status: 'success' }, | ||
workflow, | ||
), | ||
]; | ||
|
||
await executionRepository.prune(); | ||
|
||
const result = await findAllExecutions(); | ||
expect(result).toEqual([ | ||
expect.objectContaining({ id: executions[0].id, deletedAt: expect.any(Date) }), | ||
expect.objectContaining({ id: executions[1].id, deletedAt: null }), | ||
]); | ||
}); | ||
|
||
test('should not re-mark already marked executions', async () => { | ||
const executions = [ | ||
await testDb.createExecution( | ||
{ | ||
status: 'success', | ||
finished: true, | ||
startedAt: yesterday, | ||
stoppedAt: yesterday, | ||
deletedAt: yesterday, | ||
}, | ||
workflow, | ||
), | ||
await testDb.createSuccessfulExecution(workflow), | ||
]; | ||
|
||
await executionRepository.prune(); | ||
|
||
const result = await findAllExecutions(); | ||
expect(result).toEqual([ | ||
expect.objectContaining({ id: executions[0].id, deletedAt: yesterday }), | ||
expect.objectContaining({ id: executions[1].id, deletedAt: null }), | ||
]); | ||
}); | ||
|
||
test.each<[ExecutionStatus, Partial<ExecutionEntity>]>([ | ||
['canceled', { startedAt: yesterday, stoppedAt: yesterday }], | ||
['crashed', { startedAt: yesterday, stoppedAt: yesterday }], | ||
['failed', { startedAt: yesterday, stoppedAt: yesterday }], | ||
['success', { finished: true, startedAt: yesterday, stoppedAt: yesterday }], | ||
])('should prune %s executions', async (status, attributes) => { | ||
const execution = await testDb.createExecution({ status, ...attributes }, workflow); | ||
|
||
await executionRepository.prune(); | ||
|
||
const result = await findAllExecutions(); | ||
expect(result).toEqual([ | ||
expect.objectContaining({ id: execution.id, deletedAt: expect.any(Date) }), | ||
]); | ||
}); | ||
|
||
test.each<[ExecutionStatus, Partial<ExecutionEntity>]>([ | ||
['new', {}], | ||
['running', { startedAt: yesterday }], | ||
['waiting', { startedAt: yesterday, stoppedAt: yesterday, waitTill: yesterday }], | ||
])('should not prune %s executions', async (status, attributes) => { | ||
const executions = [ | ||
await testDb.createExecution({ status, ...attributes }, workflow), | ||
await testDb.createSuccessfulExecution(workflow), | ||
]; | ||
|
||
await executionRepository.prune(); | ||
|
||
const result = await findAllExecutions(); | ||
expect(result).toEqual([ | ||
expect.objectContaining({ id: executions[0].id, deletedAt: null }), | ||
expect.objectContaining({ id: executions[1].id, deletedAt: null }), | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed we are using
hardDeletion
rate even for the soft-deletes. Is this intentional or should it be changed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, if you can please update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Btw, is it intentional that hard-deletion happens every 15min but soft-deletion only every 1h?