BackoffCriteria 不适用于 WorkManager Worker 类并且 getRunAttemptCount 不增加
我正在使用 WorkMangaer 并设置 backoffCriteria,但是当我运行它时,退避策略似乎不起作用,并且 getRunAttemptCount 不会增加:
以下是 logcat 中的结果,这些结果不会每 10 秒发生一次或根据退避策略发生我指定:
2022-03-05 12:11:56.705 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:56.787 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:56.787 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:56.872 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:56.872 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:56.952 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:56.952 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:57.032 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:57.032 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:57.116 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:57.116 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:57.206 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:57.206 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:57.295 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:57.295 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:57.377 doWork.IOException: Failed to connect to /10.0.0.5:5001
这是我用来对工作进行排队的代码。我尝试了线性和指数,得到了相同的结果,并增加了 backoffDelay 并使用了指定的持续时间和延迟+时间单位 此处:
WorkManager
.getInstance(context)
.cancelAllWorkByTag(sb.toString());
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresCharging(false)
.build();
OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(worker)
.setConstraints(constraints)
.addTag(sb.toString())
.setInputData(data)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL,10, TimeUnit.SECONDS)
.build();
WorkManager
.getInstance(context)
.enqueueUniqueWork(sb.toString(),REPLACE,oneTimeWorkRequest)
.getResult();
这是 doWork 方法。错误被捕获,我正在返回 Result.Retry() 因此:
@NonNull
@Override
public Result doWork() {
//get the id of the scenario
Log.d(TAG, "doWork: for " + getId() + " attempt " + getRunAttemptCount());
int scenarioRunId = getInputData().getInt(INPUT_DATA_SCENARIORUNID, -1);
CloudSync cloudSync = new CloudSync.Builder()
.setContext(context)
.build();
try {
cloudSync.syncScenarioRun(scenarioRunId);
return Result.success();
} catch (MsalException e) {
Log.d(TAG, "doWork.MsalException: " + e.getMessage());
TelemetryObject.TrackError(context, e, TAG, doWork().toString());
return Result.retry();
} catch (InterruptedException e) {
Log.d(TAG, "doWork.InterruptedException: " + e.getMessage());
TelemetryObject.TrackError(context, e, TAG, doWork().toString());
return Result.retry();
} catch (IOException e) {
Log.d(TAG, "doWork.IOException: " + e.getMessage());
TelemetryObject.TrackError(context, e, TAG, doWork().toString());
return Result.retry();
} catch (WebApiException e) {
Log.d(TAG, "doWork.WebApiException: " + e.getMessage());
TelemetryObject.TrackError(context, e, TAG, doWork().toString());
return Result.retry();
}
}
根据文档,我正在调用的 Results.retry() 应该触发我指定的退避退避行为,但事实并非如此:
在此示例中,最小退避延迟设置为最小值 允许值,10 秒。由于策略是线性的,因此重试 每个新的间隔将增加大约 10 秒 试图。例如,第一次运行以 Result.retry() 结束 将在 10 秒后再次尝试,然后是 20、30、40 和 依此类推,如果后续工作继续返回Result.retry() 尝试。如果退避策略设置为 EXPONENTIAL,则重试 持续时间序列将更接近 20、40、80 等。
这让我有点疯狂,因为我需要安排多个这样的对象,但如果它太吵闹而且没有重试限制,我就无法安排这些对象,所以我需要 getRunAttemptCount 才能工作。我真的被困住了。不知道是我操作错误还是有bug。我一遍又一遍地检查,但没有太多代码可以真正检查。
更新
我创建了一种单独测试工作线程的方法,并且 BackoffCriteria 和 getRunAttemptCount 正在工作。
2022-03-08 07:46:55.515 doWork.workerTest: attempt=0 time=2022.03.08 AD at 07:46:55 EST
2022-03-08 07:47:05.573 doWork.workerTest: attempt=1 time=2022.03.08 AD at 07:47:05 EST
2022-03-08 07:47:25.618 doWork.workerTest: attempt=2 time=2022.03.08 AD at 07:47:25 EST
2022-03-08 07:48:05.679 doWork.workerTest: attempt=3 time=2022.03.08 AD at 07:48:05 EST
这意味着一定是我正在做的工作中的某些事情导致了问题。我正在从 Room 数据库获取数据,调用带改造的 webapi,并更新数据库中的数据。
一定和线程有关。
I am using WorkMangaer and setting a backoffCriteria, but when I run it the backoff policy does not seem to work and the getRunAttemptCount does not increase:
Here are the results in the logcat, which are not happening every 10 seconds or according to the backoff policy I specified:
2022-03-05 12:11:56.705 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:56.787 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:56.787 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:56.872 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:56.872 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:56.952 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:56.952 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:57.032 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:57.032 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:57.116 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:57.116 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:57.206 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:57.206 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:57.295 doWork.IOException: Failed to connect to /10.0.0.5:5001
2022-03-05 12:11:57.295 doWork: for 22f6d39e-7404-4efb-ba13-ccb468fdcb5d attempt 0
2022-03-05 12:11:57.377 doWork.IOException: Failed to connect to /10.0.0.5:5001
Here is the code I am using to enqueue the work. I've tried both LINEAR and EXPONENTIAL with the same result and increased the backoffDelay and used both duration and delay + time unit as specified here:
WorkManager
.getInstance(context)
.cancelAllWorkByTag(sb.toString());
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresCharging(false)
.build();
OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(worker)
.setConstraints(constraints)
.addTag(sb.toString())
.setInputData(data)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL,10, TimeUnit.SECONDS)
.build();
WorkManager
.getInstance(context)
.enqueueUniqueWork(sb.toString(),REPLACE,oneTimeWorkRequest)
.getResult();
Here is the doWork method. The error is getting trapped and I am returning Result.Retry() so:
@NonNull
@Override
public Result doWork() {
//get the id of the scenario
Log.d(TAG, "doWork: for " + getId() + " attempt " + getRunAttemptCount());
int scenarioRunId = getInputData().getInt(INPUT_DATA_SCENARIORUNID, -1);
CloudSync cloudSync = new CloudSync.Builder()
.setContext(context)
.build();
try {
cloudSync.syncScenarioRun(scenarioRunId);
return Result.success();
} catch (MsalException e) {
Log.d(TAG, "doWork.MsalException: " + e.getMessage());
TelemetryObject.TrackError(context, e, TAG, doWork().toString());
return Result.retry();
} catch (InterruptedException e) {
Log.d(TAG, "doWork.InterruptedException: " + e.getMessage());
TelemetryObject.TrackError(context, e, TAG, doWork().toString());
return Result.retry();
} catch (IOException e) {
Log.d(TAG, "doWork.IOException: " + e.getMessage());
TelemetryObject.TrackError(context, e, TAG, doWork().toString());
return Result.retry();
} catch (WebApiException e) {
Log.d(TAG, "doWork.WebApiException: " + e.getMessage());
TelemetryObject.TrackError(context, e, TAG, doWork().toString());
return Result.retry();
}
}
According to the documentation the Results.retry(), which I am calling, should trigger the backoff backoff behavior I specify, but it is not:
In this example, the minimum backoff delay is set to the minimum
allowed value, 10 seconds. Since the policy is LINEAR the retry
interval will increase by approximately 10 seconds with each new
attempt. For instance, the first run finishing with Result.retry()
will be attempted again after 10 seconds, followed by 20, 30, 40, and
so on, if the work continues to return Result.retry() after subsequent
attempts. If the backoff policy were set to EXPONENTIAL, the retry
duration sequence would be closer to 20, 40, 80, and so on.
This is driving me a little crazy because I need to schedule multiple of these objects and can't if it's so chatty and there is no limit to retries, so I need the getRunAttemptCount to work. I'm really stuck. I don't know if I am doing it wrong or there is a bug. I been over and over it, but there is not a lot of code to really check.
UPDATE
I created a way to test the worker alone and the BackoffCriteria and getRunAttemptCount are working.
2022-03-08 07:46:55.515 doWork.workerTest: attempt=0 time=2022.03.08 AD at 07:46:55 EST
2022-03-08 07:47:05.573 doWork.workerTest: attempt=1 time=2022.03.08 AD at 07:47:05 EST
2022-03-08 07:47:25.618 doWork.workerTest: attempt=2 time=2022.03.08 AD at 07:47:25 EST
2022-03-08 07:48:05.679 doWork.workerTest: attempt=3 time=2022.03.08 AD at 07:48:05 EST
which means it must be something in the work I am doing which is causing the problem. I am getting data from a Room database, calling a webapi with retrofit, and updating the data in the database.
Must be something to do with the threading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论