Skip to content

Commit 9ea28c7

Browse files
committed
feat: add CreateLineForCleanup method and improve 503 error handling for stale device cleanup
1 parent 6277b64 commit 9ea28c7

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

calling/callingclient.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,25 @@ func (cc *CallingClient) CreateLine() (*Line, error) {
620620
return line, nil
621621
}
622622

623+
// CreateLineForCleanup creates a Line with Mobius URLs populated but does NOT
624+
// register it. This is useful when you need to list/delete stale devices but
625+
// can't register due to 503 errors.
626+
func (cc *CallingClient) CreateLineForCleanup() *Line {
627+
cc.mu.RLock()
628+
primary := cc.primaryMobiusURLs
629+
backup := cc.backupMobiusURLs
630+
deviceURI := cc.clientDeviceURI
631+
userID := cc.userID
632+
cc.mu.RUnlock()
633+
634+
return NewLine(cc.core, cc.config, &LineConfig{
635+
PrimaryMobiusURLs: primary,
636+
BackupMobiusURLs: backup,
637+
ClientDeviceURI: deviceURI,
638+
UserID: userID,
639+
})
640+
}
641+
623642
// GetLines returns all registered lines
624643
func (cc *CallingClient) GetLines() map[string]*Line {
625644
cc.mu.RLock()

calling/line.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,28 @@ func (l *Line) listDevicesFromURL(mobiusURL string) ([]MobiusDevice, error) {
322322
return []MobiusDevice{}, nil
323323
}
324324

325+
// 503 means stale registrations blocking new ones — try to parse device info
326+
if resp.StatusCode == http.StatusServiceUnavailable {
327+
log.Printf("ListDevices: got 503 (stale registrations), response: %s", string(body))
328+
var errResp struct {
329+
Devices []MobiusDevice `json:"devices"`
330+
}
331+
if json.Unmarshal(body, &errResp) == nil {
332+
var validDevices []MobiusDevice
333+
for _, d := range errResp.Devices {
334+
if d.DeviceID != "" {
335+
validDevices = append(validDevices, d)
336+
}
337+
}
338+
if len(validDevices) > 0 {
339+
return validDevices, nil
340+
}
341+
}
342+
// Mobius returned 503 with null devices — no IDs to delete.
343+
// Stale registrations will auto-expire in ~3-5 minutes.
344+
return nil, fmt.Errorf("503: stale registrations exist but Mobius returned no device IDs; wait a few minutes for auto-expiry")
345+
}
346+
325347
return nil, fmt.Errorf("unexpected status %d: %s", resp.StatusCode, string(body))
326348
}
327349

@@ -396,10 +418,6 @@ func (l *Line) onRegistered() {
396418
// Deregister deregisters this line from the Mobius server
397419
func (l *Line) Deregister() error {
398420
l.mu.RLock()
399-
if l.status != RegistrationStatusActive {
400-
l.mu.RUnlock()
401-
return nil
402-
}
403421
mobiusURL := l.activeMobiusURL
404422
deviceID := l.MobiusDeviceID
405423
l.mu.RUnlock()

0 commit comments

Comments
 (0)