refactor: improve image fetching with retry mechanism and error handling
This commit is contained in:
parent
420b522edf
commit
3c8038041b
@ -1,7 +1,5 @@
|
||||
package com.magamochi.image.queue.consumer;
|
||||
|
||||
import static java.util.Objects.nonNull;
|
||||
|
||||
import com.magamochi.common.queue.command.ImageFetchCommand;
|
||||
import com.magamochi.common.queue.command.ImageUpdateCommand;
|
||||
import com.magamochi.image.queue.producer.ImageUpdateProducer;
|
||||
@ -22,10 +20,13 @@ public class ImageFetchConsumer {
|
||||
public void receiveImageFetchCommand(ImageFetchCommand command) {
|
||||
log.info("Received image fetch command: {}", command);
|
||||
|
||||
try {
|
||||
var imageId = imageFetchService.fetchImage(command.url(), command.contentType());
|
||||
if (nonNull(imageId)) {
|
||||
|
||||
imageUpdateProducer.publishImageUpdateCommand(
|
||||
new ImageUpdateCommand(command.entityId(), imageId), command.contentType());
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to fetch image from URL.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ import static java.util.Objects.nonNull;
|
||||
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import com.magamochi.common.model.enumeration.ContentType;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
@ -14,6 +15,7 @@ import java.util.UUID;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.tika.Tika;
|
||||
import org.springframework.resilience.annotation.Retryable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Log4j2
|
||||
@ -28,8 +30,15 @@ public class ImageFetchService {
|
||||
HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build();
|
||||
private final Tika tika = new Tika();
|
||||
|
||||
public UUID fetchImage(String imageUrl, ContentType contentType) {
|
||||
try {
|
||||
@Retryable(
|
||||
maxRetries = 5,
|
||||
delay = 1000,
|
||||
multiplier = 2,
|
||||
maxDelay = 5000,
|
||||
includes = Exception.class,
|
||||
excludes = NoSuchAlgorithmException.class)
|
||||
public UUID fetchImage(String imageUrl, ContentType contentType)
|
||||
throws IOException, InterruptedException, NoSuchAlgorithmException {
|
||||
var request = HttpRequest.newBuilder(URI.create(imageUrl.trim())).GET().build();
|
||||
|
||||
imageDownloadRateLimiter.acquire();
|
||||
@ -38,10 +47,6 @@ public class ImageFetchService {
|
||||
var imageBytes = response.body();
|
||||
|
||||
return uploadImage(imageBytes, response, contentType);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to fetch image from URL: {}", imageUrl, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public UUID uploadImage(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user