-
Notifications
You must be signed in to change notification settings - Fork 0
Jrepp - Optimize Local Start #202
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
Open
jrepp
wants to merge
1
commit into
main
Choose a base branch
from
jrepp/optimize-local-start
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,10 +4,12 @@ package cmd | |||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||
| "os/exec" | ||||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||
| "sync" | ||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| pb "github.com/jrepp/prism-data-layer/pkg/plugin/gen/prism" | ||||||||||||||||||||||||||||
|
|
@@ -150,37 +152,53 @@ func startLocalStack() error { | |||||||||||||||||||||||||||
| return fmt.Errorf("patterns directory not found at %s", patternsDir) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Start components in order | ||||||||||||||||||||||||||||
| components := []struct { | ||||||||||||||||||||||||||||
| name string | ||||||||||||||||||||||||||||
| binary string | ||||||||||||||||||||||||||||
| args []string | ||||||||||||||||||||||||||||
| logFile string | ||||||||||||||||||||||||||||
| delay time.Duration | ||||||||||||||||||||||||||||
| }{ | ||||||||||||||||||||||||||||
| // Component definitions with health check endpoints | ||||||||||||||||||||||||||||
| type component struct { | ||||||||||||||||||||||||||||
| name string | ||||||||||||||||||||||||||||
| binary string | ||||||||||||||||||||||||||||
| args []string | ||||||||||||||||||||||||||||
| logFile string | ||||||||||||||||||||||||||||
| healthCheck string // HTTP or gRPC endpoint for health checks | ||||||||||||||||||||||||||||
| healthTimeout time.Duration | ||||||||||||||||||||||||||||
| isGRPC bool | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| components := []component{ | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| name: "prism-admin", | ||||||||||||||||||||||||||||
| binary: filepath.Join(absBinDir, "prism-admin"), | ||||||||||||||||||||||||||||
| args: []string{"serve", "--port=8981"}, | ||||||||||||||||||||||||||||
| logFile: filepath.Join(logsDir, "admin.log"), | ||||||||||||||||||||||||||||
| delay: 2 * time.Second, | ||||||||||||||||||||||||||||
| name: "prism-admin", | ||||||||||||||||||||||||||||
| binary: filepath.Join(absBinDir, "prism-admin"), | ||||||||||||||||||||||||||||
| args: []string{"serve", "--port=8981"}, | ||||||||||||||||||||||||||||
| logFile: filepath.Join(logsDir, "admin.log"), | ||||||||||||||||||||||||||||
| healthCheck: "localhost:8981", | ||||||||||||||||||||||||||||
| healthTimeout: 10 * time.Second, | ||||||||||||||||||||||||||||
| isGRPC: true, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| name: "pattern-launcher", | ||||||||||||||||||||||||||||
| binary: filepath.Join(absBinDir, "pattern-launcher"), | ||||||||||||||||||||||||||||
| args: []string{"--admin-endpoint=localhost:8981", "--launcher-id=launcher-01", "--grpc-port=7070", "--patterns-dir=" + patternsDir}, | ||||||||||||||||||||||||||||
| logFile: filepath.Join(logsDir, "launcher.log"), | ||||||||||||||||||||||||||||
| delay: 2 * time.Second, | ||||||||||||||||||||||||||||
| name: "pattern-launcher", | ||||||||||||||||||||||||||||
| binary: filepath.Join(absBinDir, "pattern-launcher"), | ||||||||||||||||||||||||||||
| args: []string{"--admin-endpoint=localhost:8981", "--launcher-id=launcher-01", "--grpc-port=7070", "--patterns-dir=" + patternsDir}, | ||||||||||||||||||||||||||||
| logFile: filepath.Join(logsDir, "launcher.log"), | ||||||||||||||||||||||||||||
| healthCheck: "http://localhost:9093/health", | ||||||||||||||||||||||||||||
| healthTimeout: 10 * time.Second, | ||||||||||||||||||||||||||||
| isGRPC: false, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| name: "keyvalue-runner", | ||||||||||||||||||||||||||||
| binary: filepath.Join(absBinDir, "keyvalue-runner"), | ||||||||||||||||||||||||||||
| args: []string{"--proxy-addr=localhost:9090"}, | ||||||||||||||||||||||||||||
| logFile: filepath.Join(logsDir, "keyvalue.log"), | ||||||||||||||||||||||||||||
| delay: 1 * time.Second, | ||||||||||||||||||||||||||||
| name: "keyvalue-runner", | ||||||||||||||||||||||||||||
| binary: filepath.Join(absBinDir, "keyvalue-runner"), | ||||||||||||||||||||||||||||
| args: []string{"--proxy-addr=localhost:9090"}, | ||||||||||||||||||||||||||||
| logFile: filepath.Join(logsDir, "keyvalue.log"), | ||||||||||||||||||||||||||||
| healthCheck: "", // No health check endpoint | ||||||||||||||||||||||||||||
| healthTimeout: 2 * time.Second, | ||||||||||||||||||||||||||||
| isGRPC: false, | ||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Start components and track errors | ||||||||||||||||||||||||||||
| var wg sync.WaitGroup | ||||||||||||||||||||||||||||
| startErrors := make(chan error, len(components)) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| startTime := time.Now() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| for _, comp := range components { | ||||||||||||||||||||||||||||
| fmt.Printf(" Starting %s...\n", comp.name) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -216,13 +234,42 @@ func startLocalStack() error { | |||||||||||||||||||||||||||
| fmt.Printf(" ⚠️ Warning: Could not save PID file: %v\n", err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Wait before starting next component | ||||||||||||||||||||||||||||
| if comp.delay > 0 { | ||||||||||||||||||||||||||||
| time.Sleep(comp.delay) | ||||||||||||||||||||||||||||
| // Wait for health check in parallel | ||||||||||||||||||||||||||||
| if comp.healthCheck != "" { | ||||||||||||||||||||||||||||
| wg.Add(1) | ||||||||||||||||||||||||||||
| go func(c component) { | ||||||||||||||||||||||||||||
| defer wg.Done() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| var err error | ||||||||||||||||||||||||||||
| if c.isGRPC { | ||||||||||||||||||||||||||||
| err = waitForGRPCHealth(c.healthCheck, c.healthTimeout) | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| err = waitForHTTPHealth(c.healthCheck, c.healthTimeout) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| startErrors <- fmt.Errorf("%s health check failed: %w", c.name, err) | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| fmt.Printf(" ✓ %s ready\n", c.name) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }(comp) | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| // Fallback to short delay for components without health checks | ||||||||||||||||||||||||||||
| time.Sleep(comp.healthTimeout) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| fmt.Printf("\n✅ Local Prism stack started successfully!\n\n") | ||||||||||||||||||||||||||||
| // Wait for all health checks to complete | ||||||||||||||||||||||||||||
| wg.Wait() | ||||||||||||||||||||||||||||
| close(startErrors) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Check for any startup errors | ||||||||||||||||||||||||||||
| if err := <-startErrors; err != nil { | ||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| elapsed := time.Since(startTime) | ||||||||||||||||||||||||||||
| fmt.Printf("\n✅ Local Prism stack started successfully in %s!\n\n", elapsed.Round(10*time.Millisecond)) | ||||||||||||||||||||||||||||
| fmt.Println("📊 Stack Overview:") | ||||||||||||||||||||||||||||
| fmt.Println(" • Admin Control Plane: localhost:8981") | ||||||||||||||||||||||||||||
| fmt.Println(" • Pattern Launcher: localhost:7070") | ||||||||||||||||||||||||||||
|
|
@@ -415,6 +462,59 @@ func isInBinariesDir(dir string) bool { | |||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // waitForHTTPHealth polls an HTTP health endpoint until it's ready or timeout | ||||||||||||||||||||||||||||
| func waitForHTTPHealth(url string, timeout time.Duration) error { | ||||||||||||||||||||||||||||
| ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||||||||||||||||||||||||||||
| defer cancel() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ticker := time.NewTicker(100 * time.Millisecond) | ||||||||||||||||||||||||||||
| defer ticker.Stop() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| client := &http.Client{Timeout: 1 * time.Second} | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| for { | ||||||||||||||||||||||||||||
| select { | ||||||||||||||||||||||||||||
| case <-ctx.Done(): | ||||||||||||||||||||||||||||
| return fmt.Errorf("timeout waiting for %s", url) | ||||||||||||||||||||||||||||
| case <-ticker.C: | ||||||||||||||||||||||||||||
| resp, err := client.Get(url) | ||||||||||||||||||||||||||||
| if err == nil && resp.StatusCode == http.StatusOK { | ||||||||||||||||||||||||||||
| resp.Body.Close() | ||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| if resp != nil { | ||||||||||||||||||||||||||||
| resp.Body.Close() | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+481
to
+487
|
||||||||||||||||||||||||||||
| if err == nil && resp.StatusCode == http.StatusOK { | |
| resp.Body.Close() | |
| return nil | |
| } | |
| if resp != nil { | |
| resp.Body.Close() | |
| } | |
| if resp != nil { | |
| defer resp.Body.Close() | |
| } | |
| if err == nil && resp.StatusCode == http.StatusOK { | |
| return nil | |
| } |
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.
Only the first error from the channel is checked. If multiple components fail their health checks, subsequent errors will be ignored. Consider draining all errors from the channel and returning a combined error or the first error encountered.