All files / src/monitor/tasks WalletMonitorTask.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 3/3
100% Lines 6/6

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                                    57x         4x         4x 4x   4x               4x                  
import { MonitorStorage, Monitor } from '../Monitor';
 
/**
 * A monitor task performs some periodic or state triggered maintenance function
 * on the data managed by a wallet (Bitcoin UTXO manager, aka wallet)
 *
 * The monitor maintains a collection of tasks.
 *
 * It runs each task's non-asynchronous trigger to determine if the runTask method needs to run.
 *
 * Tasks that need to be run are run consecutively by awaiting their async runTask override method.
 *
 * The monitor then waits a fixed interval before repeating...
 *
 * Tasks may use the monitor_events table to persist their execution history.
 * This is done by accessing the wathman.storage object.
 */
 
export abstract class WalletMonitorTask {
 
    /**
     * Set by monitor each time runTask completes
     */
    lastRunMsecsSinceEpoch = 0;
 
    storage: MonitorStorage;
 
    constructor(
        public monitor: Monitor,
        public name: string
    ) {
        this.storage = monitor.storage;
    }
 
    /**
     * Override to handle async task setup configuration.
     *
     * Called before first call to `trigger`
     */
    async asyncSetup(): Promise<void> { }
 
    /**
     * Return true if `runTask` needs to be called now.
     */
    abstract trigger(nowMsecsSinceEpoch: number): { run: boolean; };
 
    abstract runTask(): Promise<string>;
}