-
Notifications
You must be signed in to change notification settings - Fork 627
Jobscheduler #315
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
Merged
Merged
Jobscheduler #315
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
b778f4a
add scheduler
VinayGuthal 7f5150f
add scheduler service
VinayGuthal 56403a6
add scheduler log
VinayGuthal 8345b1f
switch branches
VinayGuthal 5b6fb1d
add jobscheduler
VinayGuthal 0fa700f
use static
VinayGuthal 2b4c7f7
update remote receiver
VinayGuthal 35b1a6f
use max instead of min
VinayGuthal c9e9486
add uploader to upload stuff
VinayGuthal a51a6eb
address comments
VinayGuthal 507108d
address nit
VinayGuthal 2307fb1
jobscheduler scheduler
VinayGuthal 59b4d1e
make changes
VinayGuthal 3a2262a
revert idea change
VinayGuthal df8c7ae
make small changes
VinayGuthal 33aa937
add tests
VinayGuthal 3ce957d
schedule test
VinayGuthal 1767356
add javadocs
VinayGuthal b733aa2
add javadocs
VinayGuthal ceb5fa3
address the comments
VinayGuthal fce46fe
address comments
VinayGuthal 86b3621
Merge branch 'master' into jobscheduler
VinayGuthal 4149219
move back to immediate scheduler
VinayGuthal 5bdf21e
Gjf
VinayGuthal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
.../google/android/datatransport/runtime/scheduling/jobscheduling/AlarmManagerScheduler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2018 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.android.datatransport.runtime.scheduling.jobscheduling; | ||
|
||
import android.content.Context; | ||
import com.google.android.datatransport.runtime.scheduling.persistence.EventStore; | ||
import com.google.android.datatransport.runtime.time.Clock; | ||
import javax.inject.Inject; | ||
|
||
public class AlarmManagerScheduler implements WorkScheduler { | ||
|
||
private final Context context; | ||
|
||
private final EventStore eventStore; | ||
|
||
private final Clock clock; | ||
|
||
@Inject | ||
public AlarmManagerScheduler(Context applicationContext, EventStore eventStore, Clock clock) { | ||
this.context = applicationContext; | ||
this.eventStore = eventStore; | ||
this.clock = clock; | ||
} | ||
|
||
@Override | ||
public void schedule(String backendName, int attemptNumber) {} | ||
} |
84 changes: 84 additions & 0 deletions
84
...a/com/google/android/datatransport/runtime/scheduling/jobscheduling/JobInfoScheduler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2018 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.android.datatransport.runtime.scheduling.jobscheduling; | ||
|
||
import android.annotation.TargetApi; | ||
import android.app.job.JobInfo; | ||
import android.app.job.JobScheduler; | ||
import android.content.ComponentName; | ||
import android.content.Context; | ||
import android.os.Build; | ||
import android.os.PersistableBundle; | ||
import android.support.annotation.RequiresApi; | ||
import com.google.android.datatransport.runtime.scheduling.persistence.EventStore; | ||
import com.google.android.datatransport.runtime.time.Clock; | ||
import java.util.zip.Adler32; | ||
import javax.inject.Inject; | ||
|
||
public class JobInfoScheduler implements WorkScheduler { | ||
|
||
private final Context context; | ||
|
||
private final EventStore eventStore; | ||
|
||
private final Clock clock; | ||
|
||
@Inject | ||
public JobInfoScheduler(Context applicationContext, EventStore eventStore, Clock clock) { | ||
this.context = applicationContext; | ||
this.eventStore = eventStore; | ||
this.clock = clock; | ||
} | ||
|
||
private int getJobId(String backendName) { | ||
Adler32 checksum = new Adler32(); | ||
checksum.update(backendName.getBytes()); | ||
return (int) checksum.getValue(); | ||
} | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private boolean isJobServiceOn(JobScheduler scheduler, int jobId) { | ||
for (JobInfo jobInfo : scheduler.getAllPendingJobs()) { | ||
if (jobInfo.getId() == jobId) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.M) | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@android.support.annotation.RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | ||
@Override | ||
public void schedule(String backendName, int attemptNumber) { | ||
ComponentName serviceComponent = new ComponentName(context, JobInfoSchedulerService.class); | ||
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); | ||
int jobId = getJobId(backendName); | ||
// Check if there exists a job scheduled for this backend name. | ||
if (isJobServiceOn(jobScheduler, jobId)) return; | ||
// Obtain the next available call time for the backend. | ||
long timeDiff = eventStore.getNextCallTime(backendName) - clock.getTime(); | ||
// Schedule the build. | ||
PersistableBundle bundle = new PersistableBundle(); | ||
bundle.putInt(SchedulerUtil.ATTEMPT_NUMBER, attemptNumber); | ||
bundle.putString(SchedulerUtil.BACKEND_NAME, backendName); | ||
JobInfo.Builder builder = new JobInfo.Builder(jobId, serviceComponent); | ||
builder.setMinimumLatency( | ||
clock.getTime() | ||
+ SchedulerUtil.getScheduleDelay(timeDiff, 5000, attemptNumber)); // wait at least | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
builder.setExtras(bundle); | ||
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); | ||
jobScheduler.schedule(builder.build()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...oogle/android/datatransport/runtime/scheduling/jobscheduling/JobInfoSchedulerService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2018 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.android.datatransport.runtime.scheduling.jobscheduling; | ||
|
||
import android.app.job.JobParameters; | ||
import android.app.job.JobService; | ||
import android.os.Build; | ||
import com.google.android.datatransport.runtime.TransportRuntime; | ||
|
||
/** JobService to be scheduled by the JobScheduler. start another service */ | ||
@android.support.annotation.RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class JobInfoSchedulerService extends JobService { | ||
|
||
@Override | ||
public boolean onStartJob(JobParameters params) { | ||
String backendName = params.getExtras().getString(SchedulerUtil.BACKEND_NAME); | ||
int attemptNumber = params.getExtras().getInt(SchedulerUtil.ATTEMPT_NUMBER); | ||
TransportRuntime.initialize(getApplicationContext()); | ||
TransportRuntime.getInstance() | ||
.getUploader() | ||
.upload(backendName, attemptNumber, () -> this.jobFinished(params, false)); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onStopJob(JobParameters params) { | ||
return false; | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...java/com/google/android/datatransport/runtime/scheduling/jobscheduling/SchedulerUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2018 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.android.datatransport.runtime.scheduling.jobscheduling; | ||
|
||
public class SchedulerUtil { | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
static final String ATTEMPT_NUMBER = "attemptNumber"; | ||
|
||
static final String BACKEND_NAME = "backendName"; | ||
|
||
static final String APPLICATION_BUNDLE_ID = "appBundleId"; | ||
|
||
static long getScheduleDelay(long backendTimeDiff, int delta, int numberOfAttempts) { | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (numberOfAttempts > 3) { | ||
return 1000000000; | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return Math.max((long) (Math.pow(delta / 1000, numberOfAttempts)) * 1000, backendTimeDiff); | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...main/java/com/google/android/datatransport/runtime/scheduling/jobscheduling/Uploader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2018 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.android.datatransport.runtime.scheduling.jobscheduling; | ||
|
||
import javax.inject.Inject; | ||
|
||
public class Uploader { | ||
|
||
@Inject | ||
public Uploader() {} | ||
|
||
void upload(String backendName, int attemptNumber, Runnable callback) {} | ||
} |
19 changes: 19 additions & 0 deletions
19
...java/com/google/android/datatransport/runtime/scheduling/jobscheduling/WorkScheduler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2018 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.android.datatransport.runtime.scheduling.jobscheduling; | ||
|
||
public interface WorkScheduler { | ||
void schedule(String backendName, int numberOfAttempts); | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.