Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

feat: auto-initialize Chrome on instance startup (Phase 5)

Mariobda235bd6ce9
diff --git a/internal/orchestrator/orchestrator.go b/internal/orchestrator/orchestrator.go
index 7bafd421..dccddb7c 100644
--- a/internal/orchestrator/orchestrator.go
+++ b/internal/orchestrator/orchestrator.go
@@ -195,6 +195,9 @@ func (o *Orchestrator) Launch(name, port string, headless bool) (*bridge.Instanc
go o.monitor(inst)
+ // Initialize Chrome in the background after instance starts
+ go o.initializeInstanceChrome(inst)
+
return &inst.Instance, nil
}
@@ -287,6 +290,85 @@ func (o *Orchestrator) StopProfile(name string) error {
return nil
}
+func (o *Orchestrator) initializeInstanceChrome(inst *InstanceInternal) {
+ // Wait for instance to be running before initializing Chrome
+ maxWait := 60 * time.Second
+ pollInterval := 100 * time.Millisecond
+ started := time.Now()
+
+ for time.Since(started) < maxWait {
+ o.mu.RLock()
+ status := inst.Status
+ url := inst.URL
+ o.mu.RUnlock()
+
+ if status == "running" && url != "" {
+ break
+ }
+
+ if status == "error" || status == "stopped" {
+ slog.Debug("instance not running, skipping chrome init", "id", inst.ID, "status", status)
+ return
+ }
+
+ time.Sleep(pollInterval)
+ }
+
+ o.mu.RLock()
+ status := inst.Status
+ url := inst.URL
+ id := inst.ID
+ o.mu.RUnlock()
+
+ if status != "running" {
+ slog.Warn("instance did not reach running state before chrome init timeout", "id", id, "status", status)
+ o.mu.Lock()
+ inst.Status = "error"
+ inst.Error = "chrome initialization timeout - instance did not become running"
+ o.mu.Unlock()
+ return
+ }
+
+ // Call /ensure-chrome on the instance to initialize Chrome
+ ensureChromeURL := url + "/ensure-chrome"
+ req, err := http.NewRequest(http.MethodPost, ensureChromeURL, nil)
+ if err != nil {
+ slog.Error("failed to create chrome init request", "id", id, "err", err)
+ o.mu.Lock()
+ inst.Status = "error"
+ inst.Error = fmt.Sprintf("chrome initialization failed: %v", err)
+ o.mu.Unlock()
+ return
+ }
+
+ if o.childAuthToken != "" {
+ req.Header.Set("Authorization", "Bearer "+o.childAuthToken)
+ }
+
+ resp, err := o.client.Do(req)
+ if err != nil {
+ slog.Error("chrome initialization request failed", "id", id, "err", err)
+ o.mu.Lock()
+ inst.Status = "error"
+ inst.Error = fmt.Sprintf("chrome initialization failed: %v", err)
+ o.mu.Unlock()
+ return
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(resp.Body)
+ slog.Error("chrome initialization returned error", "id", id, "status", resp.StatusCode, "body", string(body))
+ o.mu.Lock()
+ inst.Status = "error"
+ inst.Error = fmt.Sprintf("chrome initialization failed with status %d", resp.StatusCode)
+ o.mu.Unlock()
+ return
+ }
+
+ slog.Info("chrome initialized successfully", "id", id)
+}
+
func (o *Orchestrator) markStopped(id string) {
o.mu.Lock()
defer o.mu.Unlock()