From 199310de8610f33ed36341b3406fd11b2df22bae Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 26 Nov 2024 03:24:46 +0100 Subject: [PATCH] Added flag to stop listening to new emails --- .../net/codinux/invoicing/email/EmailsFetcher.kt | 10 +++++----- .../invoicing/email/ListenForNewMailsOptions.kt | 6 ++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt index 524b56d..85ee00d 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt @@ -54,22 +54,20 @@ open class EmailsFetcher( }) launch(coroutineDispatcher) { - keepConnectionOpen(account, folder) + keepConnectionOpen(account, folder, options) } } } catch (e: Throwable) { log.error(e) { "Listening to new emails of '${account.username}' failed" } options.onError?.invoke(FetchEmailsError(FetchEmailsErrorType.ListenForNewEmails, null, e)) } - - log.info { "Stopped listening to new emails of '${account.username}'" } } - protected open suspend fun keepConnectionOpen(account: EmailAccount, folder: Folder) { + protected open suspend fun keepConnectionOpen(account: EmailAccount, folder: Folder, options: ListenForNewMailsOptions) { log.info { "Listening to new emails of ${account.username}" } // Use IMAP IDLE to keep the connection alive - while (true) { + while (options.stopListening.get() == false) { if (!folder.isOpen) { log.info { "Reopening inbox of ${account.username} ..." } folder.open(Folder.READ_ONLY) @@ -79,6 +77,8 @@ open class EmailsFetcher( delay(250) } + + log.info { "Stopped listening to new emails of '${account.username}'" } } diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt index faf8c7b..4c40df8 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt @@ -1,12 +1,14 @@ package net.codinux.invoicing.email import net.codinux.invoicing.email.model.EmailWithInvoice +import java.util.concurrent.atomic.AtomicBoolean open class ListenForNewMailsOptions( + val stopListening: AtomicBoolean = AtomicBoolean(false), + downloadMessageBody: Boolean = false, emailFolderName: String = "INBOX", onError: ((FetchEmailsError) -> Unit)? = null, onEmailReceived: (EmailWithInvoice) -> Unit -) : FetchEmailsOptions(downloadMessageBody, emailFolderName, onError, onEmailReceived) { -} \ No newline at end of file +) : FetchEmailsOptions(downloadMessageBody, emailFolderName, onError, onEmailReceived) \ No newline at end of file