I have a very tiny utility macOS app that I built to migrate data from MongoDB to Couchbase. To do this, I use the Couchbase Lite Swift SDK. I create about 5 million documents locally (from some JSON files), then start a replicator to push them to Capella.
I have noticed odd behavior. If I minimize the app while sync is running or allow the window to become occluded (hidden behind other apps), after a few minutes I start to see errors about unwritable websockets. At first, there are only a few. But after a while, the console is mostly errors:
If I bring the app’s window back to the front, this instantly resolves and the websocket errors vanish:
Has anyone encountered something similar? It takes a few minutes for this to occur, so you have to be syncing a large amount of data.
Code
Here’s how I’m running the Replicator:
func startReplicator()
{
var collections: [Collection] = []
for collectionName: String in collectionNames {
let c: Collection = try! database.createCollection(name: collectionName, scope: scopeName)
collections.append(c)
}
var config = ReplicatorConfiguration(target: syncEndpoint)
config.continuous = true
config.replicatorType = .pushAndPull
config.addCollections(collections)
config.authenticator = BasicAuthenticator(username: syncEndpointUsername, password: syncEndpointPassword)
replicator = Replicator(config: config)
replicatorStatusToken = replicator!.addChangeListener({ change in
let status: Replicator.Status = change.status
switch status.activity
{
case .stopped:
logger.info("Replicator stopped")
case .offline:
logger.info("Replicator offline")
case .connecting:
logger.info("Replicator connecting...")
case .idle:
logger.info("Replicator is idle")
case .busy:
if status.progress.total != 0 {
let portion: Double = Double(status.progress.completed) / Double(status.progress.total)
logger.info("Replicator is syncing: \(portion)")
} else {
logger.info("Replicator is syncing...")
}
@unknown default:
logger.warning("Unhandled Replicator.Status.ActivityLevel case: \(status.activity.rawValue, privacy: .public). Setting ModelContext syncStatus to idle.")
}
if let error = change.status.error as? NSError {
logger.error("Sync replication error: \(error.localizedDescription, privacy: .public)")
}
})
replicator!.start(reset: false)
replicatorIsLaunched = true
}