Skip to content

Commit

Permalink
Merge pull request #79 from YAPP-Github/feature/batch-exception
Browse files Browse the repository at this point in the history
refactor: 배치 공통 예외 처리 추출
  • Loading branch information
sooyoungh authored Aug 6, 2023
2 parents 8761a5b + c25cdf4 commit 7affd3d
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 326 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,31 @@
package com.pyonsnalcolor.batch.service.cu;

import com.pyonsnalcolor.exception.PyonsnalcolorBatchException;
import org.jsoup.Jsoup;
import com.pyonsnalcolor.batch.util.BatchExceptionUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.net.SocketTimeoutException;

import static com.pyonsnalcolor.exception.model.BatchErrorCode.*;
import static com.pyonsnalcolor.exception.model.BatchErrorCode.BATCH_UNAVAILABLE;

public interface CuDescriptionBatch {

String CU_DESCRIPTION_PAGE_URL = "https://cu.bgfretail.com/product/view.do";
int TIMEOUT = 15000;

default String getDescription(Element element, String productType) {
String productCode = getProductCode(element);
return BatchExceptionUtil.handleException(() -> fetchDescriptionByProductCode(productType, productCode));
}

try {
String detailPage = UriComponentsBuilder
.fromUriString(CU_DESCRIPTION_PAGE_URL)
.queryParam("category", productType)
.queryParam("gdIdx", productCode)
.build()
.toString();
Document doc = Jsoup.connect(detailPage).timeout(TIMEOUT).get();
Elements elements = doc.select("ul.prodExplain li");
return elements.text();
} catch (IllegalArgumentException e) {
throw new PyonsnalcolorBatchException(INVALID_ACCESS, e);
} catch (SocketTimeoutException e) {
throw new PyonsnalcolorBatchException(TIME_OUT, e);
} catch (IOException e) {
throw new PyonsnalcolorBatchException(IO_EXCEPTION, e);
} catch (Exception e) {
throw new PyonsnalcolorBatchException(BATCH_UNAVAILABLE, e);
}
private String fetchDescriptionByProductCode(String productType, String productCode) {
String detailPage = UriComponentsBuilder
.fromUriString(CU_DESCRIPTION_PAGE_URL)
.queryParam("category", productType)
.queryParam("gdIdx", productCode)
.build()
.toString();
Document document = BatchExceptionUtil.getDocumentByUrlWithTimeout(detailPage, TIMEOUT);
Elements elements = document.select("ul.prodExplain li");
return elements.text();
}

default String getProductCode(Element element) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.pyonsnalcolor.batch.service.cu;

import com.pyonsnalcolor.batch.service.EventBatchService;
import com.pyonsnalcolor.batch.util.BatchExceptionUtil;
import com.pyonsnalcolor.exception.PyonsnalcolorBatchException;
import com.pyonsnalcolor.product.entity.BaseEventProduct;
import com.pyonsnalcolor.product.enumtype.*;
import com.pyonsnalcolor.product.repository.EventProductRepository;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -37,28 +35,17 @@ public CuEventBatchService(EventProductRepository eventProductRepository) {

@Override
protected List<BaseEventProduct> getAllProducts() {

try {
return getProducts();
} catch (IllegalArgumentException e) {
throw new PyonsnalcolorBatchException(INVALID_ACCESS, e);
} catch (SocketTimeoutException e) {
throw new PyonsnalcolorBatchException(TIME_OUT, e);
} catch (IOException e) {
throw new PyonsnalcolorBatchException(IO_EXCEPTION, e);
} catch (Exception e) {
throw new PyonsnalcolorBatchException(BATCH_UNAVAILABLE, e);
}
return BatchExceptionUtil.handleException(this::getProducts);
}

private List<BaseEventProduct> getProducts() throws Exception {
private List<BaseEventProduct> getProducts() {
List<BaseEventProduct> products = new ArrayList<>();

int pageIndex = 1;
while (true) {
String pagedCuEventUrl = getCuEventUrlByPageIndex(pageIndex);
Document doc = Jsoup.connect(pagedCuEventUrl).timeout(TIMEOUT).get();
Elements elements = doc.select(DOC_SELECT_TAG);
Document document = BatchExceptionUtil.getDocumentByUrlWithTimeout(pagedCuEventUrl, TIMEOUT);
Elements elements = document.select(DOC_SELECT_TAG);

if (elements.isEmpty()) {
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package com.pyonsnalcolor.batch.service.cu;

import com.pyonsnalcolor.batch.service.PbBatchService;
import com.pyonsnalcolor.exception.PyonsnalcolorBatchException;
import com.pyonsnalcolor.exception.model.BatchErrorCode;
import com.pyonsnalcolor.batch.util.BatchExceptionUtil;
import com.pyonsnalcolor.product.entity.BasePbProduct;
import com.pyonsnalcolor.product.enumtype.Category;
import com.pyonsnalcolor.product.enumtype.Filter;
import com.pyonsnalcolor.product.enumtype.Recommend;
import com.pyonsnalcolor.product.enumtype.StoreType;
import com.pyonsnalcolor.product.repository.PbProductRepository;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -42,35 +38,24 @@ public CuPbBatchService(PbProductRepository pbProductRepository) {

@Override
protected List<BasePbProduct> getAllProducts() {
try {
return getProductsByCategoryAll();
} catch (IllegalArgumentException e) {
throw new PyonsnalcolorBatchException(BatchErrorCode.INVALID_ACCESS, e);
} catch (SocketTimeoutException e) {
throw new PyonsnalcolorBatchException(BatchErrorCode.TIME_OUT, e);
} catch (IOException e) {
throw new PyonsnalcolorBatchException(BatchErrorCode.IO_EXCEPTION, e);
} catch (Exception e) {
throw new PyonsnalcolorBatchException(BatchErrorCode.BATCH_UNAVAILABLE, e);
}
return BatchExceptionUtil.handleException(this::getProductsByCategoryAll);
}

private List<BasePbProduct> getProductsByCategoryAll() throws Exception {
private List<BasePbProduct> getProductsByCategoryAll() {
List<BasePbProduct> products = new ArrayList<>();
products.addAll(getProductsByCategory(CU_CATEGORY_PB));
products.addAll(getProductsByCategory(CU_CATEGORY_CU_ONLY));
return products;
}

private List<BasePbProduct> getProductsByCategory(String category) throws Exception {
private List<BasePbProduct> getProductsByCategory(String category) {
List<BasePbProduct> products = new ArrayList<>();

int pageIndex = 1;
while (true) {
String pagedCuPbUrl = getCuPbUrlByPageIndexAndCategory(pageIndex, category);
Document doc = Jsoup.connect(pagedCuPbUrl).timeout(TIMEOUT).get();
Elements elements = doc.select(DOC_SELECT_TAG);

Document document = BatchExceptionUtil.getDocumentByUrlWithTimeout(pagedCuPbUrl, TIMEOUT);
Elements elements = document.select(DOC_SELECT_TAG);
if (elements.isEmpty()) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pyonsnalcolor.batch.service.cu;

import com.pyonsnalcolor.batch.service.PromotionBatchService;
import com.pyonsnalcolor.batch.util.BatchExceptionUtil;
import com.pyonsnalcolor.exception.PyonsnalcolorBatchException;
import com.pyonsnalcolor.product.enumtype.StoreType;
import com.pyonsnalcolor.promotion.entity.Promotion;
Expand Down Expand Up @@ -41,28 +42,18 @@ public CuPromotionBatchService(PromotionRepository promotionRepository) {

@Override
public List<Promotion> getNewPromotions() {
try {
return getPromotions();
} catch (IllegalArgumentException e) {
throw new PyonsnalcolorBatchException(INVALID_ACCESS, e);
} catch (SocketTimeoutException e) {
throw new PyonsnalcolorBatchException(TIME_OUT, e);
} catch (IOException e) {
throw new PyonsnalcolorBatchException(IO_EXCEPTION, e);
} catch (Exception e) {
throw new PyonsnalcolorBatchException(BATCH_UNAVAILABLE, e);
}
return BatchExceptionUtil.handleException(this::getPromotions);
}

private List<Promotion> getPromotions() throws IOException {
private List<Promotion> getPromotions() {
List<Promotion> promotions = new ArrayList<>();

int pageIndex = 1;
while (true) {
String pagedCuEventUrl = getPromotion(pageIndex);
Document doc = Jsoup.connect(pagedCuEventUrl).timeout(TIMEOUT).get();
Elements elements = doc.select(DOC_SELECT_TAG);
Elements finishedElements = doc.select(LAST_SELECT_TAG);
Document document = BatchExceptionUtil.getDocumentByUrlWithTimeout(pagedCuEventUrl, TIMEOUT);
Elements elements = document.select(DOC_SELECT_TAG);
Elements finishedElements = document.select(LAST_SELECT_TAG);
String isPromotionNotExist = finishedElements.text();
if (isPromotionNotExist.contains(LAST_MENT)) {
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package com.pyonsnalcolor.batch.service.emart24;

import com.pyonsnalcolor.batch.service.EventBatchService;
import com.pyonsnalcolor.batch.util.BatchExceptionUtil;
import com.pyonsnalcolor.product.entity.BaseEventProduct;
import com.pyonsnalcolor.product.enumtype.*;
import com.pyonsnalcolor.product.repository.EventProductRepository;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.pyonsnalcolor.product.entity.UUIDGenerator.generateId;

@Service("Emart24Event")
@Slf4j
public class Emart24EventBatchService extends EventBatchService {

private static final String EMART_EVENT_URL_TEMPLATE = "http://www.emart24.co.kr/goods/event?search=&page=%s&category_seq=&align=";
private static final int TIMEOUT = 20000;

@Autowired
public Emart24EventBatchService(EventProductRepository eventProductRepository) {
Expand All @@ -30,26 +31,23 @@ public Emart24EventBatchService(EventProductRepository eventProductRepository) {

@Override
protected List<BaseEventProduct> getAllProducts() {
try {
List<BaseEventProduct> results = new ArrayList<>();
Elements productElements;
int curPage = 1;

do {
String url = String.format(EMART_EVENT_URL_TEMPLATE, curPage);
Document document = Jsoup.connect(url).get();
productElements = document.getElementsByClass("itemWrap");

results.addAll(parseProductsData(productElements));
curPage++;
} while (productElements.size() > 0);

return results;
} catch (Exception e) {
//TODO : 임시로 모든 예외에 대해 퉁쳐서 처리. 후에 리팩토링 진행할 것
log.error("fail getAllProducts", e);
}
return Collections.emptyList();
return BatchExceptionUtil.handleException(this::getProducts);
}

private List<BaseEventProduct> getProducts() {
List<BaseEventProduct> results = new ArrayList<>();
Elements productElements;
int curPage = 1;
do {
String url = String.format(EMART_EVENT_URL_TEMPLATE, curPage);
Document document = BatchExceptionUtil.getDocumentByUrlWithTimeout(url, TIMEOUT);
productElements = document.getElementsByClass("itemWrap");

results.addAll(parseProductsData(productElements));
curPage++;
} while (productElements.size() > 0);

return results;
}

private List<BaseEventProduct> parseProductsData(Elements elements) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.pyonsnalcolor.batch.service.emart24;

import com.pyonsnalcolor.batch.service.PbBatchService;
import com.pyonsnalcolor.batch.util.BatchExceptionUtil;
import com.pyonsnalcolor.product.entity.BasePbProduct;
import com.pyonsnalcolor.product.enumtype.Category;
import com.pyonsnalcolor.product.enumtype.Filter;
import com.pyonsnalcolor.product.enumtype.StoreType;
import com.pyonsnalcolor.product.enumtype.Recommend;
import com.pyonsnalcolor.product.repository.PbProductRepository;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.pyonsnalcolor.product.entity.UUIDGenerator.generateId;
Expand All @@ -25,6 +24,7 @@
@Slf4j
public class Emart24PbBatchService extends PbBatchService {
private static final String EMART_PB_URL_TEMPLATE = "http://www.emart24.co.kr/goods/pl?search=&page=%s&category_seq=&align=";
private static final int TIMEOUT = 20000;

@Autowired
public Emart24PbBatchService(PbProductRepository pbProductRepository) {
Expand All @@ -33,26 +33,24 @@ public Emart24PbBatchService(PbProductRepository pbProductRepository) {

@Override
protected List<BasePbProduct> getAllProducts() {
try {
List<BasePbProduct> results = new ArrayList<>();
Elements productElements;
int curPage = 1;
return BatchExceptionUtil.handleException(this::getProducts);
}

do {
String url = String.format(EMART_PB_URL_TEMPLATE, curPage);
Document document = Jsoup.connect(url).get();
productElements = document.getElementsByClass("itemWrap");
private List<BasePbProduct> getProducts() {
List<BasePbProduct> results = new ArrayList<>();
Elements productElements;
int curPage = 1;

results.addAll(parseProductsData(productElements));
curPage++;
} while (productElements.size() > 0);
do {
String url = String.format(EMART_PB_URL_TEMPLATE, curPage);
Document document = BatchExceptionUtil.getDocumentByUrlWithTimeout(url, TIMEOUT);
productElements = document.getElementsByClass("itemWrap");

return results;
} catch (Exception e) {
//TODO : 임시로 모든 예외에 대해 퉁쳐서 처리. 후에 리팩토링 진행할 것
log.error("fail getAllProducts", e);
}
return Collections.emptyList();
results.addAll(parseProductsData(productElements));
curPage++;
} while (productElements.size() > 0);

return results;
}

private List<BasePbProduct> parseProductsData(Elements productElements) {
Expand Down
Loading

0 comments on commit 7affd3d

Please sign in to comment.