Skip to content

Commit

Permalink
Add shutdown function to Analytics to free resources. (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
didiergarcia authored Jan 27, 2023
1 parent 736db66 commit b0c175b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ dependencies {
api project(':core')
api 'com.segment:sovran-kotlin:1.2.1'
api "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation 'androidx.lifecycle:lifecycle-process:2.4.0'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.4.0'

Expand Down
6 changes: 3 additions & 3 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ test {

dependencies {
// MAIN DEPS
api 'com.segment:sovran-kotlin:1.2.1'
api 'com.segment:sovran-kotlin:1.2.2'
api "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'

// TESTING
repositories { mavenCentral() }
testImplementation 'io.mockk:mockk:1.10.6'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4'

testImplementation platform("org.junit:junit-bom:5.7.2")
testImplementation "org.junit.jupiter:junit-jupiter"
Expand Down
29 changes: 21 additions & 8 deletions core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import com.segment.analytics.kotlin.core.platform.plugins.SegmentDestination
import com.segment.analytics.kotlin.core.platform.plugins.StartupQueue
import com.segment.analytics.kotlin.core.platform.plugins.logger.SegmentLog
import com.segment.analytics.kotlin.core.platform.plugins.logger.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -83,11 +79,11 @@ open class Analytics protected constructor(
object : CoroutineConfiguration {
override val store = Store()
override val analyticsScope = CoroutineScope(SupervisorJob())
override val analyticsDispatcher =
override val analyticsDispatcher : CloseableCoroutineDispatcher =
Executors.newCachedThreadPool().asCoroutineDispatcher()
override val networkIODispatcher =
override val networkIODispatcher : CloseableCoroutineDispatcher =
Executors.newSingleThreadExecutor().asCoroutineDispatcher()
override val fileIODispatcher =
override val fileIODispatcher : CloseableCoroutineDispatcher =
Executors.newFixedThreadPool(2).asCoroutineDispatcher()
})

Expand Down Expand Up @@ -535,6 +531,23 @@ open class Analytics protected constructor(
}
}

/**
* Shuts down the library by freeing up resources includes queues and Threads. This is a
* non-reversible operation. This instance of Analytics will be shutdown and no longer process
* events.
*
* Should only be called in containerized environments where you need to free resources like
* CoroutineDispatchers and ExecutorService instances so they allow the container to shutdown
* properly.
*/
fun shutdown() {
(analyticsDispatcher as CloseableCoroutineDispatcher).close()
(networkIODispatcher as CloseableCoroutineDispatcher).close()
(fileIODispatcher as CloseableCoroutineDispatcher).close()

store.shutdown();
}

/**
* Retrieve the userId registered by a previous `identify` call in a blocking way.
* Note: this method invokes `runBlocking` internal, it's not recommended to be used
Expand Down

0 comments on commit b0c175b

Please sign in to comment.