feat: add retry operation for post and single page when publish and update (#2497)

#### What type of PR is this?
/kind improvement
/area core
/milestone 2.0

#### What this PR does / why we need it:
发布和更新文章及自定义页面时如果遇到乐观锁错误自动重试
#### Which issue(s) this PR fixes:

Fixes #

#### Special notes for your reviewer:
/cc @halo-dev/sig-halo 
#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/2499/head
guqing 2022-09-30 17:40:23 +08:00 committed by GitHub
parent 3995adba32
commit 470696fff1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package run.halo.app.content.impl;
import static run.halo.app.extension.router.selector.SelectorUtil.labelAndFieldSelectorToPredicate;
import java.security.Principal;
import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.Comparator;
@ -11,12 +12,14 @@ import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;
import run.halo.app.content.ContentService;
import run.halo.app.content.Contributor;
import run.halo.app.content.ListedPost;
@ -239,6 +242,8 @@ public class PostServiceImpl implements PostService {
post.getSpec().setHeadSnapshot(contentWrapper.snapshotName());
return client.update(post);
})
.retryWhen(Retry.backoff(5, Duration.ofMillis(100))
.filter(throwable -> throwable instanceof OptimisticLockingFailureException))
.then(Mono.defer(() -> client.fetch(Post.class, post.getMetadata().getName())));
}
@ -285,7 +290,9 @@ public class PostServiceImpl implements PostService {
return client.update(post);
})
.then(Mono.defer(() -> client.fetch(Post.class, postName)));
});
})
.retryWhen(Retry.backoff(5, Duration.ofMillis(100))
.filter(throwable -> throwable instanceof OptimisticLockingFailureException));
}
void appendPublishedCondition(Post post, Post.PostPhase phase) {

View File

@ -3,6 +3,7 @@ package run.halo.app.content.impl;
import static run.halo.app.extension.router.selector.SelectorUtil.labelAndFieldSelectorToPredicate;
import java.security.Principal;
import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.Comparator;
@ -11,12 +12,14 @@ import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;
import run.halo.app.content.ContentService;
import run.halo.app.content.Contributor;
import run.halo.app.content.ListedSinglePage;
@ -101,6 +104,8 @@ public class SinglePageServiceImpl implements SinglePageService {
page.getSpec().setHeadSnapshot(contentWrapper.snapshotName());
return client.update(page);
})
.retryWhen(Retry.backoff(5, Duration.ofMillis(100))
.filter(throwable -> throwable instanceof OptimisticLockingFailureException))
.then(Mono.defer(() -> client.fetch(SinglePage.class, page.getMetadata().getName())));
}
@ -130,7 +135,9 @@ public class SinglePageServiceImpl implements SinglePageService {
return client.update(page);
})
.then(Mono.defer(() -> client.fetch(SinglePage.class, name)));
});
})
.retryWhen(Retry.backoff(5, Duration.ofMillis(100))
.filter(throwable -> throwable instanceof OptimisticLockingFailureException));
}
private Mono<String> getContextUsername() {