in src/main/java/com/aliyun/cloudops/acs/ecs/toolkit/CloudAssistant.java [299:334]
public void run() {
try {
this.queryTimes++;
DescribeInvocationsResponse response = acsClient.sendRequest(request);
if (CollectionUtils.isEmpty(response.getInvocations())) {
logger.error("query task {}-[{}], not found.", request.getInvokeId(), queryTimes);
predicate.test(null); // 提供的入参有误,请检查 regionId与InvokeId
return;
}
Invocation invocation = response.getInvocations().get(0);
if (this.predicate.test(invocation) || isFinished(invocation.getInvocationStatus())) {
return;
}
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date creationTime = format.parse(invocation.getCreationTime()); // GMT 时间
long startTime = creationTime.getTime() + TimeZone.getDefault().getRawOffset();
long finalTime = startTime + 1000 * invocation.getTimeout();
// 计算当前时间与最晚结束时间的差值,每对过一半再查,避免产生过量无效查询
long nextStep = (finalTime - System.currentTimeMillis()) / 2;
nextStep = Math.max(taskQueryDelay * 1000L, nextStep); // 防止负数或间隔过小
// 任务创建之后的第60秒,是任务下发的最晚截止时间点。在此时间查询一次可尽早发现可能的失败
long checkPoint = startTime + 1000 * Math.min(60, invocation.getTimeout());
long offsetTime = checkPoint - System.currentTimeMillis();
if (0 < offsetTime && offsetTime < nextStep) {
nextStep = offsetTime; // 下一次查询时间,不要跳过CheckPoint
}
long during = (System.currentTimeMillis() - startTime) / 1000;
logger.info("query task {}-[{}]+{}s, next: {}ms.", request.getInvokeId(), queryTimes, during, nextStep);
threadPoolExecutor.schedule(this, nextStep, TimeUnit.MILLISECONDS);
} catch (Exception e) {
logger.warn("query task {}-[{}], error: {}.", request.getInvokeId(), queryTimes, e, e);
threadPoolExecutor.schedule(this, 1, TimeUnit.SECONDS);
}
}