feat(store): clean up orphaned workspaces on startup
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
387d1463fb
commit
0accc6e85e
3 changed files with 26 additions and 0 deletions
1
store.go
1
store.go
|
|
@ -106,6 +106,7 @@ func New(databasePath string, options ...StoreOption) (*Store, error) {
|
|||
}
|
||||
}
|
||||
storeInstance.startBackgroundPurge(purgeContext)
|
||||
storeInstance.cleanUpOrphanedWorkspaces(defaultWorkspaceStateDirectory)
|
||||
return storeInstance, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -133,6 +133,12 @@ func (storeInstance *Store) RecoverOrphans(stateDirectory string) []*Workspace {
|
|||
return workspaces
|
||||
}
|
||||
|
||||
func (storeInstance *Store) cleanUpOrphanedWorkspaces(stateDirectory string) {
|
||||
for _, orphanWorkspace := range storeInstance.RecoverOrphans(stateDirectory) {
|
||||
orphanWorkspace.Discard()
|
||||
}
|
||||
}
|
||||
|
||||
// Put appends one entry to the workspace buffer.
|
||||
// Usage example: `err := workspace.Put("like", map[string]any{"user": "@alice", "post": "video_123"})`
|
||||
func (workspace *Workspace) Put(kind string, data map[string]any) error {
|
||||
|
|
|
|||
|
|
@ -182,3 +182,22 @@ func TestWorkspace_RecoverOrphans_Good(t *testing.T) {
|
|||
orphans[0].Discard()
|
||||
assert.False(t, testFilesystem().Exists(workspaceFilePath(stateDirectory, "orphan-session")))
|
||||
}
|
||||
|
||||
func TestWorkspace_New_Good_CleansUpOrphanedWorkspaces(t *testing.T) {
|
||||
stateDirectory := useWorkspaceStateDirectory(t)
|
||||
requireCoreOK(t, testFilesystem().EnsureDir(stateDirectory))
|
||||
|
||||
orphanDatabasePath := workspaceFilePath(stateDirectory, "orphan-session")
|
||||
orphanDatabase, err := openWorkspaceDatabase(orphanDatabasePath)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, orphanDatabase.Close())
|
||||
assert.True(t, testFilesystem().Exists(orphanDatabasePath))
|
||||
|
||||
storeInstance, err := New(":memory:")
|
||||
require.NoError(t, err)
|
||||
defer storeInstance.Close()
|
||||
|
||||
assert.False(t, testFilesystem().Exists(orphanDatabasePath))
|
||||
assert.False(t, testFilesystem().Exists(orphanDatabasePath+"-wal"))
|
||||
assert.False(t, testFilesystem().Exists(orphanDatabasePath+"-shm"))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue