From 7e5827eb4fbde75e109d68393145151901c7a8ea Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 27 Feb 2024 15:36:33 +0100 Subject: -move state into shepherd --- packages/taler-wallet-core/src/exchanges.ts | 2 +- packages/taler-wallet-core/src/pay-merchant.ts | 3 +-- packages/taler-wallet-core/src/shepherd.ts | 23 +++++++++++++++++++++-- packages/taler-wallet-core/src/testing.ts | 12 ++++++------ packages/taler-wallet-core/src/wallet.ts | 17 ----------------- 5 files changed, 29 insertions(+), 28 deletions(-) (limited to 'packages/taler-wallet-core') diff --git a/packages/taler-wallet-core/src/exchanges.ts b/packages/taler-wallet-core/src/exchanges.ts index 6095fadd5..0b7c65491 100644 --- a/packages/taler-wallet-core/src/exchanges.ts +++ b/packages/taler-wallet-core/src/exchanges.ts @@ -1082,7 +1082,7 @@ export async function fetchFreshExchange( ): Promise { const canonUrl = canonicalizeBaseUrl(baseUrl); - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); await startUpdateExchangeEntry(ws, canonUrl, { forceUpdate: options.forceUpdate, diff --git a/packages/taler-wallet-core/src/pay-merchant.ts b/packages/taler-wallet-core/src/pay-merchant.ts index 332660ad5..73dc59ba2 100644 --- a/packages/taler-wallet-core/src/pay-merchant.ts +++ b/packages/taler-wallet-core/src/pay-merchant.ts @@ -1732,8 +1732,7 @@ async function waitPaymentResult( ): Promise { const ctx = new PayMerchantTransactionContext(ws, proposalId); - ws.ensureTaskLoopRunning(); - + ws.taskScheduler.ensureRunning(); ws.taskScheduler.startShepherdTask(ctx.taskId); // FIXME: Clean up using the new JS "using" / Symbol.dispose syntax. diff --git a/packages/taler-wallet-core/src/shepherd.ts b/packages/taler-wallet-core/src/shepherd.ts index bf2f6b50f..ec0f6a76e 100644 --- a/packages/taler-wallet-core/src/shepherd.ts +++ b/packages/taler-wallet-core/src/shepherd.ts @@ -141,6 +141,8 @@ export class TaskScheduler { private throttler = new TaskThrottler(); + isRunning: boolean = false; + constructor(private ws: InternalWalletState) {} async loadTasksFromDb(): Promise { @@ -153,9 +155,26 @@ export class TaskScheduler { } } + ensureRunning(): void { + if (this.isRunning) { + return; + } + this.run() + .catch((e) => { + logger.error("error running task loop"); + logger.error(`err: ${e}`); + }) + .then(() => { + logger.info("done running task loop"); + }); + } + async run(opts: RetryLoopOpts = {}): Promise { + if (this.isRunning) { + throw Error("task loop already running"); + } logger.info("Running task loop."); - this.ws.isTaskLoopRunning = true; + this.isRunning = true; await this.loadTasksFromDb(); logger.info("loaded!"); logger.info(`sheps: ${this.sheps.size}`); @@ -182,7 +201,7 @@ export class TaskScheduler { } await this.iterCond.wait(); } - this.ws.isTaskLoopRunning = false; + this.isRunning = false; logger.info("Done with task loop."); } diff --git a/packages/taler-wallet-core/src/testing.ts b/packages/taler-wallet-core/src/testing.ts index bcb6abca0..22af816e5 100644 --- a/packages/taler-wallet-core/src/testing.ts +++ b/packages/taler-wallet-core/src/testing.ts @@ -402,7 +402,7 @@ export async function waitUntilAllTransactionsFinal( ws: InternalWalletState, ): Promise { logger.info("waiting until all transactions are in a final state"); - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); let p: OpenedPromise | undefined = undefined; const cancelNotifs = ws.addNotificationListener((notif) => { if (!p) { @@ -462,7 +462,7 @@ export async function waitUntilGivenTransactionsFinal( if (transactionIds.length === 0) { return; } - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); const txIdSet = new Set(transactionIds); let p: OpenedPromise | undefined = undefined; const cancelNotifs = ws.addNotificationListener((notif) => { @@ -522,7 +522,7 @@ export async function waitUntilRefreshesDone( ws: InternalWalletState, ): Promise { logger.info("waiting until all refresh transactions are in a final state"); - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); let p: OpenedPromise | undefined = undefined; const cancelNotifs = ws.addNotificationListener((notif) => { if (!p) { @@ -576,7 +576,7 @@ async function waitUntilTransactionPendingReady( transactionId: string, ): Promise { logger.info(`starting waiting for ${transactionId} to be in pending(ready)`); - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); let p: OpenedPromise | undefined = undefined; const cancelNotifs = ws.addNotificationListener((notif) => { if (!p) { @@ -617,7 +617,7 @@ export async function waitTransactionState( txState, )})`, ); - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); let p: OpenedPromise | undefined = undefined; const cancelNotifs = ws.addNotificationListener((notif) => { if (!p) { @@ -669,7 +669,7 @@ export async function runIntegrationTest2( ws: InternalWalletState, args: IntegrationTestV2Args, ): Promise { - ws.ensureTaskLoopRunning(); + ws.taskScheduler.ensureRunning(); logger.info("running test with arguments", args); const exchangeInfo = await fetchFreshExchange(ws, args.exchangeBaseUrl); diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts index 79f7a1be5..bab054f4b 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -1486,8 +1486,6 @@ export class InternalWalletState { */ private resourceLocks: Set = new Set(); - isTaskLoopRunning: boolean = false; - taskScheduler: TaskScheduler = new TaskScheduler(this); config: Readonly; @@ -1604,21 +1602,6 @@ export class InternalWalletState { } } } - - ensureTaskLoopRunning(): void { - if (this.isTaskLoopRunning) { - return; - } - this.taskScheduler - .run() - .catch((e) => { - logger.error("error running task loop"); - logger.error(`err: ${e}`); - }) - .then(() => { - logger.info("done running task loop"); - }); - } } /** -- cgit v1.2.3