event logging toegevoegd
This commit is contained in:
75
src/main/java/nl/veenm/jobfindr/domain/Event.java
Normal file
75
src/main/java/nl/veenm/jobfindr/domain/Event.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package nl.veenm.jobfindr.domain;
|
||||
|
||||
public class Event {
|
||||
private String timestamp;
|
||||
private String className;
|
||||
private String methodName;
|
||||
private String message;
|
||||
private String level;
|
||||
private String error;
|
||||
|
||||
public Event(String timestamp, String className, String methodName, String message, String level) {
|
||||
this.timestamp = timestamp;
|
||||
this.className = className;
|
||||
this.methodName = methodName;
|
||||
this.message = message;
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public Event(String timestamp, String className, String methodName, String message, String level, String error) {
|
||||
this.timestamp = timestamp;
|
||||
this.className = className;
|
||||
this.methodName = methodName;
|
||||
this.message = message;
|
||||
this.level = level;
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public String getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(String timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public String getClassName() {
|
||||
return className;
|
||||
}
|
||||
|
||||
public void setClassName(String className) {
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
public String getMethodName() {
|
||||
return methodName;
|
||||
}
|
||||
|
||||
public void setMethodName(String methodName) {
|
||||
this.methodName = methodName;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(String level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
public void setError(String error) {
|
||||
this.error = error;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,9 @@ package nl.veenm.jobfindr.services;
|
||||
import io.quarkus.mailer.Mail;
|
||||
import io.quarkus.mailer.Mailer;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import nl.veenm.jobfindr.domain.Vacature;
|
||||
import nl.veenm.jobfindr.util.EventService;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@@ -12,6 +14,11 @@ import java.util.List;
|
||||
@ApplicationScoped
|
||||
public class EmailService {
|
||||
|
||||
@Inject
|
||||
EventService eventService;
|
||||
|
||||
private final String className = this.getClass().getSimpleName();
|
||||
|
||||
private static final Logger log = Logger.getLogger(EmailService.class);
|
||||
|
||||
private final Mailer mailer;
|
||||
@@ -76,7 +83,15 @@ public class EmailService {
|
||||
|
||||
// Verstuur de e-mail met HTML-inhoud
|
||||
log.info("Sending email to " + recipient);
|
||||
eventService.logInfo(className, "stuurVacatureEmail", "Sending email to " + recipient);
|
||||
try{
|
||||
mailer.send(Mail.withHtml(recipient, subject, emailBody.toString()));
|
||||
eventService.logSucces(className, "stuurVacatureEmail", "Email sent");
|
||||
} catch (Exception e) {
|
||||
eventService.logError(className, "stuurVacatureEmail", "Sending email failed", e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import nl.veenm.jobfindr.domain.Vacature;
|
||||
import nl.veenm.jobfindr.repository.VacatureDetailRepository;
|
||||
import nl.veenm.jobfindr.repository.VacatureRepository;
|
||||
import nl.veenm.jobfindr.scrapers.ScraperService;
|
||||
import nl.veenm.jobfindr.util.EventService;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -23,6 +24,9 @@ public class VacatureService {
|
||||
@Inject
|
||||
ScraperService scraperService;
|
||||
|
||||
@Inject
|
||||
EventService eventService;
|
||||
|
||||
@Inject
|
||||
EmailService emailService;
|
||||
|
||||
@@ -34,7 +38,10 @@ public class VacatureService {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(VacatureService.class);
|
||||
|
||||
private final String className = this.getClass().getName();
|
||||
|
||||
public List<Vacature> getServices() throws IOException {
|
||||
eventService.logInfo(VacatureService.class.getName(), "getServices", "getServices aangeroepen");
|
||||
List<Vacature> vacatures = new ArrayList<>();
|
||||
vacatures.addAll(scraperService.scrapeVacatures("https://www.meesterbaan.nl/vacatures/50-km?trefwoord=duits&locatie=Apeldoorn"));
|
||||
vacatures.addAll(scraperService.scrapeVacatures("https://www.meesterbaan.nl/vacatures/50-km?trefwoord=frans&locatie=Apeldoorn"));
|
||||
@@ -43,6 +50,8 @@ public class VacatureService {
|
||||
}
|
||||
|
||||
public void checkAndSendNewVacatures() throws IOException {
|
||||
logger.info("Checking for new vacatures");
|
||||
eventService.logInfo(className, "checkAndSendNewVacatures", "Checking for new vacatures");
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate yesterday = today.minusDays(1);
|
||||
|
||||
@@ -57,7 +66,7 @@ public class VacatureService {
|
||||
try {
|
||||
scraperService.getVacatureDetails(todayVacature);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
eventService.logError(className, "checkAndSendNewVacatures", "checkAndSendNewVacatures failed", e);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -83,8 +92,10 @@ public class VacatureService {
|
||||
emailService.stuurVacatureEmail("danthefranken@gmail.com", newVacatures);
|
||||
emailService.stuurVacatureEmail("vanveenmel11@gmail.com", newVacatures);
|
||||
logger.info("Nieuwe vacatures verstuurd en opgeslagen.");
|
||||
eventService.logSucces(className, "checkAndSendNewVacatures", "Nieuwe vacatures verstuurd en opgeslagen.");
|
||||
} else {
|
||||
logger.info("Geen nieuwe vacatures gevonden.");
|
||||
eventService.logInfo(className, "checkAndSendNewVacatures", "Geen nieuwe vacatures gevonden.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +107,6 @@ public class VacatureService {
|
||||
}
|
||||
|
||||
public List<Vacature> getAllVacatures() {
|
||||
logger.info("Get all vacatures");
|
||||
return vacatureRepository.listAll();
|
||||
}
|
||||
|
||||
@@ -108,6 +118,7 @@ public class VacatureService {
|
||||
@Transactional
|
||||
public void cleanVacatures() {
|
||||
logger.info("Cleaning vacatures");
|
||||
eventService.logInfo(className, "cleanVacatures", "Cleaning vacatures");
|
||||
getAllVacatures().forEach(vacature -> {
|
||||
var date = convertStringToLocalDate(vacature.getDetail().getSluitingsdatum());
|
||||
var today = LocalDate.now();
|
||||
@@ -115,11 +126,12 @@ public class VacatureService {
|
||||
if (date != null) {
|
||||
if (date.isBefore(today)) {
|
||||
logger.info("Deleting vacature " + vacature.getTitel());
|
||||
eventService.logInfo(className, "cleanVacatures", "Deleting vacature " + vacature.getTitel());
|
||||
vacatureDetailRepository.delete(vacature.getDetail());
|
||||
vacatureRepository.delete(vacature);
|
||||
}
|
||||
}
|
||||
|
||||
eventService.logSucces(className, "cleanVacatures", "Cleaning vacatures done");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
59
src/main/java/nl/veenm/jobfindr/util/EventService.java
Normal file
59
src/main/java/nl/veenm/jobfindr/util/EventService.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package nl.veenm.jobfindr.util;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import nl.veenm.jobfindr.domain.Event;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@ApplicationScoped
|
||||
public class EventService {
|
||||
|
||||
@Inject
|
||||
EventWebSocket eventWebSocket;
|
||||
|
||||
public void logInfo(String className, String methodName, String message) {
|
||||
// Maak een nieuw event
|
||||
Event event = new Event(
|
||||
Instant.now().toString(), // Tijdstempel
|
||||
className, // Naam van de klasse
|
||||
methodName, // Naam van de methode
|
||||
message,
|
||||
"INFO"
|
||||
// Bericht
|
||||
);
|
||||
|
||||
// Verstuur het event via WebSocket
|
||||
eventWebSocket.sendEvent(event);
|
||||
}
|
||||
|
||||
public void logSucces(String className, String methodName, String message) {
|
||||
// Maak een nieuw event
|
||||
Event event = new Event(
|
||||
Instant.now().toString(), // Tijdstempel
|
||||
className, // Naam van de klasse
|
||||
methodName, // Naam van de methode
|
||||
message,
|
||||
"SUCCESS"
|
||||
// Bericht
|
||||
);
|
||||
|
||||
// Verstuur het event via WebSocket
|
||||
eventWebSocket.sendEvent(event);
|
||||
}
|
||||
public void logError(String className, String methodName, String message, Exception e) {
|
||||
// Maak een nieuw event
|
||||
Event event = new Event(
|
||||
Instant.now().toString(), // Tijdstempel
|
||||
className, // Naam van de klasse
|
||||
methodName, // Naam van de methode
|
||||
message,
|
||||
"ERROR",
|
||||
e.getMessage()
|
||||
// Bericht
|
||||
);
|
||||
|
||||
// Verstuur het event via WebSocket
|
||||
eventWebSocket.sendEvent(event);
|
||||
}
|
||||
}
|
||||
48
src/main/java/nl/veenm/jobfindr/util/EventWebSocket.java
Normal file
48
src/main/java/nl/veenm/jobfindr/util/EventWebSocket.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package nl.veenm.jobfindr.util;
|
||||
|
||||
import jakarta.websocket.OnOpen;
|
||||
import jakarta.websocket.Session;
|
||||
import jakarta.websocket.server.ServerEndpoint;
|
||||
import nl.veenm.jobfindr.domain.Event;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ServerEndpoint("/events")
|
||||
@ApplicationScoped
|
||||
public class EventWebSocket {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(EventWebSocket.class);
|
||||
private final Set<Session> sessions = ConcurrentHashMap.newKeySet();
|
||||
private final ObjectMapper objectMapper = new ObjectMapper(); // Om objecten naar JSON te serialiseren
|
||||
|
||||
@OnOpen
|
||||
public void onOpen(Session session) {
|
||||
logger.info("event websocket opened: " + session.getId());
|
||||
sessions.add(session);
|
||||
}
|
||||
|
||||
public void sendEvent(Event event) {
|
||||
String jsonEvent;
|
||||
try {
|
||||
jsonEvent = objectMapper.writeValueAsString(event); // Event naar JSON converteren
|
||||
} catch (IOException e) {
|
||||
logger.error(e);
|
||||
return;
|
||||
}
|
||||
|
||||
for (Session session : sessions) {
|
||||
if (session.isOpen()) {
|
||||
try {
|
||||
session.getBasicRemote().sendText(jsonEvent);
|
||||
} catch (IOException e) {
|
||||
logger.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user