为什么Jetpack WorkManager反复调用doWork() ?



问题描述

当使用WorkManager执行唯一的任务时,在doWork()中发生错误并且处理异常为Result.failure()后,doWork()将再次被调用。

我试图通过日志文件排除错误,我发现androidx.work.impl.Schedulers#createBestAvailableBackgroundSchedulersetComponentEnabled(context, SystemJobService.class, true);被调用了两次。WorkManagerImpl是一个单例对象,不会被调用两次。所以我很困惑。

日志

01-15 15:25:01.198  7797  7797 D WM-PackageManagerHelper: androidx.work.impl.background.systemjob.SystemJobService enabled
01-15 15:25:01.198  7797  7797 D WM-Schedulers: Created SystemJobScheduler and enabled SystemJobService
01-15 15:25:01.208  7797  7847 D WM-ForceStopRunnable: Performing cleanup operations.
01-15 15:25:01.303  7797  7851 D WM-PackageManagerHelper: androidx.work.impl.background.systemalarm.RescheduleReceiver enabled
01-15 15:25:01.319  7797  7851 D WM-SystemJobScheduler: Scheduling work ID b278a34c-aeeb-4c88-8f29-71b9c23e0954 Job ID 68
01-15 15:25:01.327  7797  7851 D WM-GreedyScheduler: Starting work for b278a34c-aeeb-4c88-8f29-71b9c23e0954
01-15 15:25:01.334  7797  7858 D WM-Processor: Processor: processing b278a34c-aeeb-4c88-8f29-71b9c23e0954
01-15 15:25:02.742  7797  7797 D WM-WorkerWrapper: Starting work for my_package.service.StatisticsService
01-15 15:25:02.822  7797  7847 D WM-WorkerWrapper: my_package.service.StatisticsService returned a Failure {mOutputData=Data {}} result.
01-15 15:25:02.829  7797  7847 I WM-WorkerWrapper: Worker result FAILURE for Work [ id=b278a34c-aeeb-4c88-8f29-71b9c23e0954, tags={ my_package.service.StatisticsService } ]
01-15 15:25:02.842  7797  7847 D WM-PackageManagerHelper: androidx.work.impl.background.systemalarm.RescheduleReceiver disabled
01-15 15:25:02.847  7797  7797 D WM-SystemJobService: onStartJob for b278a34c-aeeb-4c88-8f29-71b9c23e0954
01-15 15:25:02.847  7797  7847 D WM-GreedyScheduler: Cancelling work ID b278a34c-aeeb-4c88-8f29-71b9c23e0954
01-15 15:25:02.862  7797  7851 D WM-Processor: Work b278a34c-aeeb-4c88-8f29-71b9c23e0954 is already enqueued for processing
01-15 15:25:02.868  7797  7851 D WM-Processor: Processor stopping background work b278a34c-aeeb-4c88-8f29-71b9c23e0954
01-15 15:25:02.869  7797  7851 D WM-WorkerWrapper: Work interrupted for Work [ id=b278a34c-aeeb-4c88-8f29-71b9c23e0954, tags={ my_package.service.StatisticsService } ]
01-15 15:25:02.883  7797  7851 D WM-PackageManagerHelper: androidx.work.impl.background.systemalarm.RescheduleReceiver disabled
01-15 15:25:02.884  7797  7851 D WM-WorkerWrapper: WorkSpec {WorkSpec: b278a34c-aeeb-4c88-8f29-71b9c23e0954} is already done. Not interrupting.
01-15 15:25:02.884  7797  7851 D WM-Processor: WorkerWrapper interrupted for b278a34c-aeeb-4c88-8f29-71b9c23e0954
01-15 15:25:02.884  7797  7851 D WM-StopWorkRunnable: StopWorkRunnable for b278a34c-aeeb-4c88-8f29-71b9c23e0954; Processor.stopWork = true
01-15 15:25:02.899  7797  7797 D WM-Processor: Processor b278a34c-aeeb-4c88-8f29-71b9c23e0954 executed; reschedule = false
01-15 15:25:02.900  7797  7797 D WM-SystemJobService: b278a34c-aeeb-4c88-8f29-71b9c23e0954 executed on JobScheduler
01-15 15:25:02.900  7797  7797 D WM-SystemJobService: onStopJob for b278a34c-aeeb-4c88-8f29-71b9c23e0954
01-15 15:25:02.903  7797  7847 D WM-Processor: Processor stopping background work b278a34c-aeeb-4c88-8f29-71b9c23e0954
01-15 15:25:02.903  7797  7847 D WM-Processor: WorkerWrapper could not be found for b278a34c-aeeb-4c88-8f29-71b9c23e0954
01-15 15:25:02.904  7797  7847 D WM-StopWorkRunnable: StopWorkRunnable for b278a34c-aeeb-4c88-8f29-71b9c23e0954; Processor.stopWork = false
// Here doWork() is repeated once.
01-15 15:25:04.511  7923  7923 D WM-PackageManagerHelper: androidx.work.impl.background.systemjob.SystemJobService enabled
01-15 15:25:04.511  7923  7923 D WM-Schedulers: Created SystemJobScheduler and enabled SystemJobService
01-15 15:25:04.519  7923  7985 D WM-ForceStopRunnable: Performing cleanup operations.
01-15 15:25:04.600  7923  7989 D WM-PackageManagerHelper: androidx.work.impl.background.systemalarm.RescheduleReceiver enabled
01-15 15:25:04.616  7923  7989 D WM-SystemJobScheduler: Scheduling work ID 07bddcf0-33c3-43c5-ad2b-0dcb1f200e18 Job ID 69
01-15 15:25:04.624  7923  7989 I WM-GreedyScheduler: Ignoring schedule request in non-main process
01-15 15:25:04.633  7797  7797 D WM-SystemJobService: onStartJob for 07bddcf0-33c3-43c5-ad2b-0dcb1f200e18
01-15 15:25:04.634  7797  7851 D WM-Processor: Processor: processing 07bddcf0-33c3-43c5-ad2b-0dcb1f200e18
01-15 15:25:04.650  7797  7797 D WM-WorkerWrapper: Starting work for my_package.service.StatisticsService
01-15 15:25:04.697  7797  7847 D WM-WorkerWrapper: my_package.service.StatisticsService returned a Failure {mOutputData=Data {}} result.
01-15 15:25:04.700  7797  7847 I WM-WorkerWrapper: Worker result FAILURE for Work [ id=07bddcf0-33c3-43c5-ad2b-0dcb1f200e18, tags={ my_package.service.StatisticsService } ]
01-15 15:25:04.704  7797  7847 D WM-PackageManagerHelper: androidx.work.impl.background.systemalarm.RescheduleReceiver disabled
01-15 15:25:04.704  7797  7797 D WM-Processor: Processor 07bddcf0-33c3-43c5-ad2b-0dcb1f200e18 executed; reschedule = false
01-15 15:25:04.705  7797  7797 D WM-SystemJobService: 07bddcf0-33c3-43c5-ad2b-0dcb1f200e18 executed on JobScheduler
01-15 15:25:04.713  7797  7847 D WM-GreedyScheduler: Cancelling work ID 07bddcf0-33c3-43c5-ad2b-0dcb1f200e18
01-15 15:25:04.718  7797  7847 D WM-Processor: Processor stopping background work 07bddcf0-33c3-43c5-ad2b-0dcb1f200e18
01-15 15:25:04.719  7797  7847 D WM-Processor: WorkerWrapper could not be found for 07bddcf0-33c3-43c5-ad2b-0dcb1f200e18
01-15 15:25:04.719  7797  7847 D WM-StopWorkRunnable: StopWorkRunnable for 07bddcf0-33c3-43c5-ad2b-0dcb1f200e18; Processor.stopWork = false


