Skip to content

Commit

Permalink
make message processing timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenMassaro committed Nov 14, 2023
1 parent 114eb89 commit 9dfa7e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ BW_CLIENTSECRET=<get from Bitwarden vault>
```
Note that if the account you have specified in Bitwarden does not end in "gmail.com" or "aol.com", but is actually one of those accounts, you can add a custom field called "hostname" and specify the IMAP server address, like "imap.gmail.com". This is useful for custom domains that are hosted using GSuite.

### Optional configuration settings:

- `messageProcessingTimeoutSeconds`: number of seconds that should be spent processing each individual message before cancelling attempt to obtain message

## Steps to get Bitwarden IDs

You must have the [Bitwarden CLI](https://github.com/bitwarden/clients) installed.
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/email/service/ImapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import email.model.bitwarden.Item;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

Expand All @@ -24,6 +25,7 @@ public class ImapService {

private final MessageService messageService;
private final BitwardenService bitwardenService;
private final int messageProcessingTimeoutSeconds;
Cache<String, Store> storeCache = CacheBuilder.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.removalListener(new RemovalListener<String, Store>() {
Expand All @@ -41,9 +43,12 @@ public void onRemoval(RemovalNotification<String, Store> notification) {
})
.build();

public ImapService(MessageService messageService, BitwardenService bitwardenService) {
public ImapService(MessageService messageService,
BitwardenService bitwardenService,
@Value("${messageProcessingTimeoutSeconds:60}") int messageProcessingTimeoutSeconds) {
this.messageService = messageService;
this.bitwardenService = bitwardenService;
this.messageProcessingTimeoutSeconds = messageProcessingTimeoutSeconds;
}

public List<Message> getInboxMessages(String hostname, int port, String username, String decryptedPassword, List<Message> existingMessages, UUID accountBitwardenId) throws Exception {
Expand Down Expand Up @@ -74,7 +79,7 @@ public List<Message> getInboxMessages(String hostname, int port, String username
});

try {
messageProcessingFuture.get(60, TimeUnit.SECONDS);
messageProcessingFuture.get(messageProcessingTimeoutSeconds, TimeUnit.SECONDS);
} catch (TimeoutException e) {
log.warn("{} - Ran out of time while processing message {} of {}", username, finalI + 1, messages.length, e);
messageProcessingFuture.cancel(true);
Expand Down

0 comments on commit 9dfa7e0

Please sign in to comment.