All files / src/monitor/tasks TaskFailAbandoned.ts

12% Statements 3/25
0% Branches 0/4
0% Functions 0/6
13.63% Lines 3/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47  57x                       57x 57x                                                                
import { Monitor } from '../Monitor';
import { WalletMonitorTask } from './WalletMonitorTask';
 
/**
 * Handles transactions which do not have terminal status and have not been
 * updated for an extended time period.
 *
 * Calls `updateTransactionStatus` to set `status` to `failed`.
 * This returns inputs to spendable status and verifies that any
 * outputs are not spendable.
 */
 
 
export class TaskFailAbandoned extends WalletMonitorTask {
    static taskName = 'FailAbandoned';
 
    constructor(monitor: Monitor, public triggerMsecs = 1000 * 60 * 5) {
        super(monitor, TaskFailAbandoned.taskName);
    }
 
    trigger(nowMsecsSinceEpoch: number): { run: boolean; } {
        return { run: nowMsecsSinceEpoch > this.lastRunMsecsSinceEpoch + this.triggerMsecs };
    }
 
    async runTask(): Promise<string> {
        let log = ''
        const limit = 100;
        let offset = 0;
        for (; ;) {
            const now = new Date();
            const abandoned = new Date(now.getTime() - this.monitor.options.abandonedMsecs);
            const done = await this.storage.runAsStorageProvider(async (sp) => {
                const txsAll = await sp.findTransactions({ partial: {}, status: ['unprocessed', 'unsigned'], paged: { limit, offset } });
                const txs = txsAll.filter(t => t.updated_at && t.updated_at < abandoned);
                for (const tx of txs) {
                    await sp.updateTransactionStatus('failed', tx.transactionId);
                    log += `updated tx ${tx.transactionId} status to 'failed'\n`
                }
                return txs.length < limit
            })
            Iif (done) break;
            offset += limit;
        }
        return log
    }
}