场景我需要执行一个任务,每次我打开应用程序上传我的统计数据时只执行一次,如果没有网络,缓存它。

我的环境jetpack workmanager 2.4.0

我的代码 Application.java

// Start a background only task.
StatisticsService.startService(getApplicationContext());

StatisticsService.java

public static void startService(Context context) {
    OneTimeWorkRequest.Builder builder = new OneTimeWorkRequest.Builder(StatisticsService.class);
    ...
    Data.Builder data = new Data.Builder();
    data.putBoolean(EXTRA_IS_FIRST, isFirstOpenApp);
    builder.setInputData(data.build());
    WorkManager.getInstance(context).enqueueUniqueWork("StatisticsService", ExistingWorkPolicy.APPEND_OR_REPLACE, builder.build());
}

@NonNull
@Override
public Result doWork() {
    log.info("doWork:" + hashCode());
    boolean isSuccessful = false;
    try {
        Data inputData = getInputData();
        boolean isFirstOpenApp = inputData.getBoolean(EXTRA_IS_FIRST, true);
        isSuccessful = uploadStatisticsInfo(isFirstOpenApp);
        if (isSuccessful) {
            return Result.success();
        } else {
            return Result.failure();
        }
    } catch (Exception e) {
        log.severe(ThrowableUtils.getFullStackTrace(e));
        return Result.failure();
    }
}
private boolean uploadStatisticsInfo(boolean isFirstOpenApp) {
    // Synchronously determine whether the network is available.
    // If the network is unavailable, an exception will be thrown.
    boolean available = NetworkUtils.isAvailable();
    if (!available) {
        AppStatisticsBean.cacheOneMap(isFirstOpenApp);
        return false;
    }
    // Upload statistics
    ...
    if (success){
        return true;
    } else {
        return false;
    }
}

您正在将WorkRequest作为具有ExistingWorkPolicy.APPEND_OR_REPLACE策略的唯一工作进行排队。这意味着,如果已经有另一个请求挂起,则将新请求附加到现有请求上,从而创建一个工作链。
只有在先前的工作失败时才使用替换件。

创建一个工作链,一旦一个工作被正确执行,链中的下一个工作将被执行。这在WorkManager的文档中有解释。

另一种选择是使用ExistingWorkPolicy.KEEP策略,以便只有一个WorkRequest将被排队。

相关内容

  • 没有找到相关文章

最新更新