Skip to content

Commit

Permalink
fix: updated to single instance changes.
Browse files Browse the repository at this point in the history
feat: moved migration to setup of plugin
  • Loading branch information
itsdebs committed May 29, 2024
1 parent 99bed42 commit 88f28f1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ private const val RUDDER_SERVER_FILE_NAME_V1 = "RudderServerConfig"

internal class ReinstatePlugin : InfrastructurePlugin {
private var _analytics: Analytics? = null
private var sourceId: String? = null
private val _isReinstated = AtomicBoolean(false)
private val _isDbBeingMigrated = AtomicBoolean(false)
private fun setReinstated(isReinstated: Boolean){
synchronized(_isReinstated) {
_isReinstated.set(isReinstated)
Expand All @@ -69,37 +69,20 @@ internal class ReinstatePlugin : InfrastructurePlugin {
override fun setup(analytics: Analytics) {
_analytics = analytics
setReinstated(false)
}

override fun updateRudderServerConfig(config: RudderServerConfig) {
val sourceId = config.source?.sourceId ?: return
this.sourceId = sourceId
reinstate()
}

override fun updateConfiguration(configuration: Configuration) {
reinstate()
}

private fun reinstate() {
if (_isReinstated.get()) return
synchronized(this) {
val config = _analytics?.currentConfigurationAndroid ?: return
if (isV2DataAvailable()) {
setReinstated(true)
reinstateV2FromCache()
return
}
if (!config.shouldVerifySdk) {
setReinstated(true)
migrateV1DataIfAvailable()
if (_analytics?.currentConfigurationAndroid?.anonymousId == null) {
fillDefaults()
return
}
val sourceId = this.sourceId ?: return

migrateV1DataIfAvailable(config.application, sourceId, config)

setReinstated(true)
}
}

Expand Down Expand Up @@ -131,22 +114,20 @@ internal class ReinstatePlugin : InfrastructurePlugin {
!_analytics?.contextState?.value.isNullOrEmpty()
}

private fun migrateV1DataIfAvailable(
context: Context, sourceId: String, configurationAndroid: ConfigurationAndroid
) {
val isV1DataAvailable =
context.isV1SavedServerConfigContainsSourceId(RUDDER_SERVER_FILE_NAME_V1, sourceId)
if(!isV1DataAvailable) return
private fun migrateV1DataIfAvailable() {
// migrate user id/ anon id
_analytics?.setUserIdFromV1()
_analytics?.migrateAnonymousIdFromV1()
_analytics?.migrateOptOutFromV1()
_analytics?.migrateContextFromV1()
_analytics?.androidStorage?.migrateV1StorageToV2Sync()

_analytics?.migrateContextFromV1()
_analytics?.initializeSessionManagement(
_analytics?.androidStorage?.v1SessionId, _analytics?.androidStorage?.v1LastActiveTimestamp
)
if(_isDbBeingMigrated.compareAndSet(false, true)) {
_analytics?.androidStorage?.migrateV1StorageToV2 {
setReinstated(true)
}
}
}

private fun Analytics.setUserIdFromV1() {
Expand Down Expand Up @@ -180,7 +161,6 @@ internal class ReinstatePlugin : InfrastructurePlugin {

override fun shutdown() {
_analytics = null
this.sourceId = null
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ interface AndroidStorage : Storage {
fun setBuild(build: Int)
fun setVersionName(versionName: String)
fun migrateV1StorageToV2Sync()
fun migrateV1StorageToV2(callback: () -> Unit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,13 @@ class AndroidStorageImpl(
jsonAdapter?:return, logger)
}

override fun migrateV1StorageToV2(callback: () -> Unit) {
storageExecutor.execute {
migrateV1StorageToV2Sync()
callback()
}
}

override fun setBuild(build: Int) {
preferenceManager?.saveBuild(build)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ReinstatePluginTest {

// plugin.updateConfiguration(configurationAndroid)
busyWait(100)
Mockito.verify(androidStorage, times(1)).migrateV1StorageToV2Sync()
Mockito.verify(androidStorage, times(1)).migrateV1StorageToV2(any())


}
Expand Down

0 comments on commit 88f28f1

Please sign in to comment.