mirror of
https://github.com/dev-claw/vripper-project.git
synced 2026-08-19 08:35:41 +02:00
Implement an event bus
This commit is contained in:
@@ -16,14 +16,6 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jersey</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
@@ -43,7 +35,6 @@
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>2.5.1</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -64,7 +55,6 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.htmlcleaner</groupId>
|
||||
@@ -83,7 +73,6 @@
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>tn.mnlr</groupId>
|
||||
|
||||
@@ -5,23 +5,18 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import tn.mnlr.vripper.jpa.repositories.IRepository;
|
||||
import tn.mnlr.vripper.services.DataService;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@Component
|
||||
public class EventListenerBean {
|
||||
|
||||
@Getter private static boolean init = false;
|
||||
|
||||
private final DataService dataService;
|
||||
private final Set<IRepository> repositorySet;
|
||||
|
||||
@Autowired
|
||||
public EventListenerBean(DataService dataService, Set<IRepository> repositorySet) {
|
||||
public EventListenerBean(DataService dataService) {
|
||||
this.dataService = dataService;
|
||||
this.repositorySet = repositorySet;
|
||||
}
|
||||
|
||||
@EventListener
|
||||
|
||||
@@ -4,9 +4,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import net.jodah.failsafe.Failsafe;
|
||||
import tn.mnlr.vripper.SpringContext;
|
||||
import tn.mnlr.vripper.Utils;
|
||||
import tn.mnlr.vripper.jpa.domain.Event;
|
||||
import tn.mnlr.vripper.jpa.domain.LogEvent;
|
||||
import tn.mnlr.vripper.jpa.domain.enums.Status;
|
||||
import tn.mnlr.vripper.jpa.repositories.IEventRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.ILogEventRepository;
|
||||
import tn.mnlr.vripper.services.ConnectionService;
|
||||
import tn.mnlr.vripper.services.DataService;
|
||||
|
||||
@@ -18,7 +18,7 @@ public class DownloadRunnable implements Runnable {
|
||||
private final DownloadService downloadService;
|
||||
private final DataService dataService;
|
||||
private final ConnectionService connectionService;
|
||||
private final IEventRepository eventRepository;
|
||||
private final ILogEventRepository eventRepository;
|
||||
|
||||
private final DownloadJob downloadJob;
|
||||
|
||||
@@ -26,7 +26,7 @@ public class DownloadRunnable implements Runnable {
|
||||
downloadService = SpringContext.getBean(DownloadService.class);
|
||||
dataService = SpringContext.getBean(DataService.class);
|
||||
connectionService = SpringContext.getBean(ConnectionService.class);
|
||||
eventRepository = SpringContext.getBean(IEventRepository.class);
|
||||
eventRepository = SpringContext.getBean(ILogEventRepository.class);
|
||||
|
||||
this.downloadJob = downloadJob;
|
||||
}
|
||||
@@ -37,16 +37,16 @@ public class DownloadRunnable implements Runnable {
|
||||
.onFailure(
|
||||
e -> {
|
||||
try {
|
||||
Event event =
|
||||
new Event(
|
||||
Event.Type.DOWNLOAD,
|
||||
Event.Status.ERROR,
|
||||
LogEvent logEvent =
|
||||
new LogEvent(
|
||||
LogEvent.Type.DOWNLOAD,
|
||||
LogEvent.Status.ERROR,
|
||||
LocalDateTime.now(),
|
||||
String.format(
|
||||
"Failed to download %s\n %s",
|
||||
downloadJob.getImage().getUrl(),
|
||||
Utils.throwableToString(e.getFailure())));
|
||||
eventRepository.save(event);
|
||||
eventRepository.save(logEvent);
|
||||
} catch (Exception exp) {
|
||||
log.error("Failed to save event", exp);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package tn.mnlr.vripper.event;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class Event<T> {
|
||||
|
||||
private final T data;
|
||||
private final Kind kind;
|
||||
|
||||
private Event(Kind kind, T data) {
|
||||
this.data = data;
|
||||
this.kind = kind;
|
||||
}
|
||||
|
||||
public static <T> Event<T> wrap(Kind kind, T data) {
|
||||
return new Event(kind, data);
|
||||
}
|
||||
|
||||
public enum Kind {
|
||||
POST_UPDATE,
|
||||
POST_REMOVE,
|
||||
IMAGE_UPDATE,
|
||||
METADATA_UPDATE,
|
||||
QUEUED_UPDATE,
|
||||
QUEUED_REMOVE,
|
||||
LOG_EVENT_UPDATE,
|
||||
LOG_EVENT_REMOVE,
|
||||
VG_USER,
|
||||
GLOBAL_STATE,
|
||||
BYTES_PER_SECOND
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package tn.mnlr.vripper.event;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@Service
|
||||
public class EventBus {
|
||||
|
||||
public static final Sinks.EmitFailureHandler RETRY = (signalType, emitResult) -> true;
|
||||
private final Sinks.Many<Event> sink = Sinks.many().multicast().onBackpressureBuffer();
|
||||
|
||||
public void publishEvent(Event event) {
|
||||
sink.emitNext(event, RETRY);
|
||||
}
|
||||
|
||||
public Flux<Event> flux() {
|
||||
return sink.asFlux();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(RETRY);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package tn.mnlr.vripper.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
@Getter
|
||||
public class EventRemoveEvent extends ApplicationEvent {
|
||||
|
||||
private final Long id;
|
||||
|
||||
public EventRemoveEvent(Object source, Long id) {
|
||||
super(source);
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package tn.mnlr.vripper.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
@Getter
|
||||
public class EventUpdateEvent extends ApplicationEvent {
|
||||
|
||||
private final Long id;
|
||||
|
||||
public EventUpdateEvent(Object source, Long id) {
|
||||
super(source);
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package tn.mnlr.vripper.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
@Getter
|
||||
public class ImageUpdateEvent extends ApplicationEvent {
|
||||
|
||||
private final Long id;
|
||||
|
||||
public ImageUpdateEvent(Object source, Long id) {
|
||||
super(source);
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package tn.mnlr.vripper.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
@Getter
|
||||
public class MetadataUpdateEvent extends ApplicationEvent {
|
||||
|
||||
private final Long postIdRef;
|
||||
|
||||
public MetadataUpdateEvent(Object source, Long postIdRef) {
|
||||
super(source);
|
||||
this.postIdRef = postIdRef;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package tn.mnlr.vripper.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
@Getter
|
||||
public class PostRemoveEvent extends ApplicationEvent {
|
||||
|
||||
private final String postId;
|
||||
|
||||
public PostRemoveEvent(Object source, String postId) {
|
||||
super(source);
|
||||
this.postId = postId;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package tn.mnlr.vripper.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
@Getter
|
||||
public class PostUpdateEvent extends ApplicationEvent {
|
||||
|
||||
private final Long id;
|
||||
|
||||
public PostUpdateEvent(Object source, Long id) {
|
||||
super(source);
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package tn.mnlr.vripper.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
@Getter
|
||||
public class QueuedRemoveEvent extends ApplicationEvent {
|
||||
|
||||
private final String threadId;
|
||||
|
||||
public QueuedRemoveEvent(Object source, String threadId) {
|
||||
super(source);
|
||||
this.threadId = threadId;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package tn.mnlr.vripper.event;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
@Getter
|
||||
public class QueuedUpdateEvent extends ApplicationEvent {
|
||||
|
||||
private final Long id;
|
||||
|
||||
public QueuedUpdateEvent(Object source, Long id) {
|
||||
super(source);
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -17,7 +17,7 @@ import java.time.format.DateTimeFormatter;
|
||||
@Setter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class Event {
|
||||
public class LogEvent {
|
||||
|
||||
private Long id;
|
||||
private Type type;
|
||||
@@ -28,7 +28,7 @@ public class Event {
|
||||
|
||||
private String message;
|
||||
|
||||
public Event(Type type, Status status, LocalDateTime time, String message) {
|
||||
public LogEvent(Type type, Status status, LocalDateTime time, String message) {
|
||||
this.type = type;
|
||||
this.status = status;
|
||||
this.time = time;
|
||||
@@ -1,21 +0,0 @@
|
||||
package tn.mnlr.vripper.jpa.repositories;
|
||||
|
||||
import tn.mnlr.vripper.jpa.domain.Event;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface IEventRepository extends IRepository {
|
||||
|
||||
Event save(Event event);
|
||||
|
||||
Event update(Event event);
|
||||
|
||||
List<Event> findAll();
|
||||
|
||||
Optional<Event> findById(Long id);
|
||||
|
||||
void delete(Long id);
|
||||
|
||||
void deleteAll();
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package tn.mnlr.vripper.jpa.repositories;
|
||||
|
||||
import tn.mnlr.vripper.jpa.domain.LogEvent;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ILogEventRepository extends IRepository {
|
||||
|
||||
LogEvent save(LogEvent logEvent);
|
||||
|
||||
LogEvent update(LogEvent logEvent);
|
||||
|
||||
List<LogEvent> findAll();
|
||||
|
||||
Optional<LogEvent> findById(Long id);
|
||||
|
||||
void delete(Long id);
|
||||
|
||||
void deleteAll();
|
||||
}
|
||||
-113
@@ -1,113 +0,0 @@
|
||||
package tn.mnlr.vripper.jpa.repositories.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tn.mnlr.vripper.event.EventRemoveEvent;
|
||||
import tn.mnlr.vripper.event.EventUpdateEvent;
|
||||
import tn.mnlr.vripper.jpa.domain.Event;
|
||||
import tn.mnlr.vripper.jpa.repositories.IEventRepository;
|
||||
import tn.mnlr.vripper.services.SettingsService;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class EventRepository implements IEventRepository, ApplicationEventPublisherAware {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final SettingsService settingsService;
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
public EventRepository(JdbcTemplate jdbcTemplate, SettingsService settingsService) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.settingsService = settingsService;
|
||||
}
|
||||
|
||||
private synchronized Long nextId() {
|
||||
return jdbcTemplate.queryForObject("CALL NEXT VALUE FOR SEQ_EVENT", Long.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Event save(@NonNull Event event) {
|
||||
|
||||
int maxRecords = settingsService.getSettings().getMaxEventLog() - 1;
|
||||
|
||||
Long count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EVENT", Long.class);
|
||||
if (count > maxRecords) {
|
||||
List<Long> idList =
|
||||
jdbcTemplate.queryForList(
|
||||
"SELECT ID FROM EVENT ORDER BY TIME ASC LIMIT ?", Long.class, count - maxRecords);
|
||||
idList.forEach(this::delete);
|
||||
}
|
||||
|
||||
long id = nextId();
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO EVENT (ID, TYPE, STATUS, TIME, MESSAGE) VALUES (?,?,?,?,?)",
|
||||
id,
|
||||
event.getType().name(),
|
||||
event.getStatus().name(),
|
||||
event.getTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
|
||||
event.getMessage());
|
||||
event.setId(id);
|
||||
applicationEventPublisher.publishEvent(new EventUpdateEvent(EventRepository.class, id));
|
||||
return event;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event update(@NonNull Event event) {
|
||||
if (event.getId() == null) {
|
||||
log.warn("Cannot update entity with null id");
|
||||
return event;
|
||||
}
|
||||
|
||||
jdbcTemplate.update(
|
||||
"UPDATE EVENT SET TYPE = ?, STATUS = ?, TIME = ?, MESSAGE = ? WHERE ID = ?",
|
||||
event.getType().name(),
|
||||
event.getStatus().name(),
|
||||
event.getTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
|
||||
event.getMessage(),
|
||||
event.getId());
|
||||
applicationEventPublisher.publishEvent(
|
||||
new EventUpdateEvent(EventRepository.class, event.getId()));
|
||||
return event;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Event> findById(Long id) {
|
||||
List<Event> events =
|
||||
jdbcTemplate.query("SELECT * FROM EVENT WHERE ID = ?", new EventRowMapper(), id);
|
||||
if (events.isEmpty()) {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
return Optional.of(events.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Event> findAll() {
|
||||
return jdbcTemplate.query("SELECT * FROM EVENT", new EventRowMapper());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
jdbcTemplate.update("DELETE FROM EVENT WHERE ID = ?", id);
|
||||
applicationEventPublisher.publishEvent(new EventRemoveEvent(EventRepository.class, id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
jdbcTemplate.update("DELETE FROM EVENT");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(
|
||||
@NonNull ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
}
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
package tn.mnlr.vripper.jpa.repositories.impl;
|
||||
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import tn.mnlr.vripper.jpa.domain.Event;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class EventRowMapper implements RowMapper<Event> {
|
||||
|
||||
@Override
|
||||
public Event mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Event event = new Event();
|
||||
event.setId(rs.getLong("ID"));
|
||||
event.setType(Event.Type.valueOf(rs.getString("TYPE")));
|
||||
event.setStatus(Event.Status.valueOf(rs.getString("STATUS")));
|
||||
event.setTime(LocalDateTime.parse(rs.getString("TIME"), DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||
event.setMessage(rs.getString("MESSAGE"));
|
||||
return event;
|
||||
}
|
||||
}
|
||||
+35
-14
@@ -1,28 +1,32 @@
|
||||
package tn.mnlr.vripper.jpa.repositories.impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tn.mnlr.vripper.event.ImageUpdateEvent;
|
||||
import tn.mnlr.vripper.SpringContext;
|
||||
import tn.mnlr.vripper.event.Event;
|
||||
import tn.mnlr.vripper.event.EventBus;
|
||||
import tn.mnlr.vripper.host.Host;
|
||||
import tn.mnlr.vripper.jpa.domain.Image;
|
||||
import tn.mnlr.vripper.jpa.domain.enums.Status;
|
||||
import tn.mnlr.vripper.jpa.repositories.IImageRepository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class ImageRepository implements IImageRepository, ApplicationEventPublisherAware {
|
||||
public class ImageRepository implements IImageRepository {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
private final EventBus eventBus;
|
||||
|
||||
@Autowired
|
||||
public ImageRepository(JdbcTemplate jdbcTemplate) {
|
||||
public ImageRepository(JdbcTemplate jdbcTemplate, EventBus eventBus) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
private synchronized Long nextId() {
|
||||
@@ -44,7 +48,7 @@ public class ImageRepository implements IImageRepository, ApplicationEventPublis
|
||||
image.getUrl(),
|
||||
image.getPostIdRef());
|
||||
image.setId(id);
|
||||
applicationEventPublisher.publishEvent(new ImageUpdateEvent(ImageRepository.class, id));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.IMAGE_UPDATE, id));
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -105,7 +109,7 @@ public class ImageRepository implements IImageRepository, ApplicationEventPublis
|
||||
int mutationCount =
|
||||
jdbcTemplate.update(
|
||||
"UPDATE IMAGE AS image SET image.STATUS = ? WHERE image.ID = ?", status.name(), id);
|
||||
applicationEventPublisher.publishEvent(new ImageUpdateEvent(ImageRepository.class, id));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.IMAGE_UPDATE, id));
|
||||
return mutationCount;
|
||||
}
|
||||
|
||||
@@ -114,7 +118,7 @@ public class ImageRepository implements IImageRepository, ApplicationEventPublis
|
||||
int mutationCount =
|
||||
jdbcTemplate.update(
|
||||
"UPDATE IMAGE AS image SET image.CURRENT = ? WHERE image.ID = ?", current, id);
|
||||
applicationEventPublisher.publishEvent(new ImageUpdateEvent(ImageRepository.class, id));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.IMAGE_UPDATE, id));
|
||||
return mutationCount;
|
||||
}
|
||||
|
||||
@@ -123,13 +127,30 @@ public class ImageRepository implements IImageRepository, ApplicationEventPublis
|
||||
int mutationCount =
|
||||
jdbcTemplate.update(
|
||||
"UPDATE IMAGE AS image SET image.TOTAL = ? WHERE image.ID = ?", total, id);
|
||||
applicationEventPublisher.publishEvent(new ImageUpdateEvent(ImageRepository.class, id));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.IMAGE_UPDATE, id));
|
||||
return mutationCount;
|
||||
}
|
||||
}
|
||||
|
||||
class ImageRowMapper implements RowMapper<Image> {
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(
|
||||
@NonNull ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
public Image mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Image image = new Image();
|
||||
image.setId(rs.getLong("ID"));
|
||||
String host = rs.getString("HOST");
|
||||
image.setHost(
|
||||
SpringContext.getBeansOfType(Host.class).values().stream()
|
||||
.filter(e -> e.getHost().equals(host))
|
||||
.findAny()
|
||||
.orElse(null));
|
||||
image.setUrl(rs.getString("URL"));
|
||||
image.setIndex(rs.getInt("INDEX"));
|
||||
image.setCurrent(rs.getLong("CURRENT"));
|
||||
image.setTotal(rs.getLong("TOTAL"));
|
||||
image.setStatus(Status.valueOf(rs.getString("STATUS")));
|
||||
image.setPostId(rs.getString("POST_ID"));
|
||||
image.setPostIdRef(rs.getLong("POST_ID_REF"));
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
package tn.mnlr.vripper.jpa.repositories.impl;
|
||||
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import tn.mnlr.vripper.SpringContext;
|
||||
import tn.mnlr.vripper.host.Host;
|
||||
import tn.mnlr.vripper.jpa.domain.Image;
|
||||
import tn.mnlr.vripper.jpa.domain.enums.Status;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class ImageRowMapper implements RowMapper<Image> {
|
||||
|
||||
@Override
|
||||
public Image mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Image image = new Image();
|
||||
image.setId(rs.getLong("ID"));
|
||||
String host = rs.getString("HOST");
|
||||
image.setHost(
|
||||
SpringContext.getBeansOfType(Host.class).values().stream()
|
||||
.filter(e -> e.getHost().equals(host))
|
||||
.findAny()
|
||||
.orElse(null));
|
||||
image.setUrl(rs.getString("URL"));
|
||||
image.setIndex(rs.getInt("INDEX"));
|
||||
image.setCurrent(rs.getLong("CURRENT"));
|
||||
image.setTotal(rs.getLong("TOTAL"));
|
||||
image.setStatus(Status.valueOf(rs.getString("STATUS")));
|
||||
image.setPostId(rs.getString("POST_ID"));
|
||||
image.setPostIdRef(rs.getLong("POST_ID_REF"));
|
||||
return image;
|
||||
}
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
package tn.mnlr.vripper.jpa.repositories.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tn.mnlr.vripper.event.Event;
|
||||
import tn.mnlr.vripper.event.EventBus;
|
||||
import tn.mnlr.vripper.jpa.domain.LogEvent;
|
||||
import tn.mnlr.vripper.jpa.repositories.ILogEventRepository;
|
||||
import tn.mnlr.vripper.services.SettingsService;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class LogEventRepository implements ILogEventRepository {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private final SettingsService settingsService;
|
||||
private final EventBus eventBus;
|
||||
|
||||
public LogEventRepository(
|
||||
JdbcTemplate jdbcTemplate, SettingsService settingsService, EventBus eventBus) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.settingsService = settingsService;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
private synchronized Long nextId() {
|
||||
return jdbcTemplate.queryForObject("CALL NEXT VALUE FOR SEQ_EVENT", Long.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized LogEvent save(@NonNull LogEvent logEvent) {
|
||||
|
||||
int maxRecords = settingsService.getSettings().getMaxEventLog() - 1;
|
||||
|
||||
Long count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EVENT", Long.class);
|
||||
if (count > maxRecords) {
|
||||
List<Long> idList =
|
||||
jdbcTemplate.queryForList(
|
||||
"SELECT ID FROM EVENT ORDER BY TIME ASC LIMIT ?", Long.class, count - maxRecords);
|
||||
idList.forEach(this::delete);
|
||||
}
|
||||
|
||||
long id = nextId();
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO EVENT (ID, TYPE, STATUS, TIME, MESSAGE) VALUES (?,?,?,?,?)",
|
||||
id,
|
||||
logEvent.getType().name(),
|
||||
logEvent.getStatus().name(),
|
||||
logEvent.getTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
|
||||
logEvent.getMessage());
|
||||
logEvent.setId(id);
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.LOG_EVENT_UPDATE, id));
|
||||
return logEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LogEvent update(@NonNull LogEvent logEvent) {
|
||||
if (logEvent.getId() == null) {
|
||||
log.warn("Cannot update entity with null id");
|
||||
return logEvent;
|
||||
}
|
||||
|
||||
jdbcTemplate.update(
|
||||
"UPDATE EVENT SET TYPE = ?, STATUS = ?, TIME = ?, MESSAGE = ? WHERE ID = ?",
|
||||
logEvent.getType().name(),
|
||||
logEvent.getStatus().name(),
|
||||
logEvent.getTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
|
||||
logEvent.getMessage(),
|
||||
logEvent.getId());
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.LOG_EVENT_UPDATE, logEvent.getId()));
|
||||
return logEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<LogEvent> findById(Long id) {
|
||||
List<LogEvent> logEvents =
|
||||
jdbcTemplate.query("SELECT * FROM EVENT WHERE ID = ?", new LogEventRowMapper(), id);
|
||||
if (logEvents.isEmpty()) {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
return Optional.of(logEvents.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LogEvent> findAll() {
|
||||
return jdbcTemplate.query("SELECT * FROM EVENT", new LogEventRowMapper());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
jdbcTemplate.update("DELETE FROM EVENT WHERE ID = ?", id);
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.LOG_EVENT_REMOVE, id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
jdbcTemplate.update("DELETE FROM EVENT");
|
||||
}
|
||||
}
|
||||
|
||||
class LogEventRowMapper implements RowMapper<LogEvent> {
|
||||
|
||||
@Override
|
||||
public LogEvent mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
LogEvent logEvent = new LogEvent();
|
||||
logEvent.setId(rs.getLong("ID"));
|
||||
logEvent.setType(LogEvent.Type.valueOf(rs.getString("TYPE")));
|
||||
logEvent.setStatus(LogEvent.Status.valueOf(rs.getString("STATUS")));
|
||||
logEvent.setTime(
|
||||
LocalDateTime.parse(rs.getString("TIME"), DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
||||
logEvent.setMessage(rs.getString("MESSAGE"));
|
||||
return logEvent;
|
||||
}
|
||||
}
|
||||
+23
-10
@@ -1,26 +1,29 @@
|
||||
package tn.mnlr.vripper.jpa.repositories.impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tn.mnlr.vripper.event.MetadataUpdateEvent;
|
||||
import tn.mnlr.vripper.event.Event;
|
||||
import tn.mnlr.vripper.event.EventBus;
|
||||
import tn.mnlr.vripper.jpa.domain.Metadata;
|
||||
import tn.mnlr.vripper.jpa.repositories.IMetadataRepository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class MetadataRepository implements IMetadataRepository, ApplicationEventPublisherAware {
|
||||
public class MetadataRepository implements IMetadataRepository {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
private final EventBus eventBus;
|
||||
|
||||
@Autowired
|
||||
public MetadataRepository(JdbcTemplate jdbcTemplate) {
|
||||
public MetadataRepository(JdbcTemplate jdbcTemplate, EventBus eventBus) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -31,8 +34,7 @@ public class MetadataRepository implements IMetadataRepository, ApplicationEvent
|
||||
metadata.getPostId(),
|
||||
metadata.getPostedBy(),
|
||||
String.join("%sep%", metadata.getResolvedNames()));
|
||||
applicationEventPublisher.publishEvent(
|
||||
new MetadataUpdateEvent(MetadataRepository.class, metadata.getPostIdRef()));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.METADATA_UPDATE, metadata.getPostIdRef()));
|
||||
return metadata;
|
||||
}
|
||||
|
||||
@@ -55,9 +57,20 @@ public class MetadataRepository implements IMetadataRepository, ApplicationEvent
|
||||
return jdbcTemplate.update(
|
||||
"DELETE FROM METADATA AS metadata WHERE metadata.POST_ID = ?", postId);
|
||||
}
|
||||
}
|
||||
|
||||
class MetadataRowMapper implements RowMapper<Metadata> {
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
public Metadata mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setPostIdRef(rs.getLong("POST_ID_REF"));
|
||||
metadata.setPostId(rs.getString("POST_ID"));
|
||||
metadata.setPostedBy(rs.getString("POSTED_BY"));
|
||||
String resolvedNames = rs.getString("RESOLVED_NAMES");
|
||||
if (resolvedNames != null && !resolvedNames.isBlank()) {
|
||||
metadata.setResolvedNames(List.of(resolvedNames.split("%sep%")));
|
||||
}
|
||||
return metadata;
|
||||
}
|
||||
}
|
||||
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
package tn.mnlr.vripper.jpa.repositories.impl;
|
||||
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import tn.mnlr.vripper.jpa.domain.Metadata;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public class MetadataRowMapper implements RowMapper<Metadata> {
|
||||
|
||||
@Override
|
||||
public Metadata mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setPostIdRef(rs.getLong("POST_ID_REF"));
|
||||
metadata.setPostId(rs.getString("POST_ID"));
|
||||
metadata.setPostedBy(rs.getString("POSTED_BY"));
|
||||
String resolvedNames = rs.getString("RESOLVED_NAMES");
|
||||
if (resolvedNames != null && !resolvedNames.isBlank()) {
|
||||
metadata.setResolvedNames(List.of(resolvedNames.split("%sep%")));
|
||||
}
|
||||
return metadata;
|
||||
}
|
||||
}
|
||||
+59
-18
@@ -1,29 +1,32 @@
|
||||
package tn.mnlr.vripper.jpa.repositories.impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tn.mnlr.vripper.event.PostRemoveEvent;
|
||||
import tn.mnlr.vripper.event.PostUpdateEvent;
|
||||
import tn.mnlr.vripper.event.Event;
|
||||
import tn.mnlr.vripper.event.EventBus;
|
||||
import tn.mnlr.vripper.jpa.domain.Metadata;
|
||||
import tn.mnlr.vripper.jpa.domain.Post;
|
||||
import tn.mnlr.vripper.jpa.domain.enums.Status;
|
||||
import tn.mnlr.vripper.jpa.repositories.IPostRepository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
public class PostRepository implements IPostRepository, ApplicationEventPublisherAware {
|
||||
public class PostRepository implements IPostRepository {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
private final EventBus eventBus;
|
||||
|
||||
@Autowired
|
||||
public PostRepository(JdbcTemplate jdbcTemplate) {
|
||||
public PostRepository(JdbcTemplate jdbcTemplate, EventBus eventBus) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
private synchronized Long nextId() {
|
||||
@@ -51,7 +54,7 @@ public class PostRepository implements IPostRepository, ApplicationEventPublishe
|
||||
post.getTotal(),
|
||||
post.getUrl());
|
||||
post.setId(id);
|
||||
applicationEventPublisher.publishEvent(new PostUpdateEvent(PostRepository.class, id));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.POST_UPDATE, id));
|
||||
return post;
|
||||
}
|
||||
|
||||
@@ -119,7 +122,7 @@ public class PostRepository implements IPostRepository, ApplicationEventPublishe
|
||||
public int deleteByPostId(String postId) {
|
||||
int mutationCount =
|
||||
jdbcTemplate.update("DELETE FROM POST AS post WHERE post.POST_ID = ?", postId);
|
||||
applicationEventPublisher.publishEvent(new PostRemoveEvent(PostRepository.class, postId));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.POST_REMOVE, postId));
|
||||
return mutationCount;
|
||||
}
|
||||
|
||||
@@ -128,7 +131,7 @@ public class PostRepository implements IPostRepository, ApplicationEventPublishe
|
||||
int mutationCount =
|
||||
jdbcTemplate.update(
|
||||
"UPDATE POST AS post SET post.STATUS = ? WHERE post.ID = ?", status.name(), id);
|
||||
applicationEventPublisher.publishEvent(new PostUpdateEvent(PostRepository.class, id));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.POST_UPDATE, id));
|
||||
return mutationCount;
|
||||
}
|
||||
|
||||
@@ -136,7 +139,7 @@ public class PostRepository implements IPostRepository, ApplicationEventPublishe
|
||||
public int updateDone(int done, Long id) {
|
||||
int mutationCount =
|
||||
jdbcTemplate.update("UPDATE POST AS post SET post.DONE = ? WHERE post.ID = ?", done, id);
|
||||
applicationEventPublisher.publishEvent(new PostUpdateEvent(PostRepository.class, id));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.POST_UPDATE, id));
|
||||
return mutationCount;
|
||||
}
|
||||
|
||||
@@ -147,7 +150,7 @@ public class PostRepository implements IPostRepository, ApplicationEventPublishe
|
||||
"UPDATE POST AS post SET post.POST_FOLDER_NAME = ? WHERE post.ID = ?",
|
||||
postFolderName,
|
||||
id);
|
||||
applicationEventPublisher.publishEvent(new PostUpdateEvent(PostRepository.class, id));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.POST_UPDATE, id));
|
||||
return mutationCount;
|
||||
}
|
||||
|
||||
@@ -155,7 +158,7 @@ public class PostRepository implements IPostRepository, ApplicationEventPublishe
|
||||
public int updateTitle(String title, Long id) {
|
||||
int mutationCount =
|
||||
jdbcTemplate.update("UPDATE POST AS post SET post.TITLE = ? WHERE post.ID = ?", title, id);
|
||||
applicationEventPublisher.publishEvent(new PostUpdateEvent(PostRepository.class, id));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.POST_UPDATE, id));
|
||||
return mutationCount;
|
||||
}
|
||||
|
||||
@@ -164,13 +167,51 @@ public class PostRepository implements IPostRepository, ApplicationEventPublishe
|
||||
int mutationCount =
|
||||
jdbcTemplate.update(
|
||||
"UPDATE POST AS post SET post.THANKED = ? WHERE post.ID = ?", thanked, id);
|
||||
applicationEventPublisher.publishEvent(new PostUpdateEvent(PostRepository.class, id));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.POST_UPDATE, id));
|
||||
return mutationCount;
|
||||
}
|
||||
}
|
||||
|
||||
class PostRowMapper implements RowMapper<Post> {
|
||||
|
||||
private static final String DELIMITER = ";";
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(
|
||||
@NonNull ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
public Post mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
|
||||
Post post = new Post();
|
||||
post.setId(rs.getLong("post.ID"));
|
||||
post.setStatus(Status.valueOf(rs.getString("post.STATUS")));
|
||||
post.setPostId(rs.getString("post.POST_ID"));
|
||||
post.setThreadTitle(rs.getString("post.THREAD_TITLE"));
|
||||
post.setThreadId(rs.getString("post.THREAD_ID"));
|
||||
post.setTitle(rs.getString("post.TITLE"));
|
||||
post.setUrl(rs.getString("post.URL"));
|
||||
post.setDone(rs.getInt("post.DONE"));
|
||||
post.setTotal(rs.getInt("post.TOTAL"));
|
||||
post.setHosts(Set.of(rs.getString("post.HOSTS").split(DELIMITER)));
|
||||
post.setForum(rs.getString("post.FORUM"));
|
||||
post.setSecurityToken(rs.getString("post.SECURITY_TOKEN"));
|
||||
post.setDownloadDirectory(rs.getString("post.POST_FOLDER_NAME"));
|
||||
post.setThanked(rs.getBoolean("post.THANKED"));
|
||||
String previews;
|
||||
if ((previews = rs.getString("post.PREVIEWS")) != null) {
|
||||
post.setPreviews(Set.of(previews.split(DELIMITER)));
|
||||
}
|
||||
|
||||
Long metadataId = rs.getLong("metadata.POST_ID_REF");
|
||||
if (!rs.wasNull()) {
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setPostIdRef(metadataId);
|
||||
metadata.setPostId(rs.getString("metadata.POST_ID"));
|
||||
String resolvedNames = rs.getString("metadata.RESOLVED_NAMES");
|
||||
if (resolvedNames != null && !resolvedNames.isBlank()) {
|
||||
metadata.setResolvedNames(List.of(resolvedNames.split("%sep%")));
|
||||
}
|
||||
metadata.setPostedBy(rs.getString("metadata.POSTED_BY"));
|
||||
post.setMetadata(metadata);
|
||||
}
|
||||
|
||||
return post;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
package tn.mnlr.vripper.jpa.repositories.impl;
|
||||
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import tn.mnlr.vripper.jpa.domain.Metadata;
|
||||
import tn.mnlr.vripper.jpa.domain.Post;
|
||||
import tn.mnlr.vripper.jpa.domain.enums.Status;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class PostRowMapper implements RowMapper<Post> {
|
||||
|
||||
private static final String DELIMITER = ";";
|
||||
|
||||
@Override
|
||||
public Post mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
|
||||
Post post = new Post();
|
||||
post.setId(rs.getLong("post.ID"));
|
||||
post.setStatus(Status.valueOf(rs.getString("post.STATUS")));
|
||||
post.setPostId(rs.getString("post.POST_ID"));
|
||||
post.setThreadTitle(rs.getString("post.THREAD_TITLE"));
|
||||
post.setThreadId(rs.getString("post.THREAD_ID"));
|
||||
post.setTitle(rs.getString("post.TITLE"));
|
||||
post.setUrl(rs.getString("post.URL"));
|
||||
post.setDone(rs.getInt("post.DONE"));
|
||||
post.setTotal(rs.getInt("post.TOTAL"));
|
||||
post.setHosts(Set.of(rs.getString("post.HOSTS").split(DELIMITER)));
|
||||
post.setForum(rs.getString("post.FORUM"));
|
||||
post.setSecurityToken(rs.getString("post.SECURITY_TOKEN"));
|
||||
post.setDownloadDirectory(rs.getString("post.POST_FOLDER_NAME"));
|
||||
post.setThanked(rs.getBoolean("post.THANKED"));
|
||||
String previews;
|
||||
if ((previews = rs.getString("post.PREVIEWS")) != null) {
|
||||
post.setPreviews(Set.of(previews.split(DELIMITER)));
|
||||
}
|
||||
|
||||
Long metadataId = rs.getLong("metadata.POST_ID_REF");
|
||||
if (!rs.wasNull()) {
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setPostIdRef(metadataId);
|
||||
metadata.setPostId(rs.getString("metadata.POST_ID"));
|
||||
String resolvedNames = rs.getString("metadata.RESOLVED_NAMES");
|
||||
if (resolvedNames != null && !resolvedNames.isBlank()) {
|
||||
metadata.setResolvedNames(List.of(resolvedNames.split("%sep%")));
|
||||
}
|
||||
metadata.setPostedBy(rs.getString("metadata.POSTED_BY"));
|
||||
post.setMetadata(metadata);
|
||||
}
|
||||
|
||||
return post;
|
||||
}
|
||||
}
|
||||
+23
-11
@@ -1,27 +1,29 @@
|
||||
package tn.mnlr.vripper.jpa.repositories.impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tn.mnlr.vripper.event.QueuedRemoveEvent;
|
||||
import tn.mnlr.vripper.event.QueuedUpdateEvent;
|
||||
import tn.mnlr.vripper.event.Event;
|
||||
import tn.mnlr.vripper.event.EventBus;
|
||||
import tn.mnlr.vripper.jpa.domain.Queued;
|
||||
import tn.mnlr.vripper.jpa.repositories.IQueuedRepository;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class QueuedRepository implements IQueuedRepository, ApplicationEventPublisherAware {
|
||||
public class QueuedRepository implements IQueuedRepository {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
private final EventBus eventBus;
|
||||
|
||||
@Autowired
|
||||
public QueuedRepository(JdbcTemplate jdbcTemplate) {
|
||||
public QueuedRepository(JdbcTemplate jdbcTemplate, EventBus eventBus) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
private synchronized Long nextId() {
|
||||
@@ -40,7 +42,7 @@ public class QueuedRepository implements IQueuedRepository, ApplicationEventPubl
|
||||
queued.getPostId(),
|
||||
queued.getThreadId());
|
||||
queued.setId(id);
|
||||
applicationEventPublisher.publishEvent(new QueuedUpdateEvent(QueuedRepository.class, id));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.QUEUED_UPDATE, id));
|
||||
return queued;
|
||||
}
|
||||
|
||||
@@ -79,7 +81,7 @@ public class QueuedRepository implements IQueuedRepository, ApplicationEventPubl
|
||||
public int deleteByThreadId(String threadId) {
|
||||
int mutationCount =
|
||||
jdbcTemplate.update("DELETE FROM QUEUED AS queued WHERE THREAD_ID = ?", threadId);
|
||||
applicationEventPublisher.publishEvent(new QueuedRemoveEvent(QueuedRepository.class, threadId));
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.QUEUED_REMOVE, threadId));
|
||||
return mutationCount;
|
||||
}
|
||||
|
||||
@@ -87,9 +89,19 @@ public class QueuedRepository implements IQueuedRepository, ApplicationEventPubl
|
||||
public void deleteAll() {
|
||||
jdbcTemplate.update("DELETE FROM QUEUED");
|
||||
}
|
||||
}
|
||||
|
||||
class QueuedRowMapper implements RowMapper<Queued> {
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
public Queued mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Queued queued = new Queued();
|
||||
queued.setId(rs.getLong("ID"));
|
||||
queued.setLink(rs.getString("LINK"));
|
||||
queued.setThreadId(rs.getString("THREAD_ID"));
|
||||
queued.setPostId(rs.getString("POST_ID"));
|
||||
queued.setTotal(rs.getInt("TOTAL"));
|
||||
queued.setLoading(rs.getBoolean("LOADING"));
|
||||
return queued;
|
||||
}
|
||||
}
|
||||
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
package tn.mnlr.vripper.jpa.repositories.impl;
|
||||
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import tn.mnlr.vripper.jpa.domain.Queued;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class QueuedRowMapper implements RowMapper<Queued> {
|
||||
|
||||
@Override
|
||||
public Queued mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Queued queued = new Queued();
|
||||
queued.setId(rs.getLong("ID"));
|
||||
queued.setLink(rs.getString("LINK"));
|
||||
queued.setThreadId(rs.getString("THREAD_ID"));
|
||||
queued.setPostId(rs.getString("POST_ID"));
|
||||
queued.setTotal(rs.getInt("TOTAL"));
|
||||
queued.setLoading(rs.getBoolean("LOADING"));
|
||||
return queued;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package tn.mnlr.vripper.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import tn.mnlr.vripper.event.EventRemoveEvent;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class EventRemoveEventListener
|
||||
implements ApplicationListener<EventRemoveEvent>, DataEventListener<EventRemoveEvent> {
|
||||
|
||||
private final Sinks.Many<EventRemoveEvent> sink = Sinks.many().multicast().onBackpressureBuffer();
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(EventRemoveEvent event) {
|
||||
sink.emitNext(event, EmitHandler.RETRY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<EventRemoveEvent> getDataFlux() {
|
||||
return sink.asFlux();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package tn.mnlr.vripper.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import tn.mnlr.vripper.event.EventUpdateEvent;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class EventUpdateEventListener
|
||||
implements ApplicationListener<EventUpdateEvent>, DataEventListener<EventUpdateEvent> {
|
||||
|
||||
private final Sinks.Many<EventUpdateEvent> sink = Sinks.many().multicast().onBackpressureBuffer();
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(@NonNull EventUpdateEvent event) {
|
||||
sink.emitNext(event, EmitHandler.RETRY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<EventUpdateEvent> getDataFlux() {
|
||||
return sink.asFlux();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package tn.mnlr.vripper.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import tn.mnlr.vripper.event.ImageUpdateEvent;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class ImageUpdateEventListener
|
||||
implements ApplicationListener<ImageUpdateEvent>, DataEventListener<ImageUpdateEvent> {
|
||||
|
||||
private final Sinks.Many<ImageUpdateEvent> sink = Sinks.many().multicast().onBackpressureBuffer();
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ImageUpdateEvent event) {
|
||||
sink.emitNext(event, EmitHandler.RETRY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<ImageUpdateEvent> getDataFlux() {
|
||||
return sink.asFlux();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
|
||||
}
|
||||
}
|
||||
-34
@@ -1,34 +0,0 @@
|
||||
package tn.mnlr.vripper.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import tn.mnlr.vripper.event.MetadataUpdateEvent;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class MetadataUpdateEventListener
|
||||
implements ApplicationListener<MetadataUpdateEvent>, DataEventListener<MetadataUpdateEvent> {
|
||||
|
||||
private final Sinks.Many<MetadataUpdateEvent> sink =
|
||||
Sinks.many().multicast().onBackpressureBuffer();
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(MetadataUpdateEvent event) {
|
||||
sink.emitNext(event, (signalType, emitResult) -> true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<MetadataUpdateEvent> getDataFlux() {
|
||||
return sink.asFlux();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package tn.mnlr.vripper.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import tn.mnlr.vripper.event.PostRemoveEvent;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class PostRemoveEventListener
|
||||
implements ApplicationListener<PostRemoveEvent>, DataEventListener<PostRemoveEvent> {
|
||||
|
||||
private final Sinks.Many<PostRemoveEvent> sink = Sinks.many().multicast().onBackpressureBuffer();
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(PostRemoveEvent event) {
|
||||
sink.emitNext(event, EmitHandler.RETRY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<PostRemoveEvent> getDataFlux() {
|
||||
return sink.asFlux();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package tn.mnlr.vripper.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import tn.mnlr.vripper.event.PostUpdateEvent;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class PostUpdateEventListener
|
||||
implements ApplicationListener<PostUpdateEvent>, DataEventListener<PostUpdateEvent> {
|
||||
|
||||
private final Sinks.Many<PostUpdateEvent> sink = Sinks.many().multicast().onBackpressureBuffer();
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(PostUpdateEvent event) {
|
||||
sink.emitNext(event, EmitHandler.RETRY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<PostUpdateEvent> getDataFlux() {
|
||||
return sink.asFlux();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package tn.mnlr.vripper.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import tn.mnlr.vripper.event.QueuedRemoveEvent;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class QueuedRemoveEventListener
|
||||
implements ApplicationListener<QueuedRemoveEvent>, DataEventListener<QueuedRemoveEvent> {
|
||||
|
||||
private final Sinks.Many<QueuedRemoveEvent> sink =
|
||||
Sinks.many().multicast().onBackpressureBuffer();
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(QueuedRemoveEvent event) {
|
||||
sink.emitNext(event, (signalType, emitResult) -> true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<QueuedRemoveEvent> getDataFlux() {
|
||||
return sink.asFlux();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package tn.mnlr.vripper.listener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import tn.mnlr.vripper.event.QueuedUpdateEvent;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class QueuedUpdateEventListener
|
||||
implements ApplicationListener<QueuedUpdateEvent>, DataEventListener<QueuedUpdateEvent> {
|
||||
|
||||
private final Sinks.Many<QueuedUpdateEvent> sink =
|
||||
Sinks.many().multicast().onBackpressureBuffer();
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(QueuedUpdateEvent event) {
|
||||
sink.emitNext(event, (signalType, emitResult) -> true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<QueuedUpdateEvent> getDataFlux() {
|
||||
return sink.asFlux();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import tn.mnlr.vripper.jpa.repositories.IImageRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.IMetadataRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.IPostRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.IQueuedRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.impl.EventRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.impl.LogEventRepository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -27,7 +27,7 @@ public class DataService {
|
||||
private final IQueuedRepository queuedRepository;
|
||||
private final IMetadataRepository metadataRepository;
|
||||
private final SettingsService settingsService;
|
||||
private final EventRepository eventRepository;
|
||||
private final LogEventRepository eventRepository;
|
||||
|
||||
@Autowired
|
||||
public DataService(
|
||||
@@ -36,7 +36,7 @@ public class DataService {
|
||||
IQueuedRepository queuedRepository,
|
||||
IMetadataRepository metadataRepository,
|
||||
SettingsService settingsService,
|
||||
EventRepository eventRepository) {
|
||||
LogEventRepository eventRepository) {
|
||||
this.postRepository = postRepository;
|
||||
this.imageRepository = imageRepository;
|
||||
this.queuedRepository = queuedRepository;
|
||||
@@ -219,11 +219,11 @@ public class DataService {
|
||||
postRepository.updateThanked(thanked, id);
|
||||
}
|
||||
|
||||
public Optional<Event> findEventById(Long id) {
|
||||
public Optional<LogEvent> findEventById(Long id) {
|
||||
return eventRepository.findById(id);
|
||||
}
|
||||
|
||||
public List<Event> findAllEvents() {
|
||||
public List<LogEvent> findAllEvents() {
|
||||
return eventRepository.findAll();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,11 +4,9 @@ import lombok.Getter;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import tn.mnlr.vripper.listener.EmitHandler;
|
||||
import tn.mnlr.vripper.event.Event;
|
||||
import tn.mnlr.vripper.event.EventBus;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@Service
|
||||
@@ -16,12 +14,12 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
public class DownloadSpeedService {
|
||||
|
||||
private final AtomicLong read = new AtomicLong(0);
|
||||
private final Sinks.Many<Long> sink = Sinks.many().multicast().onBackpressureBuffer();
|
||||
private final EventBus eventBus;
|
||||
@Getter private long currentValue;
|
||||
private boolean allowWrite = false;
|
||||
|
||||
public Flux<Long> getReadBytesPerSecond() {
|
||||
return sink.asFlux();
|
||||
public DownloadSpeedService(EventBus eventBus) {
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
public void increase(long read) {
|
||||
@@ -36,13 +34,8 @@ public class DownloadSpeedService {
|
||||
long newValue = read.getAndSet(0);
|
||||
if (newValue != currentValue) {
|
||||
currentValue = newValue;
|
||||
sink.emitNext(currentValue, EmitHandler.RETRY);
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.BYTES_PER_SECOND, currentValue));
|
||||
}
|
||||
allowWrite = true;
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(EmitHandler.RETRY);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,15 +5,12 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import tn.mnlr.vripper.download.DownloadService;
|
||||
import tn.mnlr.vripper.download.PendingQueue;
|
||||
import tn.mnlr.vripper.listener.EmitHandler;
|
||||
import tn.mnlr.vripper.event.Event;
|
||||
import tn.mnlr.vripper.event.EventBus;
|
||||
import tn.mnlr.vripper.services.domain.GlobalState;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
@Service
|
||||
@EnableScheduling
|
||||
public class GlobalStateService {
|
||||
@@ -21,19 +18,19 @@ public class GlobalStateService {
|
||||
private final PendingQueue pendingQueue;
|
||||
private final DownloadService downloadService;
|
||||
private final DataService dataService;
|
||||
private final Sinks.Many<GlobalState> sink = Sinks.many().multicast().onBackpressureBuffer();
|
||||
private final EventBus eventBus;
|
||||
@Getter private GlobalState currentState;
|
||||
|
||||
@Autowired
|
||||
public GlobalStateService(
|
||||
PendingQueue pendingQueue, DownloadService downloadService, DataService dataService) {
|
||||
PendingQueue pendingQueue,
|
||||
DownloadService downloadService,
|
||||
DataService dataService,
|
||||
EventBus eventBus) {
|
||||
this.pendingQueue = pendingQueue;
|
||||
this.downloadService = downloadService;
|
||||
this.dataService = dataService;
|
||||
}
|
||||
|
||||
public Flux<GlobalState> getGlobalState() {
|
||||
return sink.asFlux();
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 3000)
|
||||
@@ -43,12 +40,7 @@ public class GlobalStateService {
|
||||
downloadService.runningCount(), pendingQueue.size(), dataService.countErrorImages());
|
||||
if (!newGlobalState.equals(currentState)) {
|
||||
currentState = newGlobalState;
|
||||
sink.emitNext(currentState, EmitHandler.RETRY);
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.GLOBAL_STATE, currentState));
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(EmitHandler.RETRY);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,11 +15,10 @@ import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.Disposable;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import tn.mnlr.vripper.event.Event;
|
||||
import tn.mnlr.vripper.event.EventBus;
|
||||
import tn.mnlr.vripper.exception.VripperException;
|
||||
import tn.mnlr.vripper.jpa.domain.Post;
|
||||
import tn.mnlr.vripper.listener.EmitHandler;
|
||||
import tn.mnlr.vripper.services.domain.tasks.LeaveThanksRunnable;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
@@ -38,17 +37,22 @@ public class VGAuthService {
|
||||
private final Disposable disposable;
|
||||
|
||||
@Getter private final HttpClientContext context = HttpClientContext.create();
|
||||
private final Sinks.Many<String> sink = Sinks.many().multicast().onBackpressureBuffer();
|
||||
@Getter private boolean authenticated = false;
|
||||
@Getter private String loggedUser = "";
|
||||
|
||||
private final EventBus eventBus;
|
||||
|
||||
@Autowired
|
||||
public VGAuthService(
|
||||
ConnectionService cm, SettingsService settingsService, ThreadPoolService threadPoolService) {
|
||||
ConnectionService cm,
|
||||
SettingsService settingsService,
|
||||
ThreadPoolService threadPoolService,
|
||||
EventBus eventBus) {
|
||||
this.cm = cm;
|
||||
this.settingsService = settingsService;
|
||||
this.threadPoolService = threadPoolService;
|
||||
disposable = settingsService.getSettingsFlux().subscribe(settings -> this.authenticate());
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
@@ -59,14 +63,9 @@ public class VGAuthService {
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
sink.emitComplete(EmitHandler.RETRY);
|
||||
disposable.dispose();
|
||||
}
|
||||
|
||||
public Flux<String> getLoggedInUser() {
|
||||
return sink.asFlux();
|
||||
}
|
||||
|
||||
public void authenticate() {
|
||||
|
||||
log.info("Authenticating using ViperGirls credentials");
|
||||
@@ -76,7 +75,7 @@ public class VGAuthService {
|
||||
log.debug("Authentication option is disabled");
|
||||
context.getCookieStore().clear();
|
||||
loggedUser = "";
|
||||
sink.emitNext(loggedUser, EmitHandler.RETRY);
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.VG_USER, loggedUser));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -87,7 +86,7 @@ public class VGAuthService {
|
||||
log.error("Cannot authenticate with ViperGirls credentials, username or password is empty");
|
||||
context.getCookieStore().clear();
|
||||
loggedUser = "";
|
||||
sink.emitNext(loggedUser, EmitHandler.RETRY);
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.VG_USER, loggedUser));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -104,7 +103,7 @@ public class VGAuthService {
|
||||
} catch (Exception e) {
|
||||
context.getCookieStore().clear();
|
||||
loggedUser = "";
|
||||
sink.emitNext(loggedUser, EmitHandler.RETRY);
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.VG_USER, loggedUser));
|
||||
log.error("Failed to authenticate user with " + settingsService.getSettings().getVProxy(), e);
|
||||
return;
|
||||
}
|
||||
@@ -138,14 +137,14 @@ public class VGAuthService {
|
||||
} catch (Exception e) {
|
||||
context.getCookieStore().clear();
|
||||
loggedUser = "";
|
||||
sink.emitNext(loggedUser, EmitHandler.RETRY);
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.VG_USER, loggedUser));
|
||||
log.error("Failed to authenticate user with " + settingsService.getSettings().getVProxy(), e);
|
||||
return;
|
||||
}
|
||||
authenticated = true;
|
||||
loggedUser = username;
|
||||
log.info(String.format("Authenticated: %s", username));
|
||||
sink.emitNext(loggedUser, EmitHandler.RETRY);
|
||||
eventBus.publishEvent(Event.wrap(Event.Kind.VG_USER, loggedUser));
|
||||
}
|
||||
|
||||
public void leaveThanks(Post post) {
|
||||
|
||||
@@ -2,6 +2,8 @@ package tn.mnlr.vripper.services.domain;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@Getter
|
||||
public class DownloadSpeed {
|
||||
|
||||
@@ -22,4 +24,17 @@ public class DownloadSpeed {
|
||||
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
|
||||
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
DownloadSpeed that = (DownloadSpeed) o;
|
||||
return Objects.equals(speed, that.speed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(speed);
|
||||
}
|
||||
}
|
||||
|
||||
+33
-32
@@ -5,11 +5,11 @@ import tn.mnlr.vripper.SpringContext;
|
||||
import tn.mnlr.vripper.Utils;
|
||||
import tn.mnlr.vripper.download.PendingQueue;
|
||||
import tn.mnlr.vripper.exception.PostParseException;
|
||||
import tn.mnlr.vripper.jpa.domain.Event;
|
||||
import tn.mnlr.vripper.jpa.domain.Image;
|
||||
import tn.mnlr.vripper.jpa.domain.LogEvent;
|
||||
import tn.mnlr.vripper.jpa.domain.Post;
|
||||
import tn.mnlr.vripper.jpa.domain.enums.Status;
|
||||
import tn.mnlr.vripper.jpa.repositories.IEventRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.ILogEventRepository;
|
||||
import tn.mnlr.vripper.services.DataService;
|
||||
import tn.mnlr.vripper.services.MetadataService;
|
||||
import tn.mnlr.vripper.services.SettingsService;
|
||||
@@ -20,8 +20,8 @@ import tn.mnlr.vripper.services.domain.PostScanResult;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Set;
|
||||
|
||||
import static tn.mnlr.vripper.jpa.domain.Event.Status.ERROR;
|
||||
import static tn.mnlr.vripper.jpa.domain.Event.Status.PROCESSING;
|
||||
import static tn.mnlr.vripper.jpa.domain.LogEvent.Status.ERROR;
|
||||
import static tn.mnlr.vripper.jpa.domain.LogEvent.Status.PROCESSING;
|
||||
|
||||
@Slf4j
|
||||
public class AddPostRunnable implements Runnable {
|
||||
@@ -33,8 +33,8 @@ public class AddPostRunnable implements Runnable {
|
||||
private final SettingsService settingsService;
|
||||
private final PendingQueue pendingQueue;
|
||||
private final VGAuthService VGAuthService;
|
||||
private final Event event;
|
||||
private final IEventRepository eventRepository;
|
||||
private final LogEvent logEvent;
|
||||
private final ILogEventRepository eventRepository;
|
||||
private final String link;
|
||||
|
||||
public AddPostRunnable(String postId, String threadId) {
|
||||
@@ -45,30 +45,30 @@ public class AddPostRunnable implements Runnable {
|
||||
this.settingsService = SpringContext.getBean(SettingsService.class);
|
||||
this.pendingQueue = SpringContext.getBean(PendingQueue.class);
|
||||
this.VGAuthService = SpringContext.getBean(VGAuthService.class);
|
||||
this.eventRepository = SpringContext.getBean(IEventRepository.class);
|
||||
this.eventRepository = SpringContext.getBean(ILogEventRepository.class);
|
||||
link =
|
||||
settingsService.getSettings().getVProxy()
|
||||
+ String.format("/threads/%s?%s", threadId, (postId != null ? "p=" + postId : ""));
|
||||
event =
|
||||
new Event(
|
||||
Event.Type.POST,
|
||||
Event.Status.PENDING,
|
||||
logEvent =
|
||||
new LogEvent(
|
||||
LogEvent.Type.POST,
|
||||
LogEvent.Status.PENDING,
|
||||
LocalDateTime.now(),
|
||||
String.format("Processing %s", link));
|
||||
eventRepository.save(event);
|
||||
eventRepository.save(logEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
event.setStatus(PROCESSING);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(PROCESSING);
|
||||
eventRepository.update(logEvent);
|
||||
if (dataService.exists(postId)) {
|
||||
log.warn(String.format("skipping %s, already loaded", postId));
|
||||
event.setMessage(String.format("Gallery %s is already loaded", link));
|
||||
event.setStatus(ERROR);
|
||||
eventRepository.update(event);
|
||||
logEvent.setMessage(String.format("Gallery %s is already loaded", link));
|
||||
logEvent.setStatus(ERROR);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -80,25 +80,25 @@ public class AddPostRunnable implements Runnable {
|
||||
} catch (PostParseException e) {
|
||||
String error = String.format("parsing failed for gallery %s", link);
|
||||
log.error(error, e);
|
||||
event.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
event.setStatus(ERROR);
|
||||
eventRepository.update(event);
|
||||
logEvent.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
logEvent.setStatus(ERROR);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
if (postScanResult.getPost().isEmpty()) {
|
||||
String error = String.format("Gallery %s contains no galleries", link);
|
||||
log.error(error);
|
||||
event.setMessage(error);
|
||||
event.setStatus(ERROR);
|
||||
eventRepository.update(event);
|
||||
logEvent.setMessage(error);
|
||||
logEvent.setStatus(ERROR);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
if (postScanResult.getImages().isEmpty()) {
|
||||
String error = String.format("Gallery %s contains no images to download", link);
|
||||
log.error(error);
|
||||
event.setMessage(error);
|
||||
event.setStatus(ERROR);
|
||||
eventRepository.update(event);
|
||||
logEvent.setMessage(error);
|
||||
logEvent.setStatus(ERROR);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -129,15 +129,16 @@ public class AddPostRunnable implements Runnable {
|
||||
VGAuthService.leaveThanks(post);
|
||||
}
|
||||
dataService.updatePostStatus(post.getStatus(), post.getId());
|
||||
event.setMessage(String.format("Gallery %s is successfully added to download queue", link));
|
||||
event.setStatus(Event.Status.DONE);
|
||||
eventRepository.update(event);
|
||||
logEvent.setMessage(
|
||||
String.format("Gallery %s is successfully added to download queue", link));
|
||||
logEvent.setStatus(LogEvent.Status.DONE);
|
||||
eventRepository.update(logEvent);
|
||||
} catch (Exception e) {
|
||||
String error = String.format("Error when adding gallery %s", link);
|
||||
log.error(error, e);
|
||||
event.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
event.setStatus(ERROR);
|
||||
eventRepository.update(event);
|
||||
logEvent.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
logEvent.setStatus(ERROR);
|
||||
eventRepository.update(logEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+34
-33
@@ -3,9 +3,9 @@ package tn.mnlr.vripper.services.domain.tasks;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import tn.mnlr.vripper.SpringContext;
|
||||
import tn.mnlr.vripper.Utils;
|
||||
import tn.mnlr.vripper.jpa.domain.Event;
|
||||
import tn.mnlr.vripper.jpa.domain.LogEvent;
|
||||
import tn.mnlr.vripper.jpa.domain.Queued;
|
||||
import tn.mnlr.vripper.jpa.repositories.IEventRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.ILogEventRepository;
|
||||
import tn.mnlr.vripper.services.DataService;
|
||||
import tn.mnlr.vripper.services.PostService;
|
||||
import tn.mnlr.vripper.services.ThreadPoolService;
|
||||
@@ -13,8 +13,8 @@ import tn.mnlr.vripper.services.domain.MultiPostScanResult;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static tn.mnlr.vripper.jpa.domain.Event.Status.ERROR;
|
||||
import static tn.mnlr.vripper.jpa.domain.Event.Status.PROCESSING;
|
||||
import static tn.mnlr.vripper.jpa.domain.LogEvent.Status.ERROR;
|
||||
import static tn.mnlr.vripper.jpa.domain.LogEvent.Status.PROCESSING;
|
||||
|
||||
@Slf4j
|
||||
public class AddQueuedRunnable implements Runnable {
|
||||
@@ -22,49 +22,49 @@ public class AddQueuedRunnable implements Runnable {
|
||||
private final Queued queued;
|
||||
private final ThreadPoolService threadPoolService;
|
||||
private final DataService dataService;
|
||||
private final IEventRepository eventRepository;
|
||||
private final ILogEventRepository eventRepository;
|
||||
private final PostService postService;
|
||||
private final Event event;
|
||||
private final LogEvent logEvent;
|
||||
|
||||
public AddQueuedRunnable(Queued queued) {
|
||||
this.queued = queued;
|
||||
threadPoolService = SpringContext.getBean(ThreadPoolService.class);
|
||||
dataService = SpringContext.getBean(DataService.class);
|
||||
eventRepository = SpringContext.getBean(IEventRepository.class);
|
||||
eventRepository = SpringContext.getBean(ILogEventRepository.class);
|
||||
postService = SpringContext.getBean(PostService.class);
|
||||
event =
|
||||
new Event(
|
||||
Event.Type.QUEUED,
|
||||
Event.Status.PENDING,
|
||||
logEvent =
|
||||
new LogEvent(
|
||||
LogEvent.Type.QUEUED,
|
||||
LogEvent.Status.PENDING,
|
||||
LocalDateTime.now(),
|
||||
String.format("Processing multi-post link %s", queued.getLink()));
|
||||
eventRepository.save(event);
|
||||
eventRepository.save(logEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
event.setStatus(PROCESSING);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(PROCESSING);
|
||||
eventRepository.update(logEvent);
|
||||
MultiPostScanResult multiPostScanResult = postService.get(queued);
|
||||
if (multiPostScanResult == null) {
|
||||
String message = String.format("Fetching multi-post link %s failed", queued.getLink());
|
||||
event.setStatus(ERROR);
|
||||
event.setMessage(message);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(ERROR);
|
||||
logEvent.setMessage(message);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
} else if (multiPostScanResult.getError() != null) {
|
||||
String message =
|
||||
"Nothing found for " + queued.getLink() + "\n" + multiPostScanResult.getError();
|
||||
event.setStatus(ERROR);
|
||||
event.setMessage(message);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(ERROR);
|
||||
logEvent.setMessage(message);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
} else if (multiPostScanResult.getPosts().isEmpty()) {
|
||||
String message = "Nothing found for " + queued.getLink();
|
||||
event.setStatus(ERROR);
|
||||
event.setMessage(message);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(ERROR);
|
||||
logEvent.setMessage(message);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
queued.setTotal(multiPostScanResult.getPosts().size());
|
||||
@@ -76,27 +76,28 @@ public class AddQueuedRunnable implements Runnable {
|
||||
new AddPostRunnable(
|
||||
multiPostScanResult.getPosts().get(0).getPostId(),
|
||||
multiPostScanResult.getPosts().get(0).getThreadId()));
|
||||
event.setStatus(Event.Status.DONE);
|
||||
event.setMessage(String.format("Link %s is added to download queue", queued.getLink()));
|
||||
logEvent.setStatus(LogEvent.Status.DONE);
|
||||
logEvent.setMessage(String.format("Link %s is added to download queue", queued.getLink()));
|
||||
} else {
|
||||
if (dataService.findQueuedByThreadId(queued.getThreadId()).isEmpty()) {
|
||||
dataService.newQueueLink(queued);
|
||||
event.setStatus(Event.Status.DONE);
|
||||
event.setMessage(String.format("Link %s is added to multi-post links", queued.getLink()));
|
||||
logEvent.setStatus(LogEvent.Status.DONE);
|
||||
logEvent.setMessage(
|
||||
String.format("Link %s is added to multi-post links", queued.getLink()));
|
||||
} else {
|
||||
log.info(String.format("Link %s is already loaded", queued.getLink()));
|
||||
event.setStatus(Event.Status.ERROR);
|
||||
event.setMessage(
|
||||
logEvent.setStatus(LogEvent.Status.ERROR);
|
||||
logEvent.setMessage(
|
||||
String.format("%s has already been added to multi-post links", queued.getLink()));
|
||||
}
|
||||
}
|
||||
eventRepository.update(event);
|
||||
eventRepository.update(logEvent);
|
||||
} catch (Exception e) {
|
||||
String error = String.format("Error when adding multi-post link %s", queued.getLink());
|
||||
log.error(error, e);
|
||||
event.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
event.setStatus(ERROR);
|
||||
eventRepository.update(event);
|
||||
logEvent.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
logEvent.setStatus(ERROR);
|
||||
eventRepository.update(logEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+32
-32
@@ -11,9 +11,9 @@ import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import tn.mnlr.vripper.SpringContext;
|
||||
import tn.mnlr.vripper.Utils;
|
||||
import tn.mnlr.vripper.jpa.domain.Event;
|
||||
import tn.mnlr.vripper.jpa.domain.LogEvent;
|
||||
import tn.mnlr.vripper.jpa.domain.Post;
|
||||
import tn.mnlr.vripper.jpa.repositories.IEventRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.ILogEventRepository;
|
||||
import tn.mnlr.vripper.services.ConnectionService;
|
||||
import tn.mnlr.vripper.services.DataService;
|
||||
import tn.mnlr.vripper.services.SettingsService;
|
||||
@@ -30,10 +30,10 @@ public class LeaveThanksRunnable implements Runnable {
|
||||
private final DataService dataService;
|
||||
private final HttpClientContext context;
|
||||
private final SettingsService settingsService;
|
||||
private final IEventRepository eventRepository;
|
||||
private final ILogEventRepository eventRepository;
|
||||
private final boolean authenticated;
|
||||
private final Post post;
|
||||
private final Event event;
|
||||
private final LogEvent logEvent;
|
||||
|
||||
public LeaveThanksRunnable(Post post, boolean authenticated, HttpClientContext context) {
|
||||
this.post = post;
|
||||
@@ -41,51 +41,51 @@ public class LeaveThanksRunnable implements Runnable {
|
||||
this.context = context;
|
||||
cm = SpringContext.getBean(ConnectionService.class);
|
||||
dataService = SpringContext.getBean(DataService.class);
|
||||
eventRepository = SpringContext.getBean(IEventRepository.class);
|
||||
eventRepository = SpringContext.getBean(ILogEventRepository.class);
|
||||
settingsService = SpringContext.getBean(SettingsService.class);
|
||||
event =
|
||||
new Event(
|
||||
Event.Type.THANKS,
|
||||
Event.Status.PENDING,
|
||||
logEvent =
|
||||
new LogEvent(
|
||||
LogEvent.Type.THANKS,
|
||||
LogEvent.Status.PENDING,
|
||||
LocalDateTime.now(),
|
||||
String.format("Leaving thanks for %s", post.getUrl()));
|
||||
eventRepository.save(event);
|
||||
eventRepository.save(logEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
event.setStatus(Event.Status.PROCESSING);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(LogEvent.Status.PROCESSING);
|
||||
eventRepository.update(logEvent);
|
||||
if (!settingsService.getSettings().getVLogin()) {
|
||||
event.setMessage(
|
||||
logEvent.setMessage(
|
||||
String.format(
|
||||
"Will not send a like for %s\nAuthentication with ViperGirls option is disabled",
|
||||
post.getUrl()));
|
||||
event.setStatus(Event.Status.DONE);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(LogEvent.Status.DONE);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
if (!settingsService.getSettings().getVThanks()) {
|
||||
event.setMessage(
|
||||
logEvent.setMessage(
|
||||
String.format(
|
||||
"Will not send a like for %s\nLeave thanks option is disabled", post.getUrl()));
|
||||
event.setStatus(Event.Status.DONE);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(LogEvent.Status.DONE);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
if (!authenticated) {
|
||||
event.setMessage(
|
||||
logEvent.setMessage(
|
||||
String.format("Will not send a like for %s\nYou are not authenticated", post.getUrl()));
|
||||
event.setStatus(Event.Status.ERROR);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(LogEvent.Status.ERROR);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
if (post.isThanked()) {
|
||||
event.setMessage(
|
||||
logEvent.setMessage(
|
||||
String.format("Will not send a like for %s\nAlready left a thanks", post.getUrl()));
|
||||
event.setStatus(Event.Status.DONE);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(LogEvent.Status.DONE);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -101,9 +101,9 @@ public class LeaveThanksRunnable implements Runnable {
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
String error = String.format("Request error for %s", post.getUrl());
|
||||
log.error(error, e);
|
||||
event.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
event.setStatus(Event.Status.ERROR);
|
||||
eventRepository.update(event);
|
||||
logEvent.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
logEvent.setStatus(LogEvent.Status.ERROR);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -121,14 +121,14 @@ public class LeaveThanksRunnable implements Runnable {
|
||||
}
|
||||
EntityUtils.consumeQuietly(response.getEntity());
|
||||
}
|
||||
event.setStatus(Event.Status.DONE);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(LogEvent.Status.DONE);
|
||||
eventRepository.update(logEvent);
|
||||
} catch (Exception e) {
|
||||
String error = String.format("Failed to leave a thanks for %s", post.getUrl());
|
||||
log.error(error, e);
|
||||
event.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
event.setStatus(Event.Status.ERROR);
|
||||
eventRepository.update(event);
|
||||
logEvent.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
logEvent.setStatus(LogEvent.Status.ERROR);
|
||||
eventRepository.update(logEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+20
-20
@@ -4,9 +4,9 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.lang.NonNull;
|
||||
import tn.mnlr.vripper.SpringContext;
|
||||
import tn.mnlr.vripper.Utils;
|
||||
import tn.mnlr.vripper.jpa.domain.Event;
|
||||
import tn.mnlr.vripper.jpa.domain.LogEvent;
|
||||
import tn.mnlr.vripper.jpa.domain.Queued;
|
||||
import tn.mnlr.vripper.jpa.repositories.impl.EventRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.impl.LogEventRepository;
|
||||
import tn.mnlr.vripper.services.PostService;
|
||||
import tn.mnlr.vripper.services.SettingsService;
|
||||
|
||||
@@ -16,7 +16,7 @@ import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static tn.mnlr.vripper.jpa.domain.Event.Status.ERROR;
|
||||
import static tn.mnlr.vripper.jpa.domain.LogEvent.Status.ERROR;
|
||||
|
||||
@Slf4j
|
||||
public class LinkScanRunnable implements Runnable {
|
||||
@@ -25,29 +25,29 @@ public class LinkScanRunnable implements Runnable {
|
||||
private final List<String> urlList;
|
||||
private final SettingsService settingsService;
|
||||
private final PostService postService;
|
||||
private final EventRepository eventRepository;
|
||||
private final Event event;
|
||||
private final LogEventRepository eventRepository;
|
||||
private final LogEvent logEvent;
|
||||
|
||||
public LinkScanRunnable(@NonNull List<String> urlList) {
|
||||
this.urlList = urlList;
|
||||
settingsService = SpringContext.getBean(SettingsService.class);
|
||||
postService = SpringContext.getBean(PostService.class);
|
||||
eventRepository = SpringContext.getBean(EventRepository.class);
|
||||
event =
|
||||
new Event(
|
||||
Event.Type.SCAN,
|
||||
Event.Status.PENDING,
|
||||
eventRepository = SpringContext.getBean(LogEventRepository.class);
|
||||
logEvent =
|
||||
new LogEvent(
|
||||
LogEvent.Type.SCAN,
|
||||
LogEvent.Status.PENDING,
|
||||
LocalDateTime.now(),
|
||||
"Links to scan:\n\t" + String.join("\n\t", urlList));
|
||||
eventRepository.save(event);
|
||||
eventRepository.save(logEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized (LOCK) {
|
||||
try {
|
||||
event.setStatus(Event.Status.PROCESSING);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(LogEvent.Status.PROCESSING);
|
||||
eventRepository.update(logEvent);
|
||||
ArrayList<Queued> queuedList = new ArrayList<>();
|
||||
List<String> unsupported = new ArrayList<>();
|
||||
List<String> unrecognized = new ArrayList<>();
|
||||
@@ -91,18 +91,18 @@ public class LinkScanRunnable implements Runnable {
|
||||
|
||||
postService.processMultiPost(queuedList);
|
||||
if (!unsupported.isEmpty() || !unrecognized.isEmpty()) {
|
||||
event.setStatus(Event.Status.ERROR);
|
||||
event.setMessage("Some links failed to be scanned: \n" + errorMessage);
|
||||
logEvent.setStatus(LogEvent.Status.ERROR);
|
||||
logEvent.setMessage("Some links failed to be scanned: \n" + errorMessage);
|
||||
} else {
|
||||
event.setStatus(Event.Status.DONE);
|
||||
logEvent.setStatus(LogEvent.Status.DONE);
|
||||
}
|
||||
eventRepository.update(event);
|
||||
eventRepository.update(logEvent);
|
||||
} catch (Exception e) {
|
||||
String error = "Error when scanning links";
|
||||
log.error(error, e);
|
||||
event.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
event.setStatus(ERROR);
|
||||
eventRepository.update(event);
|
||||
logEvent.setMessage(error + "\n" + Utils.throwableToString(e));
|
||||
logEvent.setStatus(ERROR);
|
||||
eventRepository.update(logEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+24
-24
@@ -6,16 +6,16 @@ import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import tn.mnlr.vripper.SpringContext;
|
||||
import tn.mnlr.vripper.Utils;
|
||||
import tn.mnlr.vripper.jpa.domain.Event;
|
||||
import tn.mnlr.vripper.jpa.domain.LogEvent;
|
||||
import tn.mnlr.vripper.jpa.domain.Metadata;
|
||||
import tn.mnlr.vripper.jpa.domain.Post;
|
||||
import tn.mnlr.vripper.jpa.repositories.IEventRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.ILogEventRepository;
|
||||
import tn.mnlr.vripper.services.DataService;
|
||||
import tn.mnlr.vripper.services.MetadataService;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static tn.mnlr.vripper.jpa.domain.Event.Status.ERROR;
|
||||
import static tn.mnlr.vripper.jpa.domain.LogEvent.Status.ERROR;
|
||||
|
||||
@Slf4j
|
||||
public class MetadataRunnable implements Runnable {
|
||||
@@ -24,9 +24,9 @@ public class MetadataRunnable implements Runnable {
|
||||
|
||||
private final MetadataService metadataService;
|
||||
private final DataService dataService;
|
||||
private final IEventRepository eventRepository;
|
||||
private final ILogEventRepository eventRepository;
|
||||
|
||||
private final Event event;
|
||||
private final LogEvent logEvent;
|
||||
|
||||
@Setter private volatile boolean interrupted = false;
|
||||
|
||||
@@ -34,48 +34,48 @@ public class MetadataRunnable implements Runnable {
|
||||
this.post = post;
|
||||
metadataService = SpringContext.getBean(MetadataService.class);
|
||||
dataService = SpringContext.getBean(DataService.class);
|
||||
eventRepository = SpringContext.getBean(IEventRepository.class);
|
||||
event =
|
||||
new Event(
|
||||
Event.Type.METADATA,
|
||||
Event.Status.PENDING,
|
||||
eventRepository = SpringContext.getBean(ILogEventRepository.class);
|
||||
logEvent =
|
||||
new LogEvent(
|
||||
LogEvent.Type.METADATA,
|
||||
LogEvent.Status.PENDING,
|
||||
LocalDateTime.now(),
|
||||
"Fetching metadata for " + post.getUrl());
|
||||
eventRepository.save(event);
|
||||
eventRepository.save(logEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
event.setStatus(Event.Status.PROCESSING);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(LogEvent.Status.PROCESSING);
|
||||
eventRepository.update(logEvent);
|
||||
if (interrupted) {
|
||||
String message = String.format("Fetching metadata for %s interrupted", post.getUrl());
|
||||
event.setStatus(Event.Status.DONE);
|
||||
event.setMessage(message);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(LogEvent.Status.DONE);
|
||||
logEvent.setMessage(message);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
|
||||
Metadata metadata = metadataService.get(post);
|
||||
if (metadata == null) {
|
||||
String message = String.format("Fetching metadata for %s failed", post.getUrl());
|
||||
event.setStatus(ERROR);
|
||||
event.setMessage(message);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(ERROR);
|
||||
logEvent.setMessage(message);
|
||||
eventRepository.update(logEvent);
|
||||
return;
|
||||
}
|
||||
dataService.setMetadata(post, metadata);
|
||||
|
||||
event.setStatus(Event.Status.DONE);
|
||||
eventRepository.update(event);
|
||||
logEvent.setStatus(LogEvent.Status.DONE);
|
||||
eventRepository.update(logEvent);
|
||||
} catch (Exception e) {
|
||||
String message = String.format("Failed to fetch metadata for %s", post.getUrl());
|
||||
log.error(message, e);
|
||||
event.setMessage(message + "\n" + Utils.throwableToString(e));
|
||||
event.setStatus(ERROR);
|
||||
eventRepository.update(event);
|
||||
logEvent.setMessage(message + "\n" + Utils.throwableToString(e));
|
||||
logEvent.setStatus(ERROR);
|
||||
eventRepository.update(logEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -6,16 +6,16 @@ import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import tn.mnlr.vripper.jpa.repositories.IEventRepository;
|
||||
import tn.mnlr.vripper.jpa.repositories.ILogEventRepository;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@CrossOrigin(value = "*")
|
||||
public class EventLogRestEndpoint {
|
||||
|
||||
private final IEventRepository eventRepository;
|
||||
private final ILogEventRepository eventRepository;
|
||||
|
||||
public EventLogRestEndpoint(IEventRepository eventRepository) {
|
||||
public EventLogRestEndpoint(ILogEventRepository eventRepository) {
|
||||
this.eventRepository = eventRepository;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,22 +5,21 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.Disposable;
|
||||
import tn.mnlr.vripper.event.*;
|
||||
import tn.mnlr.vripper.event.Event;
|
||||
import tn.mnlr.vripper.event.EventBus;
|
||||
import tn.mnlr.vripper.jpa.domain.Image;
|
||||
import tn.mnlr.vripper.listener.*;
|
||||
import tn.mnlr.vripper.services.DataService;
|
||||
import tn.mnlr.vripper.services.DownloadSpeedService;
|
||||
import tn.mnlr.vripper.services.GlobalStateService;
|
||||
import tn.mnlr.vripper.services.VGAuthService;
|
||||
import tn.mnlr.vripper.services.domain.DownloadSpeed;
|
||||
import tn.mnlr.vripper.services.domain.GlobalState;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -29,204 +28,138 @@ import java.util.stream.Collectors;
|
||||
public class DataBroadcast {
|
||||
|
||||
private final SimpMessagingTemplate template;
|
||||
private final VGAuthService VGAuthService;
|
||||
private final GlobalStateService globalStateService;
|
||||
private final DownloadSpeedService downloadSpeedService;
|
||||
private final DataService dataService;
|
||||
private final EventBus eventBus;
|
||||
|
||||
private final PostUpdateEventListener postUpdateEventListener;
|
||||
private final MetadataUpdateEventListener metadataUpdateEventListener;
|
||||
private final ImageUpdateEventListener imageUpdateEventListener;
|
||||
private final QueuedUpdateEventListener queuedUpdateEventListener;
|
||||
private final EventUpdateEventListener eventUpdateEventListener;
|
||||
|
||||
private final QueuedRemoveEventListener queuedRemoveEventListener;
|
||||
private final PostRemoveEventListener postRemoveEventListener;
|
||||
private final EventRemoveEventListener eventRemoveEventListener;
|
||||
|
||||
private final List<Disposable> disposables = new ArrayList<>();
|
||||
private Disposable disposable;
|
||||
|
||||
@Autowired
|
||||
public DataBroadcast(
|
||||
SimpMessagingTemplate template,
|
||||
VGAuthService VGAuthService,
|
||||
GlobalStateService globalStateService,
|
||||
DownloadSpeedService downloadSpeedService,
|
||||
DataService dataService,
|
||||
PostUpdateEventListener postUpdateEventListener,
|
||||
MetadataUpdateEventListener metadataUpdateEventListener,
|
||||
ImageUpdateEventListener imageUpdateEventListener,
|
||||
QueuedUpdateEventListener queuedUpdateEventListener,
|
||||
EventUpdateEventListener eventUpdateEventListener,
|
||||
QueuedRemoveEventListener queuedRemoveEventListener,
|
||||
PostRemoveEventListener postRemoveEventListener,
|
||||
EventRemoveEventListener eventRemoveEventListener) {
|
||||
EventBus eventBus) {
|
||||
this.template = template;
|
||||
this.VGAuthService = VGAuthService;
|
||||
this.globalStateService = globalStateService;
|
||||
this.downloadSpeedService = downloadSpeedService;
|
||||
this.dataService = dataService;
|
||||
this.postUpdateEventListener = postUpdateEventListener;
|
||||
this.metadataUpdateEventListener = metadataUpdateEventListener;
|
||||
this.imageUpdateEventListener = imageUpdateEventListener;
|
||||
this.queuedUpdateEventListener = queuedUpdateEventListener;
|
||||
this.eventUpdateEventListener = eventUpdateEventListener;
|
||||
this.queuedRemoveEventListener = queuedRemoveEventListener;
|
||||
this.postRemoveEventListener = postRemoveEventListener;
|
||||
this.eventRemoveEventListener = eventRemoveEventListener;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void run() {
|
||||
disposables.add(
|
||||
VGAuthService.getLoggedInUser()
|
||||
.map(DataController.LoggedUser::new)
|
||||
.subscribe(
|
||||
user -> template.convertAndSend("/topic/user", user),
|
||||
e -> log.error("Failed to send data to client", e)));
|
||||
|
||||
disposables.add(
|
||||
globalStateService
|
||||
.getGlobalState()
|
||||
.subscribe(
|
||||
state -> template.convertAndSend("/topic/download-state", state),
|
||||
e -> log.error("Failed to send data to client", e)));
|
||||
|
||||
disposables.add(
|
||||
downloadSpeedService
|
||||
.getReadBytesPerSecond()
|
||||
.map(DownloadSpeed::new)
|
||||
.subscribe(
|
||||
speed -> template.convertAndSend("/topic/speed", speed),
|
||||
e -> log.error("Failed to send data to client", e)));
|
||||
|
||||
disposables.add(
|
||||
postUpdateEventListener
|
||||
.getDataFlux()
|
||||
.map(PostUpdateEvent::getId)
|
||||
disposable =
|
||||
eventBus
|
||||
.flux()
|
||||
.buffer(Duration.of(500, ChronoUnit.MILLIS))
|
||||
.map(HashSet::new)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.subscribe(
|
||||
ids ->
|
||||
template.convertAndSend(
|
||||
"/topic/posts",
|
||||
ids.stream()
|
||||
.map(dataService::findById)
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.collect(Collectors.toList())),
|
||||
e -> log.error("Failed to send data to client", e)));
|
||||
data -> {
|
||||
Map<Event.Kind, List<Event>> eventMap =
|
||||
data.stream().collect(Collectors.groupingBy(Event::getKind));
|
||||
|
||||
disposables.add(
|
||||
metadataUpdateEventListener
|
||||
.getDataFlux()
|
||||
.map(MetadataUpdateEvent::getPostIdRef)
|
||||
.buffer(Duration.of(500, ChronoUnit.MILLIS))
|
||||
.map(HashSet::new)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.subscribe(
|
||||
ids ->
|
||||
template.convertAndSend(
|
||||
"/topic/posts",
|
||||
ids.stream()
|
||||
.map(dataService::findById)
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.collect(Collectors.toList())),
|
||||
e -> log.error("Failed to send data to client", e)));
|
||||
eventMap.forEach(
|
||||
(kind, eventList) -> {
|
||||
switch (kind) {
|
||||
case POST_UPDATE:
|
||||
case METADATA_UPDATE:
|
||||
template.convertAndSend(
|
||||
"/topic/posts",
|
||||
eventList.stream()
|
||||
.map(e -> ((Long) e.getData()))
|
||||
.distinct()
|
||||
.map(dataService::findById)
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.collect(Collectors.toUnmodifiableSet()));
|
||||
|
||||
disposables.add(
|
||||
eventUpdateEventListener
|
||||
.getDataFlux()
|
||||
.map(EventUpdateEvent::getId)
|
||||
.buffer(Duration.of(500, ChronoUnit.MILLIS))
|
||||
.map(HashSet::new)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.subscribe(
|
||||
ids ->
|
||||
template.convertAndSend(
|
||||
"/topic/events",
|
||||
ids.stream()
|
||||
.map(dataService::findEventById)
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.collect(Collectors.toList())),
|
||||
e -> log.error("Failed to send data to client", e)));
|
||||
break;
|
||||
case POST_REMOVE:
|
||||
template.convertAndSend(
|
||||
"/topic/posts/deleted",
|
||||
eventList.stream()
|
||||
.map(e -> ((String) e.getData()))
|
||||
.collect(Collectors.toUnmodifiableSet()));
|
||||
break;
|
||||
case IMAGE_UPDATE:
|
||||
eventList.stream()
|
||||
.map(e -> ((Long) e.getData()))
|
||||
.distinct()
|
||||
.map(dataService::findImageById)
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.collect(Collectors.groupingBy(Image::getPostId))
|
||||
.forEach(
|
||||
(postId, images) ->
|
||||
template.convertAndSend("/topic/images/" + postId, images));
|
||||
break;
|
||||
|
||||
disposables.add(
|
||||
imageUpdateEventListener
|
||||
.getDataFlux()
|
||||
.map(ImageUpdateEvent::getId)
|
||||
.buffer(Duration.of(500, ChronoUnit.MILLIS))
|
||||
.map(HashSet::new)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.subscribe(
|
||||
id ->
|
||||
id.stream()
|
||||
.map(dataService::findImageById)
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.collect(Collectors.groupingBy(Image::getPostId))
|
||||
.forEach(
|
||||
(postId, images) ->
|
||||
template.convertAndSend("/topic/images/" + postId, images)),
|
||||
e -> log.error("Failed to send data to client", e)));
|
||||
case QUEUED_UPDATE:
|
||||
template.convertAndSend(
|
||||
"/topic/queued",
|
||||
eventList.stream()
|
||||
.map(e -> ((Long) e.getData()))
|
||||
.distinct()
|
||||
.map(dataService::findQueuedById)
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.collect(Collectors.toUnmodifiableSet()));
|
||||
break;
|
||||
case QUEUED_REMOVE:
|
||||
template.convertAndSend(
|
||||
"/topic/queued/deleted",
|
||||
eventList.stream()
|
||||
.map(e -> ((String) e.getData()))
|
||||
.collect(Collectors.toUnmodifiableSet()));
|
||||
break;
|
||||
case LOG_EVENT_UPDATE:
|
||||
template.convertAndSend(
|
||||
"/topic/events",
|
||||
eventList.stream()
|
||||
.map(e -> ((Long) e.getData()))
|
||||
.distinct()
|
||||
.map(dataService::findEventById)
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.collect(Collectors.toUnmodifiableSet()));
|
||||
break;
|
||||
case LOG_EVENT_REMOVE:
|
||||
template.convertAndSend(
|
||||
"/topic/events/deleted",
|
||||
eventList.stream()
|
||||
.map(e -> ((Long) e.getData()))
|
||||
.collect(Collectors.toUnmodifiableSet()));
|
||||
break;
|
||||
case VG_USER:
|
||||
eventList.stream()
|
||||
.map(e -> new DataController.LoggedUser((String) e.getData()))
|
||||
.distinct()
|
||||
.forEach(user -> template.convertAndSend("/topic/user", user));
|
||||
break;
|
||||
case GLOBAL_STATE:
|
||||
eventList.stream()
|
||||
.map(e -> ((GlobalState) e.getData()))
|
||||
.distinct()
|
||||
.forEach(
|
||||
globalState ->
|
||||
template.convertAndSend(
|
||||
"/topic/download-state", globalState));
|
||||
break;
|
||||
case BYTES_PER_SECOND:
|
||||
eventList.stream()
|
||||
.map(e -> new DownloadSpeed((Long) e.getData()))
|
||||
.distinct()
|
||||
.forEach(speed -> template.convertAndSend("/topic/speed", speed));
|
||||
|
||||
disposables.add(
|
||||
queuedUpdateEventListener
|
||||
.getDataFlux()
|
||||
.map(QueuedUpdateEvent::getId)
|
||||
.buffer(Duration.of(500, ChronoUnit.MILLIS))
|
||||
.map(HashSet::new)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.subscribe(
|
||||
ids ->
|
||||
template.convertAndSend(
|
||||
"/topic/queued",
|
||||
ids.stream()
|
||||
.map(dataService::findQueuedById)
|
||||
.filter(Optional::isPresent)
|
||||
.map(Optional::get)
|
||||
.collect(Collectors.toList())),
|
||||
e -> log.error("Failed to send data to client", e)));
|
||||
|
||||
disposables.add(
|
||||
queuedRemoveEventListener
|
||||
.getDataFlux()
|
||||
.map(QueuedRemoveEvent::getThreadId)
|
||||
.buffer(Duration.of(500, ChronoUnit.MILLIS))
|
||||
.map(HashSet::new)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.subscribe(
|
||||
threadIds -> template.convertAndSend("/topic/queued/deleted", threadIds),
|
||||
e -> log.error("Failed to send data to client", e)));
|
||||
|
||||
disposables.add(
|
||||
postRemoveEventListener
|
||||
.getDataFlux()
|
||||
.map(PostRemoveEvent::getPostId)
|
||||
.buffer(Duration.of(500, ChronoUnit.MILLIS))
|
||||
.map(HashSet::new)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.subscribe(
|
||||
postIds -> template.convertAndSend("/topic/posts/deleted", postIds),
|
||||
e -> log.error("Failed to send data to client", e)));
|
||||
|
||||
disposables.add(
|
||||
eventRemoveEventListener
|
||||
.getDataFlux()
|
||||
.map(EventRemoveEvent::getId)
|
||||
.buffer(Duration.of(500, ChronoUnit.MILLIS))
|
||||
.map(HashSet::new)
|
||||
.filter(e -> !e.isEmpty())
|
||||
.subscribe(
|
||||
postIds -> template.convertAndSend("/topic/events/deleted", postIds),
|
||||
e -> log.error("Failed to send data to client", e)));
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
disposables.forEach(Disposable::dispose);
|
||||
if (disposable != null) {
|
||||
disposable.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.handler.annotation.DestinationVariable;
|
||||
import org.springframework.messaging.simp.annotation.SubscribeMapping;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import tn.mnlr.vripper.jpa.domain.Event;
|
||||
import tn.mnlr.vripper.jpa.domain.Image;
|
||||
import tn.mnlr.vripper.jpa.domain.LogEvent;
|
||||
import tn.mnlr.vripper.jpa.domain.Post;
|
||||
import tn.mnlr.vripper.jpa.domain.Queued;
|
||||
import tn.mnlr.vripper.services.DataService;
|
||||
@@ -70,7 +70,7 @@ public class DataController {
|
||||
}
|
||||
|
||||
@SubscribeMapping("/events")
|
||||
public Collection<Event> events() {
|
||||
public Collection<LogEvent> events() {
|
||||
return dataService.findAllEvents();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user