Update git path.
[lightning.git] / arvados.go
index ca967824b51a98f204ff482013e7a1c539bc0930..8fd556e8d7829acb918e8808d446f0e63c3dfb3a 100644 (file)
@@ -119,6 +119,7 @@ reconnect:
                wsURL := cluster.Services.Websocket.ExternalURL
                wsURL.Scheme = strings.Replace(wsURL.Scheme, "http", "ws", 1)
                wsURL.Path = "/websocket"
+               wsURLNoToken := wsURL.String()
                wsURL.RawQuery = url.Values{"api_token": []string{client.AuthToken}}.Encode()
                conn, err := websocket.Dial(wsURL.String(), "", cluster.Services.Controller.ExternalURL.String())
                if err != nil {
@@ -126,20 +127,28 @@ reconnect:
                        time.Sleep(5 * time.Second)
                        continue reconnect
                }
+               log.Printf("connected to websocket at %s", wsURLNoToken)
+
                client.mtx.Lock()
                client.wsconn = conn
-               client.mtx.Unlock()
-
-               w := json.NewEncoder(conn)
+               resubscribe := make([]string, 0, len(client.notifying))
                for uuid := range client.notifying {
-                       w.Encode(map[string]interface{}{
-                               "method": "subscribe",
-                               "filters": [][]interface{}{
-                                       {"object_uuid", "=", uuid},
-                                       {"event_type", "in", []string{"stderr", "crunch-run", "update"}},
-                               },
-                       })
+                       resubscribe = append(resubscribe, uuid)
                }
+               client.mtx.Unlock()
+
+               go func() {
+                       w := json.NewEncoder(conn)
+                       for _, uuid := range resubscribe {
+                               w.Encode(map[string]interface{}{
+                                       "method": "subscribe",
+                                       "filters": [][]interface{}{
+                                               {"object_uuid", "=", uuid},
+                                               {"event_type", "in", []string{"stderr", "crunch-run", "update"}},
+                                       },
+                               })
+                       }
+               }()
 
                r := json.NewDecoder(conn)
                for {
@@ -157,9 +166,11 @@ reconnect:
                                        go conn.Close()
                                        continue reconnect
                                }
+                               client.mtx.Lock()
                                for ch := range client.notifying[msg.ObjectUUID] {
                                        ch <- msg
                                }
+                               client.mtx.Unlock()
                        }
                }
        }