Skip to content

Commit

Permalink
Updated SchedulerImpl to better handle scheduling of workers;
Browse files Browse the repository at this point in the history
  • Loading branch information
semsudin-tafilovic committed Oct 3, 2024
1 parent f01e8ab commit 15d1030
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 156 deletions.
63 changes: 30 additions & 33 deletions android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,39 +96,36 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation libs.kotlinStdlib
implementation libs.appCompat

implementation libs.coroutinesCore
implementation libs.coroutinesAndroid

implementation libs.workmanager

implementation libs.okHttp

implementation libs.room
implementation libs.roomKtx
testImplementation 'junit:junit:4.13.2'
kapt libs.roomCompiler

testImplementation libs.junit
testImplementation libs.androidTestCore
testImplementation libs.mockk
testImplementation libs.coroutinesTest
testImplementation libs.kotlinTest

androidTestImplementation libs.androidTestJunit
androidTestImplementation libs.androidTestCore
androidTestImplementation libs.runner
androidTestImplementation libs.androidMockk
androidTestImplementation libs.androidArchCoreTest

androidTestImplementation libs.androidRoomTest
androidTestImplementation libs.androidWorkmanagerTest
androidTestImplementation libs.truth
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.10.3'
//testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
//api group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
implementation ("androidx.appcompat:appcompat:1.7.0")
implementation ("org.jetbrains.kotlin:kotlin-stdlib:1.9.24")

implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1")

implementation ("androidx.work:work-runtime-ktx:2.9.1")

implementation ("com.squareup.okhttp3:okhttp:4.12.0")

implementation ("androidx.room:room-runtime:2.6.1")
implementation ("androidx.room:room-ktx:2.6.1")
kapt ("androidx.room:room-compiler:2.6.1")

testImplementation ("junit:junit:4.13.2")
testImplementation ("androidx.test:core:1.6.1")
testImplementation ("io.mockk:mockk:1.13.12")
testImplementation ("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.1")
testImplementation ("io.kotlintest:kotlintest-runner-junit5:3.4.2")
testImplementation ("org.junit.jupiter:junit-jupiter-engine:5.10.3")

androidTestImplementation ("androidx.test.ext:junit:1.2.1")
androidTestImplementation ("androidx.test:core:1.6.1")
androidTestImplementation ("androidx.test:runner:1.6.2")
androidTestImplementation ("io.mockk:mockk-android:1.13.12")
//androidTestImplementation ("androidx.arch.core:core-testing:2.2.0")

androidTestImplementation ("androidx.room:room-testing:2.6.1")
androidTestImplementation ("androidx.work:work-testing:2.9.1")
androidTestImplementation ("com.google.truth:truth:1.4.4")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequest
import androidx.work.OutOfQuotaPolicy
import androidx.work.PeriodicWorkRequest
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkInfo
import androidx.work.WorkManager
import kotlinx.coroutines.sync.Mutex
Expand Down Expand Up @@ -82,16 +83,34 @@ internal class SchedulerImpl(
}
}

// To be changed to clean up after executing the requests
override fun scheduleCleanUp() {
val data = Data.Builder().apply {
putStringArray("trackIds", config.trackIds.toTypedArray())
putString("trackDomain", config.trackDomain)
}.build()

val cleanWorkBuilder = PeriodicWorkRequestBuilder<CleanUpWorker>(60, TimeUnit.MINUTES)
.addTag(CleanUpWorker.TAG)
.setInitialDelay(5, TimeUnit.MINUTES)
.setInputData(data)

workManager.enqueueUniquePeriodicWork(
CLEAN_UP_WORKER,
ExistingPeriodicWorkPolicy.UPDATE,
cleanWorkBuilder.build()
)
}

override fun sendRequestsThenCleanUp() {
webtrekkLogger.debug("SEND WORKER - sendRequestsThenCleanUp")
synchronized(mutex) {
// check if SendRequestsWorker already running as periodic work request
// val future = workManager.getWorkInfosByTag(SendRequestsWorker.TAG)
// val workers = future.get()
// if (workers.none { it.state in listOf(WorkInfo.State.RUNNING) }) {
// scheduleSendAndCleanWorkers()
// }
scheduleSendAndCleanWorkers()
val future = workManager.getWorkInfosForUniqueWork(ONE_TIME_REQUEST)
val workers = future.get()
if (workers.none { it.state in listOf(WorkInfo.State.RUNNING) }) {
scheduleSendAndCleanWorkers()
}
}
}

Expand All @@ -102,8 +121,8 @@ internal class SchedulerImpl(
}.build()

val sendWorkBuilder = OneTimeWorkRequest.Builder(SendRequestsWorker::class.java)
.addTag(SendRequestsWorker.TAG)
.setInputData(data)
.addTag(SendRequestsWorker.TAG)

val cleanWorkBuilder = OneTimeWorkRequest.Builder(CleanUpWorker::class.java)
.setInputData(data)
Expand All @@ -114,42 +133,24 @@ internal class SchedulerImpl(
cleanWorkBuilder.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
}

