mirror of https://github.com/halo-dev/halo
fix: update post with retry (#5823)
#### What type of PR is this? /kind improvement /area core /milestone 2.15.x #### What this PR does / why we need it: 修复重试更新文章的错误写法 #### Does this PR introduce a user-facing change? ```release-note None ```pull/5838/head
parent
966558d1ce
commit
5770ad4c55
|
@ -7,6 +7,7 @@ import java.time.Instant;
|
|||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.UnaryOperator;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.dao.OptimisticLockingFailureException;
|
||||
|
@ -328,9 +329,11 @@ public class PostServiceImpl extends AbstractContentService implements PostServi
|
|||
.flatMap(post -> {
|
||||
var headSnapshotName = post.getSpec().getHeadSnapshot();
|
||||
if (StringUtils.equals(headSnapshotName, snapshotName)) {
|
||||
// update head to release
|
||||
post.getSpec().setHeadSnapshot(post.getSpec().getReleaseSnapshot());
|
||||
return updatePostWithRetry(post);
|
||||
return updatePostWithRetry(post, record -> {
|
||||
// update head to release
|
||||
record.getSpec().setHeadSnapshot(record.getSpec().getReleaseSnapshot());
|
||||
return record;
|
||||
});
|
||||
}
|
||||
return Mono.just(post);
|
||||
})
|
||||
|
@ -352,14 +355,15 @@ public class PostServiceImpl extends AbstractContentService implements PostServi
|
|||
});
|
||||
}
|
||||
|
||||
private Mono<Post> updatePostWithRetry(Post post) {
|
||||
return client.update(post)
|
||||
private Mono<Post> updatePostWithRetry(Post post, UnaryOperator<Post> func) {
|
||||
return client.update(func.apply(post))
|
||||
.onErrorResume(OptimisticLockingFailureException.class,
|
||||
e -> Mono.defer(() -> client.get(Post.class, post.getMetadata().getName())
|
||||
.flatMap(client::update))
|
||||
.retryWhen(Retry.backoff(8, Duration.ofMillis(100))
|
||||
.filter(OptimisticLockingFailureException.class::isInstance)
|
||||
.map(func)
|
||||
.flatMap(client::update)
|
||||
)
|
||||
.retryWhen(Retry.backoff(8, Duration.ofMillis(100))
|
||||
.filter(OptimisticLockingFailureException.class::isInstance))
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.time.Instant;
|
|||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.UnaryOperator;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.dao.OptimisticLockingFailureException;
|
||||
|
@ -206,9 +207,11 @@ public class SinglePageServiceImpl extends AbstractContentService implements Sin
|
|||
.flatMap(page -> {
|
||||
var headSnapshotName = page.getSpec().getHeadSnapshot();
|
||||
if (StringUtils.equals(headSnapshotName, snapshotName)) {
|
||||
// update head to release
|
||||
page.getSpec().setHeadSnapshot(page.getSpec().getReleaseSnapshot());
|
||||
return updatePostWithRetry(page);
|
||||
return updatePageWithRetry(page, record -> {
|
||||
// update head to release
|
||||
page.getSpec().setHeadSnapshot(page.getSpec().getReleaseSnapshot());
|
||||
return record;
|
||||
});
|
||||
}
|
||||
return Mono.just(page);
|
||||
})
|
||||
|
@ -230,14 +233,15 @@ public class SinglePageServiceImpl extends AbstractContentService implements Sin
|
|||
});
|
||||
}
|
||||
|
||||
private Mono<SinglePage> updatePostWithRetry(SinglePage page) {
|
||||
return client.update(page)
|
||||
private Mono<SinglePage> updatePageWithRetry(SinglePage page, UnaryOperator<SinglePage> func) {
|
||||
return client.update(func.apply(page))
|
||||
.onErrorResume(OptimisticLockingFailureException.class,
|
||||
e -> Mono.defer(() -> client.get(SinglePage.class, page.getMetadata().getName())
|
||||
.flatMap(client::update))
|
||||
.retryWhen(Retry.backoff(8, Duration.ofMillis(100))
|
||||
.filter(OptimisticLockingFailureException.class::isInstance)
|
||||
.map(func)
|
||||
.flatMap(client::update)
|
||||
)
|
||||
.retryWhen(Retry.backoff(8, Duration.ofMillis(100))
|
||||
.filter(OptimisticLockingFailureException.class::isInstance))
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue