Skip to content

Commit

Permalink
Merge pull request #3 from algarves/feature/recall
Browse files Browse the repository at this point in the history
Ajuste no tratamento de exceções e retentativas.
  • Loading branch information
algarves authored Mar 10, 2021
2 parents 7ef2596 + 5866fab commit 0349f74
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ script:
- "./gradlew check"
- "./gradlew jacocoTestReport"
- sonar-scanner
- "./gradlew publish"

cache:
directories:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'com.ueby'
version '2.1-SNAPSHOT'
version '2.3-SNAPSHOT'

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -68,7 +69,7 @@ public CaixaWebCrawlerService byContestNumber(final int concourse) {
* @throws InterruptedException When the process is interrupted.
* @throws CaixaWebCrawlerException If Number of attempts exhausted.
*/
private void recall() throws InterruptedException, CaixaWebCrawlerException {
private void recall() throws InterruptedException {
if (attempts <= MAX_ATTEMPTS) {
long threadSleep = getSleep();

Expand All @@ -79,7 +80,7 @@ private void recall() throws InterruptedException, CaixaWebCrawlerException {
call();
attempts++;
} else {
throw new CaixaWebCrawlerException("Oops! Number of attempts exhausted.");
throw new InterruptedException("Oops! Number of attempts exhausted.");
}
}

Expand All @@ -102,33 +103,33 @@ private void call() {
log.debug("Response status - {}", responseStatus.getStatusCode());

if (HttpStatus.SC_OK == responseStatus.getStatusCode()) {
if (html != null) {
checkContent(html);
} else {
recall();
}
checkContent(html);
} else {
log.error("[" + responseStatus.getStatusCode() + "] - {} ",
throw new CaixaWebCrawlerException("[" + responseStatus.getStatusCode() + "] - " +
responseStatus.getReasonPhrase());
recall();
}
} catch (CaixaWebCrawlerException e) {
log.error(e.getLocalizedMessage(), e);
} catch (Exception e) {
log.fatal(e.getLocalizedMessage(), e);
try {
recall();
} catch (InterruptedException ie) {
log.fatal(ie.getLocalizedMessage(), ie);
Thread.currentThread().interrupt();
}
} catch (IOException | URISyntaxException ex) {
log.error(ex.getLocalizedMessage(), ex);
}
}

private void checkContent(String html) throws InterruptedException, CaixaWebCrawlerException {
private void checkContent(String html) throws CaixaWebCrawlerException {
Document doc = Jsoup.parse(html);
Element link = doc.getElementById(HTML_ELEMENT_FIRST);
Element input = doc.select(HTML_ELEMENT_SECOND).first();

if (firstRequest) {
checkContentFirstRequest(link, input);
} else if (html.startsWith(HTML_DOCTYPE)) {
log.error("Oops! No valid content.");
recall();
throw new CaixaWebCrawlerException("Oops! No valid content.");
}

if (!html.startsWith(HTML_DOCTYPE)) {
Expand All @@ -138,21 +139,19 @@ private void checkContent(String html) throws InterruptedException, CaixaWebCraw
firstRequest = false;
}

private void checkContentFirstRequest(Element link, Element input) throws InterruptedException, CaixaWebCrawlerException {
private void checkContentFirstRequest(Element link, Element input) throws CaixaWebCrawlerException {
if (link != null) {
String linkHref = link.attr("href");
this.caixaCrawlerStub.setLinkHref(linkHref);
} else {
log.error("Oops! I couldn't find the first element '" + HTML_ELEMENT_FIRST + "'.");
recall();
throw new CaixaWebCrawlerException("Oops! I couldn't find the first element '" + HTML_ELEMENT_FIRST + "'.");
}

if (input != null) {
String inputValue = input.attr("value");
this.caixaCrawlerStub.setUrlBuscarResultado(inputValue);
} else {
log.error("Oops! I couldn't find the second element '" + HTML_ELEMENT_SECOND + "'.");
recall();
throw new CaixaWebCrawlerException("Oops! I couldn't find the second element '" + HTML_ELEMENT_SECOND + "'.");
}
}

Expand Down

0 comments on commit 0349f74

Please sign in to comment.