UNPKG

@getanthill/datastore

Version:

Event-Sourced Datastore

94 lines 4.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.main = main; const services_1 = require("../../services"); const utils_1 = require("../../utils"); function wait(delayInMilliseconds) { return new Promise((resolve) => setTimeout(resolve, delayInMilliseconds)); } function blockEventLoop(delayInMilliseconds) { const tic = Date.now(); let blockedSince = 0; while (blockedSince < delayInMilliseconds) { blockedSince = Date.now() - tic; } } async function main(url, services = (0, services_1.build)()) { var _a, _b, _c, _d, _e, _f, _g, _h; const datastore = (_a = url.searchParams.get('datastore')) !== null && _a !== void 0 ? _a : 'models'; const model = (_b = url.searchParams.get('model')) !== null && _b !== void 0 ? _b : 'all'; const source = ((_c = url.searchParams.get('source')) !== null && _c !== void 0 ? _c : 'events'); const query = JSON.parse((_d = url.searchParams.get('query')) !== null && _d !== void 0 ? _d : '{}'); const timeout = Number.parseInt((_e = url.searchParams.get('timeout')) !== null && _e !== void 0 ? _e : '0', 10); const block = Number.parseInt((_f = url.searchParams.get('block')) !== null && _f !== void 0 ? _f : '0', 10); const exception = Number.parseInt((_g = url.searchParams.get('exception')) !== null && _g !== void 0 ? _g : '0', 10); const progress = Number.parseInt((_h = url.searchParams.get('progress')) !== null && _h !== void 0 ? _h : '1000', 10); const withHeartbeat = url.searchParams.get('with_heartbeat') === 'true'; const withFetch = url.searchParams.get('with_fetch') === 'true'; const stats = { processed: 0, processing: 0, exception: 0, waiting: 0, heartbeat: 0, fetching: 0, blocking: 0, }; return { triggers: [ { datastore, model, source, raw: false, query, }, ], start: async () => { services.telemetry.logger.info('[utils#log] Starting'); return services; }, stop: async () => { services.telemetry.logger.info('[utils#log] Ending'); }, handler: async (event) => { var _a, _b; stats.processing += 1; services.telemetry.logger.debug('[utils#log] Event', event); if (timeout > 0) { stats.waiting += 1; services.telemetry.logger.debug('[utils#log] Waiting', stats); await wait(timeout); stats.waiting -= 1; } if (withHeartbeat === true) { stats.heartbeat += 1; services.telemetry.logger.debug('[utils#log] Heartbeat', stats); await ((_a = services.datastores.get(datastore)) === null || _a === void 0 ? void 0 : _a.heartbeat()); stats.heartbeat -= 1; } if (withFetch === true) { stats.fetching += 1; services.telemetry.logger.debug('[utils#log] Fetch', stats); await ((_b = services.datastores.get(datastore)) === null || _b === void 0 ? void 0 : _b.find(model, {}, 0, 1000)); stats.fetching -= 1; } if (block > 0) { stats.blocking += 1; services.telemetry.logger.debug('[utils#log] Block', stats); blockEventLoop(block); stats.blocking -= 1; } if (exception > 0 && (0, utils_1.random)() <= exception) { stats.exception += 1; services.telemetry.logger.debug('[utils#log] Exception', stats); throw new Error('This is an error'); } stats.processing -= 1; stats.processed += 1; stats.processed % progress === 0 && services.telemetry.logger.info('[utils#log] Handled', stats); }, }; } //# sourceMappingURL=utils.js.map