#180: last inbox sync infos

This commit is contained in:
Benjamin Gamard 2018-02-27 17:11:04 +01:00
parent 1054931e63
commit 062dee987f
4 changed files with 64 additions and 6 deletions

View File

@ -1,14 +1,13 @@
package com.sismics.docs.core.listener.async; package com.sismics.docs.core.listener.async;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import com.sismics.docs.core.dao.jpa.ContributorDao; import com.sismics.docs.core.dao.jpa.ContributorDao;
import com.sismics.docs.core.dao.lucene.LuceneDao; import com.sismics.docs.core.dao.lucene.LuceneDao;
import com.sismics.docs.core.event.DocumentCreatedAsyncEvent; import com.sismics.docs.core.event.DocumentCreatedAsyncEvent;
import com.sismics.docs.core.model.jpa.Contributor; import com.sismics.docs.core.model.jpa.Contributor;
import com.sismics.docs.core.util.TransactionUtil; import com.sismics.docs.core.util.TransactionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Listener on document created. * Listener on document created.
@ -25,10 +24,9 @@ public class DocumentCreatedAsyncListener {
* Document created. * Document created.
* *
* @param event Document created event * @param event Document created event
* @throws Exception
*/ */
@Subscribe @Subscribe
public void on(final DocumentCreatedAsyncEvent event) throws Exception { public void on(final DocumentCreatedAsyncEvent event) {
if (log.isInfoEnabled()) { if (log.isInfoEnabled()) {
log.info("Document created event: " + event.toString()); log.info("Document created event: " + event.toString());
} }

View File

@ -34,6 +34,13 @@ public class InboxService extends AbstractScheduledService {
*/ */
private static final Logger log = LoggerFactory.getLogger(InboxService.class); private static final Logger log = LoggerFactory.getLogger(InboxService.class);
/**
* Last synchronization data.
*/
private Date lastSyncDate;
private int lastSyncMessageCount = 0;
private String lastSyncError;
public InboxService() { public InboxService() {
} }
@ -60,18 +67,23 @@ public class InboxService extends AbstractScheduledService {
} }
Folder inbox = null; Folder inbox = null;
lastSyncError = null;
lastSyncDate = new Date();
lastSyncMessageCount = 0;
try { try {
inbox = openInbox(); inbox = openInbox();
int count = inbox.getMessageCount(); int count = inbox.getMessageCount();
Message[] messages = inbox.getMessages(1, count); Message[] messages = inbox.getMessages(1, count);
for (Message message : messages) { for (Message message : messages) {
if (!message.getFlags().contains(Flags.Flag.SEEN)) { if (!message.getFlags().contains(Flags.Flag.SEEN)) {
importMessage(message); importMessage(message);
lastSyncMessageCount++;
} }
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Error synching the inbox", e); log.error("Error synching the inbox", e);
lastSyncError = e.getMessage();
} finally { } finally {
try { try {
if (inbox != null) { if (inbox != null) {
@ -207,4 +219,16 @@ public class InboxService extends AbstractScheduledService {
FileUtil.createFile(fileContent.getName(), fileContent.getFile(), fileContent.getSize(), "eng", "admin", document.getId()); FileUtil.createFile(fileContent.getName(), fileContent.getFile(), fileContent.getSize(), "eng", "admin", document.getId());
} }
} }
public Date getLastSyncDate() {
return lastSyncDate;
}
public int getLastSyncMessageCount() {
return lastSyncMessageCount;
}
public String getLastSyncError() {
return lastSyncError;
}
} }

View File

@ -12,6 +12,7 @@ import com.sismics.docs.core.model.context.AppContext;
import com.sismics.docs.core.model.jpa.Config; import com.sismics.docs.core.model.jpa.Config;
import com.sismics.docs.core.model.jpa.File; import com.sismics.docs.core.model.jpa.File;
import com.sismics.docs.core.model.jpa.User; import com.sismics.docs.core.model.jpa.User;
import com.sismics.docs.core.service.InboxService;
import com.sismics.docs.core.util.ConfigUtil; import com.sismics.docs.core.util.ConfigUtil;
import com.sismics.docs.core.util.DirectoryUtil; import com.sismics.docs.core.util.DirectoryUtil;
import com.sismics.docs.core.util.jpa.PaginatedList; import com.sismics.docs.core.util.jpa.PaginatedList;
@ -21,6 +22,7 @@ import com.sismics.rest.exception.ClientException;
import com.sismics.rest.exception.ForbiddenClientException; import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.exception.ServerException; import com.sismics.rest.exception.ServerException;
import com.sismics.rest.util.ValidationUtil; import com.sismics.rest.util.ValidationUtil;
import com.sismics.util.JsonUtil;
import com.sismics.util.context.ThreadLocalContext; import com.sismics.util.context.ThreadLocalContext;
import com.sismics.util.log4j.LogCriteria; import com.sismics.util.log4j.LogCriteria;
import com.sismics.util.log4j.LogEntry; import com.sismics.util.log4j.LogEntry;
@ -358,6 +360,18 @@ public class AppResource extends BaseResource {
response.add("tag", tagConfig.getValue()); response.add("tag", tagConfig.getValue());
} }
// Informations about the last synchronization
InboxService inboxService = AppContext.getInstance().getInboxService();
JsonObjectBuilder lastSync = Json.createObjectBuilder();
if (inboxService.getLastSyncDate() == null) {
lastSync.addNull("date");
} else {
lastSync.add("date", inboxService.getLastSyncDate().getTime());
}
lastSync.add("error", JsonUtil.nullable(inboxService.getLastSyncError()));
lastSync.add("count", inboxService.getLastSyncMessageCount());
response.add("last_sync", lastSync);
return Response.ok().entity(response.build()).build(); return Response.ok().entity(response.build()).build();
} }

View File

@ -249,6 +249,10 @@ public class TestAppResource extends BaseJerseyTest {
Assert.assertTrue(json.isNull("username")); Assert.assertTrue(json.isNull("username"));
Assert.assertTrue(json.isNull("password")); Assert.assertTrue(json.isNull("password"));
Assert.assertTrue(json.isNull("tag")); Assert.assertTrue(json.isNull("tag"));
JsonObject lastSync = json.getJsonObject("last_sync");
Assert.assertTrue(lastSync.isNull("date"));
Assert.assertTrue(lastSync.isNull("error"));
Assert.assertEquals(0, lastSync.getJsonNumber("count").intValue());
// Change inbox configuration // Change inbox configuration
target().path("/app/config_inbox").request() target().path("/app/config_inbox").request()
@ -299,6 +303,15 @@ public class TestAppResource extends BaseJerseyTest {
.get(JsonObject.class); .get(JsonObject.class);
Assert.assertEquals(1, json.getJsonArray("documents").size()); Assert.assertEquals(1, json.getJsonArray("documents").size());
// Get inbox configuration
json = target().path("/app/config_inbox").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class);
lastSync = json.getJsonObject("last_sync");
Assert.assertFalse(lastSync.isNull("date"));
Assert.assertTrue(lastSync.isNull("error"));
Assert.assertEquals(1, lastSync.getJsonNumber("count").intValue());
// Trigger an inbox sync // Trigger an inbox sync
AppContext.getInstance().getInboxService().syncInbox(); AppContext.getInstance().getInboxService().syncInbox();
@ -310,6 +323,15 @@ public class TestAppResource extends BaseJerseyTest {
.get(JsonObject.class); .get(JsonObject.class);
Assert.assertEquals(1, json.getJsonArray("documents").size()); Assert.assertEquals(1, json.getJsonArray("documents").size());
// Get inbox configuration
json = target().path("/app/config_inbox").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class);
lastSync = json.getJsonObject("last_sync");
Assert.assertFalse(lastSync.isNull("date"));
Assert.assertTrue(lastSync.isNull("error"));
Assert.assertEquals(0, lastSync.getJsonNumber("count").intValue());
greenMail.stop(); greenMail.stop();
} }
} }