-
Notifications
You must be signed in to change notification settings - Fork 615
Android 34 WorkManager Scheduler #6221
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
emilypgoogle
wants to merge
10
commits into
main
Choose a base branch
from
ep/transport-34
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1f16596
Initial implementation
emilypgoogle dab5584
Merge branch 'main' into ep/transport-34
emilypgoogle 1cb0ed4
Requested fixes
emilypgoogle ca0644a
WorkManagerScheduler tag
emilypgoogle 7c99278
Worker changes
emilypgoogle 3a4cb1c
Migrate to tags
emilypgoogle 15c5c90
Update transport/transport-runtime/src/main/java/com/google/android/d…
emilypgoogle bd1ad49
Tag and exception
emilypgoogle 0564a10
Organize imports
emilypgoogle 1a47878
Format
emilypgoogle 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
112 changes: 112 additions & 0 deletions
112
...m/google/android/datatransport/runtime/scheduling/jobscheduling/WorkManagerScheduler.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,112 @@ | ||
// Copyright 2024 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 static android.util.Base64.DEFAULT; | ||
import static android.util.Base64.encodeToString; | ||
|
||
import android.content.Context; | ||
import android.os.Build; | ||
import androidx.annotation.RequiresApi; | ||
import androidx.work.Data; | ||
import androidx.work.OneTimeWorkRequest; | ||
import androidx.work.WorkManager; | ||
import androidx.work.WorkRequest; | ||
import com.google.android.datatransport.runtime.TransportContext; | ||
import com.google.android.datatransport.runtime.logging.Logging; | ||
import com.google.android.datatransport.runtime.scheduling.persistence.EventStore; | ||
import com.google.android.datatransport.runtime.util.PriorityMapping; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE) | ||
public class WorkManagerScheduler implements WorkScheduler { | ||
emilypgoogle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static final String LOG_TAG = "WorkManagerScheduler"; | ||
|
||
static final String ATTEMPT_NUMBER = "attemptNumber"; | ||
static final String BACKEND_NAME = "backendName"; | ||
static final String EVENT_PRIORITY = "priority"; | ||
static final String EXTRAS = "extras"; | ||
private static final Map<Integer, UUID> JOBS = new ConcurrentHashMap<>(); | ||
private final Context context; | ||
|
||
private final EventStore eventStore; | ||
|
||
private final SchedulerConfig config; | ||
|
||
public WorkManagerScheduler( | ||
Context applicationContext, EventStore eventStore, SchedulerConfig config) { | ||
this.context = applicationContext; | ||
this.eventStore = eventStore; | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public void schedule(TransportContext transportContext, int attemptNumber) { | ||
schedule(transportContext, attemptNumber, false); | ||
} | ||
|
||
@Override | ||
public void schedule(TransportContext transportContext, int attemptNumber, boolean force) { | ||
WorkManager manager = WorkManager.getInstance(context); | ||
|
||
int jobId = WorkScheduler.getJobId(context, transportContext); | ||
if (!force && JOBS.containsKey(jobId)) { | ||
emilypgoogle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
if (!manager.getWorkInfoById(JOBS.get(jobId)).get().getState().isFinished()) { | ||
Logging.d( | ||
LOG_TAG, | ||
"Upload for context %s is already scheduled. Returning...", | ||
transportContext); | ||
return; | ||
} | ||
} catch (Exception e) { | ||
emilypgoogle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
Data.Builder dataBuilder = new Data.Builder(); | ||
dataBuilder.putInt(ATTEMPT_NUMBER, attemptNumber); | ||
dataBuilder.putString(BACKEND_NAME, transportContext.getBackendName()); | ||
dataBuilder.putInt(EVENT_PRIORITY, PriorityMapping.toInt(transportContext.getPriority())); | ||
emilypgoogle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (transportContext.getExtras() != null) { | ||
dataBuilder.putString(EXTRAS, encodeToString(transportContext.getExtras(), DEFAULT)); | ||
} | ||
|
||
long backendTime = eventStore.getNextCallTime(transportContext); | ||
boolean hasPendingEvents = force && eventStore.hasPendingEventsFor(transportContext); | ||
|
||
long scheduleDelay = | ||
config.getScheduleDelay( | ||
transportContext.getPriority(), backendTime, attemptNumber, hasPendingEvents); | ||
|
||
Logging.d( | ||
LOG_TAG, | ||
"Scheduling upload for context %s in %dms(Backend next call timestamp %d). Attempt %d", | ||
transportContext, | ||
scheduleDelay, | ||
backendTime, | ||
attemptNumber); | ||
|
||
WorkRequest request = | ||
new OneTimeWorkRequest.Builder(WorkManagerSchedulerWorker.class) | ||
.setInitialDelay(scheduleDelay, TimeUnit.MILLISECONDS) | ||
.setInputData(dataBuilder.build()) | ||
.build(); | ||
JOBS.put(jobId, request.getId()); | ||
manager.enqueue(request); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...le/android/datatransport/runtime/scheduling/jobscheduling/WorkManagerSchedulerWorker.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,61 @@ | ||
// Copyright 2024 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 android.os.Build; | ||
import android.util.Base64; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.RequiresApi; | ||
import androidx.work.Data; | ||
import androidx.work.Worker; | ||
import androidx.work.WorkerParameters; | ||
import com.google.android.datatransport.runtime.TransportContext; | ||
import com.google.android.datatransport.runtime.TransportRuntime; | ||
import com.google.android.datatransport.runtime.util.PriorityMapping; | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.UPSIDE_DOWN_CAKE) | ||
public class WorkManagerSchedulerWorker extends Worker { | ||
|
||
public WorkManagerSchedulerWorker( | ||
@NonNull Context context, @NonNull WorkerParameters workerParams) { | ||
super(context, workerParams); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Result doWork() { | ||
Data data = getInputData(); | ||
String backendName = data.getString(JobInfoScheduler.BACKEND_NAME); | ||
String extras = data.getString(JobInfoScheduler.EXTRAS); | ||
|
||
int priority = data.getInt(JobInfoScheduler.EVENT_PRIORITY, 0); | ||
int attemptNumber = data.getInt(JobInfoScheduler.ATTEMPT_NUMBER, 0); | ||
TransportRuntime.initialize(getApplicationContext()); | ||
TransportContext.Builder transportContext = | ||
TransportContext.builder() | ||
.setBackendName(backendName) | ||
.setPriority(PriorityMapping.valueOf(priority)); | ||
|
||
if (extras != null) { | ||
transportContext.setExtras(Base64.decode(extras, Base64.DEFAULT)); | ||
} | ||
|
||
TransportRuntime.getInstance() | ||
.getUploader() | ||
.upload(transportContext.build(), attemptNumber, () -> {}); | ||
return Result.success(); | ||
} | ||
} |
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
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.