Skip to content

Improve Resource Cleanup #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,4 @@ dependencies {
// Add Roboelectric dependencies.
testImplementation 'org.robolectric:robolectric:4.7.3'
testImplementation 'androidx.test:core:1.5.0'
}

apply from: rootProject.file('gradle/artifacts-android.gradle')
apply from: rootProject.file('gradle/mvn-publish.gradle')
apply from: rootProject.file('gradle/codecov.gradle')

tasks.named("signReleasePublication") {
dependsOn("bundleReleaseAar")
}
4 changes: 0 additions & 4 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,3 @@ dependencies {
apply from: rootProject.file('gradle/artifacts-core.gradle')
apply from: rootProject.file('gradle/mvn-publish.gradle')
apply from: rootProject.file('gradle/codecov.gradle')

tasks.named("signReleasePublication") {
dependsOn("jar")
}
32 changes: 29 additions & 3 deletions core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.segment.analytics.kotlin.core.platform.plugins.StartupQueue
import com.segment.analytics.kotlin.core.platform.plugins.UserInfoPlugin
import com.segment.analytics.kotlin.core.platform.plugins.logger.*
import com.segment.analytics.kotlin.core.utilities.JsonAnySerializer
import com.segment.analytics.kotlin.core.utilities.StorageImpl
import com.segment.analytics.kotlin.core.utilities.FileEventStream
import kotlinx.coroutines.*
import kotlinx.serialization.*
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -598,14 +600,38 @@ open class Analytics protected constructor(
* 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.
*
* @param waitForTasks if true, waits for all analyticsScope coroutines to complete before shutdown
*/
@JvmOverloads
@OptIn(ExperimentalCoroutinesApi::class)
fun shutdown() {
fun shutdown(waitForTasks: Boolean = false) {
timeline.applyClosure {
if (it is com.segment.analytics.kotlin.core.platform.EventPipeline) {
it.stop()
}
}

val job = analyticsScope.coroutineContext[Job]
job?.cancel()
if (waitForTasks) {
runBlocking {
job?.join()
}
}

(analyticsDispatcher as CloseableCoroutineDispatcher).close()
(networkIODispatcher as CloseableCoroutineDispatcher).close()
(fileIODispatcher as CloseableCoroutineDispatcher).close()

store.shutdown();
store.shutdown()

if (storage is StorageImpl) {
val s = storage as StorageImpl
if (s.eventStream is FileEventStream) {
(s.eventStream as FileEventStream).close()
}
}
}

/**
Expand Down Expand Up @@ -773,4 +799,4 @@ internal fun isAndroid(): Boolean {
} catch (ignored: ClassNotFoundException) {
false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ open class EventPipeline(

writeChannel = Channel(UNLIMITED)
uploadChannel = Channel(UNLIMITED)

registerShutdownHook()
}

fun put(event: BaseEvent) {
Expand Down Expand Up @@ -202,13 +200,4 @@ open class EventPipeline(

return shouldCleanup
}

private fun registerShutdownHook() {
// close the stream if the app shuts down
Runtime.getRuntime().addShutdownHook(object : Thread() {
override fun run() {
[email protected]()
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ open class FileEventStream(

init {
createDirectory(directory)
registerShutdownHook()
}

protected open var fs: FileOutputStream? = null
Expand Down Expand Up @@ -239,13 +238,4 @@ open class FileEventStream(
val file = File(source)
return if (file.exists()) FileInputStream(file) else null
}

private fun registerShutdownHook() {
// close the stream if the app shuts down
Runtime.getRuntime().addShutdownHook(object : Thread() {
override fun run() {
fs?.close()
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class EventsFileManager(

init {
createDirectory(directory)
registerShutdownHook()
}

private val fileIndexKey = if(subject == null) "segment.events.file.index.$writeKey" else "segment.events.file.index.$writeKey.$subject"
Expand Down Expand Up @@ -170,15 +169,6 @@ class EventsFileManager(
curFile = null
}

private fun registerShutdownHook() {
// close the stream if the app shuts down
Runtime.getRuntime().addShutdownHook(object : Thread() {
override fun run() {
os?.close()
}
})
}

private suspend fun withLock(block: () -> Unit) {
semaphore.acquire()
block()
Expand Down
25 changes: 0 additions & 25 deletions gradle/artifacts-android.gradle

This file was deleted.

7 changes: 1 addition & 6 deletions gradle/mvn-publish.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
apply plugin: 'maven-publish'
apply plugin: 'signing'

publishing {
publications {
Expand Down Expand Up @@ -74,11 +73,7 @@ publishing {
}
}


signing {
sign publishing.publications
}

publish.dependsOn build
publishToMavenLocal.dependsOn build
publishReleasePublicationToMavenLocal.dependsOn jar
publishToSonatype.dependsOn publish