-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Importer command #dockerpush #latest
- Loading branch information
Showing
37 changed files
with
499 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ out/ | |
|
||
settings*.json | ||
settings*.yml | ||
import-config*.yml | ||
retries.json | ||
docker-compose.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 10 additions & 47 deletions
57
src/main/kotlin/io/github/smaugfm/monobudget/Application.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,34 @@ | ||
package io.github.smaugfm.monobudget | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import io.github.smaugfm.monobudget.common.exception.BudgetBackendException | ||
import io.github.smaugfm.monobudget.common.BaseApplication | ||
import io.github.smaugfm.monobudget.common.notify.TelegramApi | ||
import io.github.smaugfm.monobudget.common.notify.TelegramCallbackHandler | ||
import io.github.smaugfm.monobudget.common.startup.ApplicationStartupVerifier | ||
import io.github.smaugfm.monobudget.common.statement.StatementSource | ||
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementEvents | ||
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementItemProcessor | ||
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingScopeComponent | ||
import io.github.smaugfm.monobudget.common.telegram.TelegramApi | ||
import io.github.smaugfm.monobudget.common.telegram.TelegramCallbackHandler | ||
import io.github.smaugfm.monobudget.common.util.injectAll | ||
import kotlinx.coroutines.flow.asFlow | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.flow.flatMapMerge | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onEach | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import kotlin.system.exitProcess | ||
|
||
private val log = KotlinLogging.logger {} | ||
|
||
class Application<TTransaction, TNewTransaction> : | ||
KoinComponent { | ||
BaseApplication<TTransaction, TNewTransaction>() { | ||
override val statementSources: List<StatementSource> by injectAll<StatementSource>() | ||
private val telegramApi by inject<TelegramApi>() | ||
private val statementSources by injectAll<StatementSource>() | ||
private val startupVerifiers by injectAll<ApplicationStartupVerifier>() | ||
private val telegramCallbackHandler by inject<TelegramCallbackHandler<TTransaction>>() | ||
private val statementEvents by inject<StatementEvents>() | ||
|
||
suspend fun run() { | ||
runStartupChecks() | ||
|
||
statementSources.forEach { it.prepare() } | ||
|
||
telegramApi.start(telegramCallbackHandler::handle) | ||
log.info { "Started application" } | ||
|
||
statementSources.asFlow() | ||
.flatMapMerge { it.statements() } | ||
.filter(statementEvents::onNewStatement) | ||
.map(::StatementProcessingScopeComponent) | ||
.onEach { | ||
with(it) { | ||
try { | ||
scope.get<StatementItemProcessor<TTransaction, TNewTransaction>>() | ||
.process() | ||
statementEvents.onStatementEnd(ctx) | ||
} catch (e: BudgetBackendException) { | ||
statementEvents.onStatementRetry(ctx, e) | ||
} catch (e: Throwable) { | ||
statementEvents.onStatementError(ctx, e) | ||
} finally { | ||
scope.close() | ||
} | ||
} | ||
} | ||
.collect() | ||
} | ||
|
||
private suspend fun runStartupChecks() { | ||
override suspend fun beforeStart() { | ||
try { | ||
startupVerifiers.forEach { it.verify() } | ||
} catch (e: Throwable) { | ||
log.error(e) { "Failed to start application. Exiting..." } | ||
exitProcess(1) | ||
} | ||
} | ||
|
||
override suspend fun afterSourcesPrepare() { | ||
telegramApi.start(telegramCallbackHandler::handle) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/io/github/smaugfm/monobudget/common/BaseApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.github.smaugfm.monobudget.common | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import io.github.smaugfm.monobudget.common.exception.BudgetBackendException | ||
import io.github.smaugfm.monobudget.common.statement.StatementSource | ||
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementEvents | ||
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementItemProcessor | ||
import io.github.smaugfm.monobudget.common.statement.lifecycle.StatementProcessingScopeComponent | ||
import kotlinx.coroutines.flow.asFlow | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.flow.flatMapMerge | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onEach | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
|
||
private val log = KotlinLogging.logger {} | ||
|
||
abstract class BaseApplication<TTransaction, TNewTransaction> : KoinComponent { | ||
protected abstract val statementSources: List<StatementSource> | ||
private val statementEvents by inject<StatementEvents>() | ||
|
||
open suspend fun run() { | ||
beforeStart() | ||
|
||
statementSources.forEach { it.prepare() } | ||
|
||
afterSourcesPrepare() | ||
|
||
log.info { "Started application" } | ||
|
||
statementSources.asFlow() | ||
.flatMapMerge { it.statements() } | ||
.filter(statementEvents::onNewStatement) | ||
.map(::StatementProcessingScopeComponent) | ||
.onEach { | ||
with(it) { | ||
try { | ||
scope.get<StatementItemProcessor<TTransaction, TNewTransaction>>() | ||
.process() | ||
statementEvents.onStatementEnd(ctx) | ||
} catch (e: BudgetBackendException) { | ||
statementEvents.onStatementRetry(ctx, e) | ||
} catch (e: Throwable) { | ||
statementEvents.onStatementError(ctx, e) | ||
} finally { | ||
scope.close() | ||
} | ||
} | ||
} | ||
.collect() | ||
} | ||
|
||
protected open suspend fun beforeStart() { | ||
} | ||
|
||
protected open suspend fun afterSourcesPrepare() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...main/kotlin/io/github/smaugfm/monobudget/common/notify/StatementItemNotificationSender.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.github.smaugfm.monobudget.common.notify | ||
|
||
import io.github.smaugfm.monobudget.common.model.financial.BankAccountId | ||
import io.github.smaugfm.monobudget.common.model.telegram.MessageWithReplyKeyboard | ||
|
||
interface StatementItemNotificationSender { | ||
suspend fun notify( | ||
accountId: BankAccountId, | ||
newMessage: MessageWithReplyKeyboard, | ||
) | ||
} |
2 changes: 1 addition & 1 deletion
2
...monobudget/common/telegram/TelegramApi.kt → ...m/monobudget/common/notify/TelegramApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ommon/telegram/TelegramCallbackHandler.kt → .../common/notify/TelegramCallbackHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...gram/TelegramErrorHandlerEventListener.kt → ...tify/TelegramErrorHandlerEventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.