workManager.enqueueUniqueWork(
workManager.beginUniqueWork(
ONE_TIME_REQUEST,
ExistingWorkPolicy.APPEND_OR_REPLACE,
listOf(sendWorkBuilder.build(), cleanWorkBuilder.build())
)
}

// To be changed to clean up after executing the requests
override fun scheduleCleanUp() {
val data = Data.Builder().apply {
putStringArray("trackIds", config.trackIds.toTypedArray())
putString("trackDomain", config.trackDomain)
}.build()

val cleanWorkBuilder = OneTimeWorkRequest.Builder(CleanUpWorker::class.java)
.addTag(CleanUpWorker.TAG)
.setInputData(data)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
cleanWorkBuilder.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
}

workManager.enqueueUniqueWork(
"CleanUpWorker",
ExistingWorkPolicy.APPEND_OR_REPLACE,
cleanWorkBuilder.build()
ExistingWorkPolicy.REPLACE,
sendWorkBuilder.build()
)
.then(cleanWorkBuilder.build())
.enqueue()
}

override fun cancelScheduleSendRequests() {
workManager.cancelAllWorkByTag(CleanUpWorker.TAG)
workManager.cancelAllWorkByTag(SendRequestsWorker.TAG)
workManager.cancelUniqueWork(CLEAN_UP_WORKER)
workManager.cancelUniqueWork(ONE_TIME_REQUEST)
workManager.cancelUniqueWork(SEND_REQUESTS_WORKER)
}

