Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fail to delete multiple files if the files are removed from upstream storage #2317

Merged
merged 7 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void delete(String key) {

try {
Response response = bucketManager.delete(bucket, key);
if (!response.isOK()) {
if (!response.isOK() && response.statusCode != 404) {
JohnNiang marked this conversation as resolved.
Show resolved Hide resolved
log.warn("附件 " + key + " 从七牛云删除失败");
JackyLiang522 marked this conversation as resolved.
Show resolved Hide resolved
throw new FileOperationException("附件 " + key + " 从七牛云删除失败");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果删除了异常catch,那么可能因为网络问题而导致删除失败时,halo中的数据库记录却被删除了的问题从而导致文件遗留。
建议先查询一次来判断 bucket 中是否存在文件,如果不存在则直接跳到删除数据库记录的逻辑,否则不需要改变原来的逻辑,用户可以通过手动重试来解决

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来的代码里,删除附件时会先在数据库里删除后向云端发起删除请求,所以就算抛出了异常还是会有文件遗失问题。如果想要通过抛出异常防止文件遗失,可能要先将这个顺序改过来?

Copy link
Member

@guqing guqing Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来的代码里,删除附件时会先在数据库里删除后向云端发起删除请求,所以就算抛出了异常还是会有文件遗失问题。如果想要通过抛出异常防止文件遗失,可能要先将这个顺序改过来?

那对于这个方法可以在进入时查询一下oss bucket中该文件是否存在,不存在直接return即可,存在再删除,这样就不需要去改原来的顺序问题了,因为issue的问题就是通过其他途径删除了bucket中的该文件导致抛出异常,如果文件不存在直接return即只有数据库被删除是合理的

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void delete(String key) {

try {
Response result = manager.deleteFile(key, null);
if (!result.isSuccessful()) {
if (!result.isSuccessful() && result.code() != 404) {
JohnNiang marked this conversation as resolved.
Show resolved Hide resolved
log.warn("附件 " + key + " 从又拍云删除失败");
JackyLiang522 marked this conversation as resolved.
Show resolved Hide resolved
throw new FileOperationException("附件 " + key + " 从又拍云删除失败");
}
Expand Down