Skip to content
Permalink
Browse files

Issue #5: Schedule frequent updates of experiments configuration

Closes #5: Schedule frequent updates of experiments configuration

Fix serialization issues and add Sync class

Don't expose SyncTask and add license headers

Add jobFinished to SyncJob and add tests for SyncConfigExtensions

Document Fretboard scheduleUpdateExperiments and Sync execute methods

Removed serializable classes and scheduler interface and make SyncJob abstract

Pass jobId as schedule parameter
  • Loading branch information...
fercarcedo authored and pocmo committed Jun 5, 2018
1 parent cd812f3 commit fcf13f7b794966f2f111ebaccc2a468465c72fff
@@ -0,0 +1 @@
/build
@@ -0,0 +1,49 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'jacoco'
apply plugin: 'jacoco-android'

android {
compileSdkVersion rootProject.ext.build['compileSdkVersion']

defaultConfig {
minSdkVersion rootProject.ext.build['minSdkVersion']
targetSdkVersion rootProject.ext.build['targetSdkVersion']

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

lintOptions {
warningsAsErrors true
abortOnError true
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

jacocoAndroidUnitTestReport {
csv.enabled false
html.enabled true
xml.enabled true
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:${rootProject.ext.dependencies['kotlin']}"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${rootProject.ext.dependencies['coroutines']}"
implementation project(':fretboard')

testImplementation "junit:junit:${rootProject.ext.dependencies['junit']}"
testImplementation "org.robolectric:robolectric:${rootProject.ext.dependencies['robolectric']}"
testImplementation "org.mockito:mockito-core:${rootProject.ext.dependencies['mockito']}"
androidTestImplementation "com.android.support.test:runner:${rootProject.ext.dependencies['runner']}"
androidTestImplementation "com.android.support:support-annotations:${rootProject.ext.dependencies['annotations']}"
}
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
@@ -0,0 +1,5 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mozilla.components.service.fretboard.scheduler.jobscheduler" />
@@ -0,0 +1,41 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.service.fretboard.scheduler.jobscheduler

import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import java.util.concurrent.TimeUnit

/**
* Class used to schedule sync of experiment
* configuration from the server
*/
class JobSchedulerSyncScheduler(context: Context) {
private val jobScheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler

/**
* Schedule sync with the constrains specified
*
* @param jobInfo object with the job constraints
*/
fun schedule(jobInfo: JobInfo) {
jobScheduler.schedule(jobInfo)
}

/**
* Schedule sync with the default constraints
* (once a day)
*
* @param serviceName object with the service to run
*/
fun schedule(jobId: Int, serviceName: ComponentName) {
val jobInfo = JobInfo.Builder(jobId, serviceName)
.setPeriodic(TimeUnit.DAYS.toMillis(1))
.build()
jobScheduler.schedule(jobInfo)
}
}
@@ -0,0 +1,40 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.service.fretboard.scheduler.jobscheduler

import android.app.job.JobParameters
import android.app.job.JobService
import mozilla.components.service.fretboard.Fretboard
import java.util.concurrent.Executors

abstract class SyncJob : JobService() {
private val executor = Executors.newSingleThreadExecutor()

override fun onStartJob(params: JobParameters): Boolean {
executor.execute {
try {
getFretboard().updateExperiments()
} catch (e: InterruptedException) {
// Cancel thread
} finally {
jobFinished(params, false)
}
}
return true
}

override fun onStopJob(params: JobParameters?): Boolean {
executor.shutdownNow()
return true
}

/**
* Used to provide the instance of Fretboard
* the app is using
*
* @return current Fretboard instance
*/
abstract fun getFretboard(): Fretboard
}
@@ -1 +1 @@
include ':fretboard', ':fretboard-client-kinto', ':fretboard-storage-flatfile'
include ':fretboard', ':fretboard-client-kinto', ':fretboard-storage-flatfile', ':fretboard-scheduler-jobscheduler'

0 comments on commit fcf13f7

Please sign in to comment.
You can’t perform that action at this time.