companion object {
const val SEND_REQUESTS_WORKER = "send_requests_worker"
const val ONE_TIME_REQUEST = "one_time_request_send_and_clean"
const val CLEAN_UP_WORKER = "CleanUpWorker"
}
}
69 changes: 7 additions & 62 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ buildscript {
ext.versions = [
'webtrekkVersion' : '5.0.0',
'gradleVersion' : '3.6.4',
'kotlinVersion' : '1.9.24',//'1.4.32',
'kotlinVersion' : '1.9.24',
'gradleVersionsPlugin' : '0.21.0',
'mavenPluginVersion' : '2.1',
'bintrayPluginVersion' : '1.8.4',
Expand All @@ -41,61 +41,6 @@ buildscript {
'minSdkVersion' : 21,
'targetSdkVersion' : 34,
'buildTools' : '34.0.0',

'appCompatVersion' : '1.7.0',
'constraintLayoutVersion': '2.1.4',

'kotlinCoroutines' : '1.8.1',
'coreKtxVersion' : '1.13.1',

'okHttpVersion' : '4.12.0',

'workManagerVersion' : '2.9.0',
'roomVersion' : '2.6.1',

'testCoreVersion' : '1.6.1',
'testCoreVersionJunit' : '1.2.1',
'junitVersion' : '4.13.2',
'runnerVersion' : '1.6.1',
'espressoCoreVersion' : '3.5.1',
//Version 2.1.0 depends on androidx.arch.core:core-runtime:2.1.0 which must be explicitly added in android-sdk gradle
'coreTestingVersion' : '2.2.0',
//Downgraded version from 1.9.3 to 1.9.2 while this issue is not resolved
//https://github.com/mockk/mockk/issues/281
'mockkVersion' : '1.13.12',
'kotlinTestVersion' : '3.4.2',

'ktlintVersion' : '0.31.0',
'dokkaVersion' : '0.9.17',
'googleServices' : '4.3.14',
'firebaseAppDistribution': '2.1.1'
]

ext.libs = [
kotlinStdlib : "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlinVersion",
appCompat : "androidx.appcompat:appcompat:$versions.appCompatVersion",
coreKtx : "androidx.core:core-ktx:$versions.coreKtxVersion",
constraintLayout : "androidx.constraintlayout:constraintlayout:$versions.constraintLayoutVersion",
coroutinesCore : "org.jetbrains.kotlinx:kotlinx-coroutines-core:$versions.kotlinCoroutines",
coroutinesAndroid : "org.jetbrains.kotlinx:kotlinx-coroutines-android:$versions.kotlinCoroutines",
okHttp : "com.squareup.okhttp3:okhttp:$versions.okHttpVersion",
room : "androidx.room:room-runtime:$versions.roomVersion",
roomKtx : "androidx.room:room-ktx:$versions.roomVersion",
roomCompiler : "androidx.room:room-compiler:$versions.roomVersion",
workmanager : "androidx.work:work-runtime-ktx:$versions.workManagerVersion",
junit : "junit:junit:$versions.junitVersion",
runner : "androidx.test:runner:$versions.runnerVersion",
espresso : "androidx.test.espresso:espresso-core:$versions.espressoCoreVersion",
androidTestCore : "androidx.test:core:$versions.testCoreVersion",
mockk : "io.mockk:mockk:$versions.mockkVersion",
coroutinesTest : "org.jetbrains.kotlinx:kotlinx-coroutines-test:$versions.kotlinCoroutines",
kotlinTest : "io.kotlintest:kotlintest-runner-junit5:$versions.kotlinTestVersion",
androidTestJunit : "androidx.test.ext:junit:$versions.testCoreVersionJunit",
androidMockk : "io.mockk:mockk-android:$versions.mockkVersion",
androidArchCoreTest : "androidx.arch.core:core-testing:$versions.coreTestingVersion",
androidRoomTest : "androidx.room:room-testing:$versions.roomVersion",
androidWorkmanagerTest: "androidx.work:work-testing:$versions.workManagerVersion",
truth : "com.google.truth:truth:1.4.4"
]

repositories {
Expand All @@ -106,12 +51,12 @@ buildscript {

dependencies {
classpath 'com.android.tools.build:gradle:8.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlinVersion}"
classpath "com.github.ben-manes:gradle-versions-plugin:${versions.gradleVersionsPlugin}"
classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:${versions.dokkaVersion}"
classpath "com.github.dcendents:android-maven-gradle-plugin:${versions.mavenPluginVersion}"
classpath "com.google.gms:google-services:${versions.googleServices}"
classpath "com.google.firebase:firebase-appdistribution-gradle:${versions.firebaseAppDistribution}"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.24"
classpath "com.github.ben-manes:gradle-versions-plugin:0.21.0"
classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:0.9.17"
classpath "com.github.dcendents:android-maven-gradle-plugin:2.1"
classpath "com.google.gms:google-services:4.4.2"
classpath "com.google.firebase:firebase-appdistribution-gradle:5.0.0"
classpath 'com.google.firebase:firebase-crashlytics-gradle:3.0.2'
classpath 'io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.30.0'
}
Expand Down
24 changes: 7 additions & 17 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$versions.kotlinVersion"
//implementation 'androidx.recyclerview:recyclerview:1.3.2'
implementation('com.google.android.material:material:1.10.0')
implementation ("androidx.activity:activity:1.9.2")
implementation('com.google.android.material:material:1.12.0')
implementation "androidx.constraintlayout:constraintlayout:$versions.constraintLayoutVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$versions.kotlinCoroutines"
implementation "androidx.work:work-runtime-ktx:$versions.workManagerVersion"
Expand All @@ -145,30 +146,19 @@ dependencies {
implementation 'androidx.media3:media3-exoplayer-dash:1.4.1'
implementation 'androidx.media3:media3-exoplayer-hls:1.4.1'

implementation platform('com.google.firebase:firebase-bom:33.3.0')
implementation platform('com.google.firebase:firebase-bom:33.4.0')
implementation('com.google.firebase:firebase-messaging')
implementation("com.google.firebase:firebase-crashlytics")

implementation('com.mapp.sdk:mapp-android:6.0.24')

implementation(project(':android-sdk')) {
exclude(group: "androidx.appcompat", module: "appcompat")
}
// implementation("com.mapp.sdk:intelligence-android:5.1.10") {
// exclude(group: "androidx.appcompat", module: "appcompat")
// }
//implementation(project(':android-sdk'))

implementation("com.mapp.sdk:intelligence-android:5.1.9")

//debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1'

testImplementation "junit:junit:$versions.junitVersion"
androidTestImplementation "androidx.test:runner:$versions.runnerVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$versions.espressoCoreVersion"
}

//configurations.configureEach {
// resolutionStrategy.eachDependency { details ->
// if (details.requested.group == 'androidx.appcompat') {
// details.useVersion '1.7.0'
// }
// }
//}
}
7 changes: 5 additions & 2 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning"
tools:targetApi="m">
<activity
android:name=".WorkSchedulerTest"
android:exported="false" />
<activity
android:name=".tracking.OrdersTrackingActivity"
android:exported="false"></activity>
android:exported="false" />
<activity
android:name=".UrlActivity"
android:exported="false" />
Expand Down Expand Up @@ -76,7 +79,7 @@
android:exported="false" />
<activity android:name=".ObjectTrackingProductStatus" />
<activity
android:name="com.example.webtrekk.androidsdk.ManualMediaTracking"
android:name=".ManualMediaTracking"
android:exported="false"
android:screenOrientation="portrait"
android:taskAffinity="" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ class MainActivity : AppCompatActivity() {
startActivity(Intent(this, CampaignActivity::class.java))
}

binding.formActivity.setOnClickListener {
val intent = Intent(this, FormActivity::class.java)
binding.btnWorkSchedulerTest.setOnClickListener {
val intent = Intent(this, WorkSchedulerTest::class.java)
startActivity(intent)
}

Expand Down Expand Up @@ -267,8 +267,4 @@ class MainActivity : AppCompatActivity() {
Webtrekk.getInstance().sendRequestsNowAndClean()
}
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
}
}
Loading

0 comments on commit 15d1030

Please sign in to comment.