-
Notifications
You must be signed in to change notification settings - Fork 586
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
feat: allow disabling storage persistence #1539
Merged
B4nan
merged 7 commits into
master
from
feat/allow-disabling-writing-to-disk-in-memory-storage
Sep 15, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
28bad43
feat(MemoryStorage): allow disabling the write to disk functionality
vladfrangu 9b5760e
chore: add tests for the feature
vladfrangu 5c62c8d
chore: fix test
vladfrangu 4066523
chore: requested changes
vladfrangu 0b73e95
chore: forgot to update the test
vladfrangu dbfb58f
fix: tests and logic
vladfrangu 17f53df
chore: fix test again
vladfrangu 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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { access } from 'node:fs/promises'; | ||
import { setTimeout } from 'node:timers/promises'; | ||
|
||
export async function waitTillWrittenToDisk(path: string): Promise<void> { | ||
try { | ||
await access(path); | ||
} catch { | ||
await setTimeout(50); | ||
return waitTillWrittenToDisk(path); | ||
} | ||
} |
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,57 @@ | ||
import { readdir, rm } from 'node:fs/promises'; | ||
import { resolve } from 'node:path'; | ||
import { MemoryStorage } from '@crawlee/memory-storage'; | ||
import { waitTillWrittenToDisk } from './__shared__'; | ||
|
||
describe('persistStorage option', () => { | ||
const tmpLocation = resolve(__dirname, './tmp/no-writing-to-disk'); | ||
|
||
afterAll(async () => { | ||
await rm(tmpLocation, { force: true, recursive: true }); | ||
}); | ||
|
||
describe('when false and writeMetadata is also false', () => { | ||
const localDataDirectory = resolve(tmpLocation, './no-metadata'); | ||
const storage = new MemoryStorage({ | ||
localDataDirectory, | ||
persistStorage: false, | ||
}); | ||
|
||
test('creating a key-value pair in a key-value store should not write data to the disk', async () => { | ||
const keyValueStoreInfo = await storage.keyValueStores().getOrCreate(); | ||
|
||
const keyValueStore = storage.keyValueStore(keyValueStoreInfo.id); | ||
await keyValueStore.setRecord({ key: 'foo', value: 'test' }); | ||
|
||
// We check that reading the directory for the store throws an error, which means it wasn't created on disk | ||
await expect(() => readdir(localDataDirectory)).rejects.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('when false and writeMetadata is true', () => { | ||
const localDataDirectory = resolve(tmpLocation, './with-metadata'); | ||
const storage = new MemoryStorage({ | ||
localDataDirectory, | ||
persistStorage: false, | ||
writeMetadata: true, | ||
}); | ||
|
||
test('creating a key-value pair in a key-value store should not write data to the disk, but it should write the __metadata__ file', async () => { | ||
const keyValueStoreInfo = await storage.keyValueStores().getOrCreate(); | ||
|
||
const keyValueStore = storage.keyValueStore(keyValueStoreInfo.id); | ||
await keyValueStore.setRecord({ key: 'foo', value: 'test' }); | ||
|
||
const storePath = resolve(storage.keyValueStoresDirectory, `${keyValueStoreInfo.id}`); | ||
|
||
await waitTillWrittenToDisk(storePath); | ||
|
||
const directoryFiles = await readdir(storePath); | ||
|
||
expect(directoryFiles).toHaveLength(1); | ||
expect(directoryFiles).toEqual([ | ||
'__metadata__.json', | ||
]); | ||
}); | ||
}); | ||
}); |
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
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.
FYI the argument does not have to be callback (as long as its async), this should work the same:
At least that is how I use it, if there is a reason to prefer the callback approach, let me know :]