diff --git a/android/build.gradle b/android/build.gradle index 4b91ead4..ceb8a839 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -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") } \ No newline at end of file diff --git a/core/build.gradle b/core/build.gradle index 0d1e79a8..759b02c4 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -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") -} \ No newline at end of file diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt b/core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt index 0975d1eb..7d3bcae3 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt @@ -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 @@ -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() + } + } } /** @@ -773,4 +799,4 @@ internal fun isAndroid(): Boolean { } catch (ignored: ClassNotFoundException) { false } -} +} \ No newline at end of file diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/platform/EventPipeline.kt b/core/src/main/java/com/segment/analytics/kotlin/core/platform/EventPipeline.kt index a133ad2e..f0e3568d 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/platform/EventPipeline.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/platform/EventPipeline.kt @@ -53,8 +53,6 @@ open class EventPipeline( writeChannel = Channel(UNLIMITED) uploadChannel = Channel(UNLIMITED) - - registerShutdownHook() } fun put(event: BaseEvent) { @@ -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() { - this@EventPipeline.stop() - } - }) - } } \ No newline at end of file diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventStream.kt b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventStream.kt index 5ee3ed00..f0f9f7f9 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventStream.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventStream.kt @@ -160,7 +160,6 @@ open class FileEventStream( init { createDirectory(directory) - registerShutdownHook() } protected open var fs: FileOutputStream? = null @@ -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() - } - }) - } } \ No newline at end of file diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt index cb778aa8..78fc98f6 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt @@ -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" @@ -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() diff --git a/gradle/artifacts-android.gradle b/gradle/artifacts-android.gradle deleted file mode 100644 index 9ecc8fa9..00000000 --- a/gradle/artifacts-android.gradle +++ /dev/null @@ -1,25 +0,0 @@ -task sourcesJar(type: Jar) { - archiveClassifier.set('sources') - from android.sourceSets.main.java.srcDirs -} - -task javadoc(type: Javadoc) { - configurations.implementation.setCanBeResolved(true) - - failOnError false - source = android.sourceSets.main.java.sourceFiles - classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) - classpath += configurations.implementation -} - -// build a jar with javadoc -task javadocJar(type: Jar, dependsOn: javadoc) { - archiveClassifier.set('javadoc') - from javadoc.destinationDir -} - -// Attach Javadocs and Sources jar -artifacts { - archives sourcesJar - archives javadocJar -} \ No newline at end of file diff --git a/gradle/mvn-publish.gradle b/gradle/mvn-publish.gradle index 63fd8a94..939dd9bb 100644 --- a/gradle/mvn-publish.gradle +++ b/gradle/mvn-publish.gradle @@ -1,5 +1,4 @@ apply plugin: 'maven-publish' -apply plugin: 'signing' publishing { publications { @@ -74,11 +73,7 @@ publishing { } } - -signing { - sign publishing.publications -} - publish.dependsOn build publishToMavenLocal.dependsOn build +publishReleasePublicationToMavenLocal.dependsOn jar publishToSonatype.dependsOn publish \ No newline at end of file