-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add initializing and failed states to provider init lifecycle #847
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f9cbbd7
feat(provider): add initializing/failed states to provider init lifec…
skevetter 6ccbe37
Merge remote-tracking branch 'origin/main' into ripe-panda
skevetter 716e2ed
style(provider): tidy comments and clarify lock synchronization
skevetter fdfe8f5
style(provider): drop remaining comments per review
skevetter 2ec469e
fix(provider): reset Initialized on retry, fix rune truncation, use r…
skevetter 054a573
style(provider): use ASCII ellipsis in truncated init error
skevetter 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,66 @@ | ||
| package provider | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/devsy-org/devsy/pkg/config" | ||
| "github.com/gofrs/flock" | ||
| ) | ||
|
|
||
| // InitState is the lifecycle state of a provider's initialization. | ||
| type InitState string | ||
|
|
||
| const ( | ||
| InitStateNotInitialized InitState = "not_initialized" | ||
| InitStateInitializing InitState = "initializing" | ||
| InitStateInitialized InitState = "initialized" | ||
| InitStateFailed InitState = "failed" | ||
| ) | ||
|
|
||
| // GetProviderInitLock returns the advisory lock held while a provider's init | ||
| // command runs, so ResolveInitState can tell a live "initializing" run apart | ||
| // from one abandoned by a crashed process. | ||
| func GetProviderInitLock(contextName, name string) (*flock.Flock, error) { | ||
| locksDir, err := GetLocksDir(contextName) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("get locks dir: %w", err) | ||
| } | ||
| // #nosec G301 -- mirrors existing lock dir permissions elsewhere in this package | ||
| if err := os.MkdirAll(locksDir, 0o755); err != nil { | ||
| return nil, fmt.Errorf("create locks dir: %w", err) | ||
| } | ||
|
|
||
| return flock.New(filepath.Join(locksDir, name+".provider-init.lock")), nil | ||
| } | ||
|
|
||
| // ResolveInitState derives the current init lifecycle state for a provider | ||
| // from its persisted config and the live state of its init lock. | ||
| func ResolveInitState(contextName, name string, state *config.ProviderConfig) (InitState, error) { | ||
| if state != nil && state.Initialized { | ||
| return InitStateInitialized, nil | ||
| } | ||
|
|
||
| lock, err := GetProviderInitLock(contextName, name) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| locked, err := lock.TryRLock() | ||
| if err != nil { | ||
| return "", fmt.Errorf("check init lock: %w", err) | ||
| } | ||
| if !locked { | ||
| return InitStateInitializing, nil | ||
| } | ||
| defer func() { _ = lock.Unlock() }() | ||
|
|
||
| if state != nil && state.InitAttempted { | ||
| // The lock is free but a prior attempt never reached Initialized: | ||
| // it either failed cleanly or was abandoned by a crashed process. | ||
| return InitStateFailed, nil | ||
| } | ||
|
|
||
| return InitStateNotInitialized, nil | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or 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,105 @@ | ||
| package provider | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/devsy-org/devsy/pkg/config" | ||
| ) | ||
|
|
||
| func useTempDevsyHome(t *testing.T) { | ||
| t.Helper() | ||
| t.Setenv(config.EnvHome, t.TempDir()) | ||
| config.SetPathManager(config.NewPathManager()) | ||
| } | ||
|
|
||
| func TestResolveInitState_NeverAttempted(t *testing.T) { | ||
| useTempDevsyHome(t) | ||
|
|
||
| got, err := ResolveInitState("default", "docker", &config.ProviderConfig{}) | ||
| if err != nil { | ||
| t.Fatalf("ResolveInitState: %v", err) | ||
| } | ||
| if got != InitStateNotInitialized { | ||
| t.Fatalf("got %q, want %q", got, InitStateNotInitialized) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveInitState_NilState(t *testing.T) { | ||
| useTempDevsyHome(t) | ||
|
|
||
| got, err := ResolveInitState("default", "docker", nil) | ||
| if err != nil { | ||
| t.Fatalf("ResolveInitState: %v", err) | ||
| } | ||
| if got != InitStateNotInitialized { | ||
| t.Fatalf("got %q, want %q", got, InitStateNotInitialized) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveInitState_Initialized(t *testing.T) { | ||
| useTempDevsyHome(t) | ||
|
|
||
| got, err := ResolveInitState("default", "docker", &config.ProviderConfig{Initialized: true}) | ||
| if err != nil { | ||
| t.Fatalf("ResolveInitState: %v", err) | ||
| } | ||
| if got != InitStateInitialized { | ||
| t.Fatalf("got %q, want %q", got, InitStateInitialized) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveInitState_LiveLockHeldMeansInitializing(t *testing.T) { | ||
| useTempDevsyHome(t) | ||
|
|
||
| lock, err := GetProviderInitLock("default", "docker") | ||
| if err != nil { | ||
| t.Fatalf("GetProviderInitLock: %v", err) | ||
| } | ||
| locked, err := lock.TryLock() | ||
| if err != nil || !locked { | ||
| t.Fatalf("TryLock: locked=%v err=%v", locked, err) | ||
| } | ||
| defer func() { _ = lock.Unlock() }() | ||
|
|
||
| got, err := ResolveInitState("default", "docker", &config.ProviderConfig{InitAttempted: true}) | ||
| if err != nil { | ||
| t.Fatalf("ResolveInitState: %v", err) | ||
| } | ||
| if got != InitStateInitializing { | ||
| t.Fatalf("got %q, want %q", got, InitStateInitializing) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveInitState_InitializedTakesPrecedenceOverInitError(t *testing.T) { | ||
| useTempDevsyHome(t) | ||
|
|
||
| // initProvider must reset Initialized to false before a retry runs, so | ||
| // this combination shouldn't occur on disk in practice. Locks in the | ||
| // intended precedence if it ever does: a stale Initialized=true still | ||
| // reads as initialized, since InitError alone is not a failure signal. | ||
| got, err := ResolveInitState("default", "docker", &config.ProviderConfig{ | ||
| Initialized: true, | ||
| InitError: "boom", | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("ResolveInitState: %v", err) | ||
| } | ||
| if got != InitStateInitialized { | ||
| t.Fatalf("got %q, want %q", got, InitStateInitialized) | ||
| } | ||
| } | ||
|
|
||
| func TestResolveInitState_FreeLockWithAttemptMeansFailed(t *testing.T) { | ||
| useTempDevsyHome(t) | ||
|
|
||
| // Simulate a crash mid-init: InitAttempted was persisted before the | ||
| // process died, but nothing holds the lock anymore since the OS released | ||
| // it when the process exited. | ||
| got, err := ResolveInitState("default", "docker", &config.ProviderConfig{InitAttempted: true}) | ||
| if err != nil { | ||
| t.Fatalf("ResolveInitState: %v", err) | ||
| } | ||
| if got != InitStateFailed { | ||
| t.Fatalf("got %q, want %q", got, InitStateFailed) | ||
| } | ||
| } |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.