All files / src/monitor/tasks TaskPurge.ts

25% Statements 4/16
0% Branches 0/5
0% Functions 0/5
28.57% Lines 4/14

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68    57x                                                           57x 57x         57x                                                          
import { sdk } from '../../index.client';
import { Monitor } from '../Monitor';
import { WalletMonitorTask } from './WalletMonitorTask';
 
/**
 * The database stores a variety of data that may be considered transient.
 * 
 * At one extreme, the data that must be preserved:
 *   - unspent outputs (UTXOs)
 *   - in-use metadata (labels, baskets, tags...)
 * 
 * At the other extreme, everything can be preserved to fully log all transaction creation and processing actions.
 * 
 * The following purge actions are available to support sustained operation:
 *   - Failed transactions, delete all associated data including:
 *       + Delete tag and label mapping records
 *       + Delete output records
 *       + Delete transaction records
 *       + Delete mapi_responses records
 *       + Delete proven_tx_reqs records
 *       + Delete commissions records
 *       + Update output records marked spentBy failed transactions
 *   - Completed transactions, delete transient data including:
 *       + transactions table set truncatedExternalInputs = null
 *       + transactions table set beef = null
 *       + transactions table set rawTx = null
 *       + Delete mapi_responses records
 *       + proven_tx_reqs table delete records
 */
export interface TaskPurgeParams extends sdk.PurgeParams {
}
 
export class TaskPurge extends WalletMonitorTask {
    static taskName = 'Purge';
 
    /**
     * Set to true to trigger running this task
     */
    static checkNow = false;
 
    constructor(monitor: Monitor, public params: TaskPurgeParams, public triggerMsecs = 0) {
        super(monitor, TaskPurge.taskName);
    }
 
    trigger(nowMsecsSinceEpoch: number): { run: boolean; } {
        return {
            run: (
                TaskPurge.checkNow ||
                this.triggerMsecs > 0 && nowMsecsSinceEpoch - this.lastRunMsecsSinceEpoch > this.triggerMsecs
            )
        };
    }
 
    async runTask(): Promise<string> {
        let log = ''
        TaskPurge.checkNow = false;
 
        const r = await this.storage.runAsStorageProvider(async (sp) => {
            return await sp.purgeData(this.params)
        })
 
        Iif (r.count > 0)
            log = `${r.count} records updated or deleted.\n${r.log}`
 
        return log
    }
}