Skip to content

Commit b0c175b

Browse files
authored
Add shutdown function to Analytics to free resources. (#130)
1 parent 736db66 commit b0c175b

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

android/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ dependencies {
5454
api project(':core')
5555
api 'com.segment:sovran-kotlin:1.2.1'
5656
api "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2"
57-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
58-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
57+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
58+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
5959
implementation 'androidx.lifecycle:lifecycle-process:2.4.0'
6060
implementation 'androidx.lifecycle:lifecycle-common-java8:2.4.0'
6161

core/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ test {
1515

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

2222
// TESTING
2323
repositories { mavenCentral() }
2424
testImplementation 'io.mockk:mockk:1.10.6'
25-
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0'
25+
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4'
2626

2727
testImplementation platform("org.junit:junit-bom:5.7.2")
2828
testImplementation "org.junit.jupiter:junit-jupiter"

core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt

+21-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import com.segment.analytics.kotlin.core.platform.plugins.SegmentDestination
99
import com.segment.analytics.kotlin.core.platform.plugins.StartupQueue
1010
import com.segment.analytics.kotlin.core.platform.plugins.logger.SegmentLog
1111
import com.segment.analytics.kotlin.core.platform.plugins.logger.log
12-
import kotlinx.coroutines.CoroutineScope
13-
import kotlinx.coroutines.SupervisorJob
14-
import kotlinx.coroutines.asCoroutineDispatcher
15-
import kotlinx.coroutines.launch
16-
import kotlinx.coroutines.runBlocking
12+
import kotlinx.coroutines.*
1713
import kotlinx.serialization.DeserializationStrategy
1814
import kotlinx.serialization.SerializationStrategy
1915
import kotlinx.serialization.json.Json
@@ -83,11 +79,11 @@ open class Analytics protected constructor(
8379
object : CoroutineConfiguration {
8480
override val store = Store()
8581
override val analyticsScope = CoroutineScope(SupervisorJob())
86-
override val analyticsDispatcher =
82+
override val analyticsDispatcher : CloseableCoroutineDispatcher =
8783
Executors.newCachedThreadPool().asCoroutineDispatcher()
88-
override val networkIODispatcher =
84+
override val networkIODispatcher : CloseableCoroutineDispatcher =
8985
Executors.newSingleThreadExecutor().asCoroutineDispatcher()
90-
override val fileIODispatcher =
86+
override val fileIODispatcher : CloseableCoroutineDispatcher =
9187
Executors.newFixedThreadPool(2).asCoroutineDispatcher()
9288
})
9389

@@ -535,6 +531,23 @@ open class Analytics protected constructor(
535531
}
536532
}
537533

534+
/**
535+
* Shuts down the library by freeing up resources includes queues and Threads. This is a
536+
* non-reversible operation. This instance of Analytics will be shutdown and no longer process
537+
* events.
538+
*
539+
* Should only be called in containerized environments where you need to free resources like
540+
* CoroutineDispatchers and ExecutorService instances so they allow the container to shutdown
541+
* properly.
542+
*/
543+
fun shutdown() {
544+
(analyticsDispatcher as CloseableCoroutineDispatcher).close()
545+
(networkIODispatcher as CloseableCoroutineDispatcher).close()
546+
(fileIODispatcher as CloseableCoroutineDispatcher).close()
547+
548+
store.shutdown();
549+
}
550+
538551
/**
539552
* Retrieve the userId registered by a previous `identify` call in a blocking way.
540553
* Note: this method invokes `runBlocking` internal, it's not recommended to be used

0 commit comments

Comments
 (0)