fix torgi.gov infinite failing
This commit is contained in:
@@ -47,7 +47,7 @@ public class DownloadHeadlessController {
|
|||||||
private void addNewToDiff() {
|
private void addNewToDiff() {
|
||||||
logger.error("startProcessing");
|
logger.error("startProcessing");
|
||||||
DownloadHeadlessService downloadHeadlessService = new DownloadHeadlessService();
|
DownloadHeadlessService downloadHeadlessService = new DownloadHeadlessService();
|
||||||
Map<String, Lot> currentContent = downloadHeadlessService.downloadExcel();
|
Map<String, Lot> currentContent = downloadHeadlessService.downloadExcel(0);
|
||||||
List<TorgiEvent> newEvents = new ArrayList<>();
|
List<TorgiEvent> newEvents = new ArrayList<>();
|
||||||
|
|
||||||
for (String key : currentContent.keySet()) {
|
for (String key : currentContent.keySet()) {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.pledge.torgi.model.TorgiEvent;
|
import com.pledge.torgi.model.TorgiEvent;
|
||||||
import com.pledge.torgi.repositories.TorgiEventRepository;
|
import com.pledge.torgi.repositories.TorgiEventRepository;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
@@ -38,7 +39,8 @@ public class DownloadHeadlessService {
|
|||||||
Pattern cadastralPattern = Pattern.compile("\\d{1,2}:\\d{1,2}:\\d{1,7}:\\d{1,9}");
|
Pattern cadastralPattern = Pattern.compile("\\d{1,2}:\\d{1,2}:\\d{1,7}:\\d{1,9}");
|
||||||
Pattern vinPattern = Pattern.compile("\\b[A-HJ-NPR-Z0-9-]{12,17}\\b");
|
Pattern vinPattern = Pattern.compile("\\b[A-HJ-NPR-Z0-9-]{12,17}\\b");
|
||||||
|
|
||||||
public Map<String, Lot> downloadExcel() {
|
@SneakyThrows
|
||||||
|
public Map<String, Lot> downloadExcel(int retryCount) {
|
||||||
try {
|
try {
|
||||||
HttpURLConnection connection = (HttpURLConnection) new URL(EXCEL_LINK).openConnection();
|
HttpURLConnection connection = (HttpURLConnection) new URL(EXCEL_LINK).openConnection();
|
||||||
connection.setRequestMethod("GET");
|
connection.setRequestMethod("GET");
|
||||||
@@ -64,12 +66,17 @@ public class DownloadHeadlessService {
|
|||||||
return data;
|
return data;
|
||||||
} else {
|
} else {
|
||||||
logger.error("Error in line 99 ");
|
logger.error("Error in line 99 ");
|
||||||
return downloadExcel();
|
Thread.sleep(1000);
|
||||||
|
return downloadExcel(retryCount + 1);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error in line 103 " + e);
|
logger.error("Error in line 103 " + e);
|
||||||
|
if (retryCount > 10) {
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return downloadExcel();
|
Thread.sleep(1000);
|
||||||
|
return downloadExcel(retryCount + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Lot extractDataFromRow(Row row, String cadastral) {
|
private Lot extractDataFromRow(Row row, String cadastral) {
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ import com.pledge.torgi.model.Emails;
|
|||||||
import com.pledge.torgi.repositories.EmailsRepository;
|
import com.pledge.torgi.repositories.EmailsRepository;
|
||||||
import com.pledge.torgi.utils.StartParams;
|
import com.pledge.torgi.utils.StartParams;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class EmailService {
|
public class EmailService {
|
||||||
@@ -18,16 +20,21 @@ public class EmailService {
|
|||||||
private final EmailsRepository emailsRepository;
|
private final EmailsRepository emailsRepository;
|
||||||
|
|
||||||
public void processEmails(StartParams params) {
|
public void processEmails(StartParams params) {
|
||||||
|
log.error("Start email processing");
|
||||||
List<Emails> alreadyProcessed = emailsRepository.findTop10ByOrderByIdDesc();
|
List<Emails> alreadyProcessed = emailsRepository.findTop10ByOrderByIdDesc();
|
||||||
List<EmailData> emailData = new ReceiveEmailService("imap.gmail.com", 993, params.getFrom(), params.getToken(), params.getSpecialEmail())
|
List<EmailData> emailData = new ReceiveEmailService("imap.gmail.com", 993, params.getFrom(), params.getToken(), params.getSpecialEmail())
|
||||||
.downloadEmails(alreadyProcessed);
|
.downloadEmails(alreadyProcessed);
|
||||||
|
int sendLetters = 0;
|
||||||
|
log.error("Extracted letters = {}", emailData.size());
|
||||||
for (EmailData data : emailData) {
|
for (EmailData data : emailData) {
|
||||||
if (alreadyProcessed(data.getEmailId())) {
|
if (alreadyProcessed(data.getEmailId())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
fileService.processSync(data.getFile(), data.getEmails());
|
fileService.processSync(data.getFile(), data.getEmails());
|
||||||
saveProcessedEmail(data);
|
saveProcessedEmail(data);
|
||||||
|
sendLetters++;
|
||||||
}
|
}
|
||||||
|
log.error("End email processing, send letters = {}", sendLetters);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean alreadyProcessed(String emailId) {
|
private boolean alreadyProcessed(String emailId) {
|
||||||
|
|||||||
@@ -49,19 +49,22 @@ public class ReceiveEmailService {
|
|||||||
Message[] messages = folderInbox.getMessages(messageCount - 50, messageCount);
|
Message[] messages = folderInbox.getMessages(messageCount - 50, messageCount);
|
||||||
|
|
||||||
for (int i = 0; i < messages.length; i++) {
|
for (int i = 0; i < messages.length; i++) {
|
||||||
Message msg = messages[i];
|
|
||||||
Address[] toList = msg.getRecipients(Message.RecipientType.TO);
|
|
||||||
boolean specialAddress = Arrays.stream(toList).anyMatch(addr -> ((InternetAddress) addr).getAddress().equals(specialEmail));
|
|
||||||
if (!specialAddress) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String messageID = ((IMAPMessage) msg).getMessageID();
|
|
||||||
if (alreadyProcessed.stream().anyMatch(processed -> processed.getMailId().equals(messageID))) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
Message msg = messages[i];
|
||||||
|
Address[] toList = msg.getRecipients(Message.RecipientType.TO);
|
||||||
|
if (toList == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
boolean specialAddress = Arrays.stream(toList).anyMatch(addr -> ((InternetAddress) addr).getAddress().equals(specialEmail));
|
||||||
|
if (!specialAddress) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String messageID = ((IMAPMessage) msg).getMessageID();
|
||||||
|
if (alreadyProcessed.stream().anyMatch(processed -> processed.getMailId().equals(messageID))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
MimeMultipart content = (MimeMultipart) msg.getDataHandler().getContent();
|
MimeMultipart content = (MimeMultipart) msg.getDataHandler().getContent();
|
||||||
if (content.getCount() == 1 || !Part.ATTACHMENT.equalsIgnoreCase(content.getBodyPart(1).getDisposition())) {
|
if (content.getCount() == 1 || !Part.ATTACHMENT.equalsIgnoreCase(content.getBodyPart(1).getDisposition())) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user