Skip to content

Commit

Permalink
(android) Fix isolated business initialization
Browse files Browse the repository at this point in the history
The business variable should be set asap to allow proper
cancelling if the worker is cancelled early.
  • Loading branch information
dpad85 committed Oct 3, 2024
1 parent f89c105 commit 20bfec2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import fr.acinq.phoenix.android.security.SeedManager
import fr.acinq.phoenix.legacy.utils.LegacyAppStatus
import fr.acinq.phoenix.legacy.utils.LegacyPrefsDatastore
import fr.acinq.phoenix.managers.AppConnectionsDaemon
import fr.acinq.phoenix.utils.PlatformContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
Expand All @@ -53,14 +54,11 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.slf4j.LoggerFactory
import java.util.concurrent.TimeUnit
import kotlin.time.Duration.Companion.minutes
import kotlin.time.toJavaDuration

/**
* This worker is scheduled to run roughly every day. It simply connects to the LSP, wait for 1 minute,
* then shuts down. The purpose is to settle pending payments that may have been missed by the
* [InflightPaymentsWatcher], to complete closings properly, etc...
*
*/
class DailyConnect(context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {

Expand Down Expand Up @@ -113,6 +111,7 @@ class DailyConnect(context: Context, workerParams: WorkerParameters) : Coroutine
var jobWatchingChannels: Job? = null

val jobMain = launch {
business = PhoenixBusiness(PlatformContext(applicationContext))
service.filterNotNull().flatMapLatest { it.state.asFlow() }.collect { state ->
when (state) {
is NodeServiceState.Init, is NodeServiceState.Running, is NodeServiceState.Error, NodeServiceState.Disconnected -> {
Expand All @@ -126,7 +125,7 @@ class DailyConnect(context: Context, workerParams: WorkerParameters) : Coroutine
log.info("node service in state=${state.name}, starting an isolated business")

jobWatchingChannels = launch {
business = WorkerHelper.startIsolatedBusiness(application, encryptedSeed, userPrefs)
WorkerHelper.startIsolatedBusiness(application, business!!, encryptedSeed, userPrefs)

business?.connectionsManager?.connections?.first { it.global is Connection.ESTABLISHED }
log.debug("connections established")
Expand Down Expand Up @@ -184,8 +183,8 @@ class DailyConnect(context: Context, workerParams: WorkerParameters) : Coroutine
}

fun scheduleASAP(context: Context) {
log.info("scheduling $name")
val work = OneTimeWorkRequest.Builder(DailyConnect::class.java).setInitialDelay(1.minutes.toJavaDuration()).addTag(TAG).build()
log.info("scheduling $name once")
val work = OneTimeWorkRequest.Builder(DailyConnect::class.java).addTag(TAG).build()
WorkManager.getInstance(context).enqueueUniqueWork(TAG, ExistingWorkPolicy.REPLACE, work)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class InflightPaymentsWatcher(context: Context, workerParams: WorkerParameters)

// Start the monitoring process. If the main app starts, we interrupt this job to prevent concurrent access.
withContext(Dispatchers.Default) {
business = PhoenixBusiness(PlatformContext(applicationContext))
val stopJobs = MutableStateFlow(false)
var jobChannelsWatcher: Job? = null

Expand All @@ -152,7 +153,7 @@ class InflightPaymentsWatcher(context: Context, workerParams: WorkerParameters)
log.info("node service in state=${state.name}, starting an isolated business")

jobChannelsWatcher = launch {
business = WorkerHelper.startIsolatedBusiness(application, encryptedSeed, userPrefs)
WorkerHelper.startIsolatedBusiness(application, business!!, encryptedSeed, userPrefs)

business?.connectionsManager?.connections?.first { it.global is Connection.ESTABLISHED }
log.debug("connections established, watching channels for in-flight payments...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ import fr.acinq.phoenix.data.StartupParams
import fr.acinq.phoenix.legacy.utils.LegacyPrefsDatastore
import fr.acinq.phoenix.managers.AppConfigurationManager
import fr.acinq.phoenix.utils.MnemonicLanguage
import fr.acinq.phoenix.utils.PlatformContext
import kotlinx.coroutines.flow.first

object WorkerHelper {
suspend fun startIsolatedBusiness(context: Context, encryptedSeed: EncryptedSeed.V2.NoAuth, userPrefs: UserPrefsRepository): PhoenixBusiness {
suspend fun startIsolatedBusiness(context: Context, business: PhoenixBusiness, encryptedSeed: EncryptedSeed.V2.NoAuth, userPrefs: UserPrefsRepository) {
val mnemonics = encryptedSeed.decrypt()

// retrieve preferences before starting business
val business = PhoenixBusiness(PlatformContext(context))
val electrumServer = userPrefs.getElectrumServer.first()
val isTorEnabled = userPrefs.getIsTorEnabled.first()
val liquidityPolicy = userPrefs.getLiquidityPolicy.first()
Expand All @@ -60,6 +58,5 @@ object WorkerHelper {

// start the swap-in wallet watcher
business.peerManager.getPeer().startWatchSwapInWallet()
return business
}
}

0 comments on commit 20bfec2

Please sign in to comment.