Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@supertenant/core / src / tracing / actions.js
Size: Mime:
const { dedupReportError } = require('../logger');

/**
 * Default poll wait interval is set to 10 millisecond if not defined by server.
 */
const DefaultWaitPollInterval = 10; // millisecond

/**
 * Ensure that if we're polling, we delay by 1ms at least each time, to prevent from bad configuration coming in from
 * server and causing a busy loop.
 */
const MinWaitPollInterval = 1;

/**
 * Default HTTP rejection status code.
 */
const DefaultHttpRejectActionStatusCode = 429;

/**
 * @typedef {{
 *  sqlState: string, sqlMessage: string, errno: number
 * }} SqlRejectActionResult
 */

/** @type {SqlRejectActionResult} */
const DefaultMysqlRejectActionResult = {
    sqlState: '70100',
    sqlMessage: 'Query execution was interrupted by SuperTenant',
    errno: 1317
};


/**
 * @param {import('@supertenant/superbrain').Action} actionRef 
 * @returns {number}
 */
function getWaitPollInterval(actionRef) {
    if (actionRef.Definition !== null && typeof actionRef.Definition === 'object') {
        /** @type {import('@supertenant/superbrain').WaitActionDefinition} */
        // @ts-expect-error
        let def = actionRef.Definition;
        const definitionPollInterval = def.PollInterval;
        if (typeof definitionPollInterval === 'number' && definitionPollInterval > 0) {
            let interval = definitionPollInterval / 1000000; // convert from nano to millisecond
            if (interval < MinWaitPollInterval) {
                return MinWaitPollInterval;
            }
            return interval;
        }
    }
    return DefaultWaitPollInterval;
}

/**
 * @param {import('@supertenant/superbrain').Action} actionRef 
 * @returns {number}
 */
function getHttpRejectActionStatusCode(actionRef) {
    if (actionRef.Definition !== null && typeof actionRef.Definition === 'object') {
        /** @type {import('@supertenant/superbrain').HttpRejectActionDefinition} */
        // @ts-expect-error
        let def = actionRef.Definition;
        const httpReject = def.HttpReject;
        if (httpReject !== null && typeof httpReject === 'object' && httpReject.http_rc !== null) {
            try {
                return typeof httpReject.http_rc === 'number' ? httpReject.http_rc : parseInt(httpReject.http_rc);
            } catch (e) {
                dedupReportError('actions:HTTP_REJECT_PARSE_CODE', 'failed to parse http status code', e);
            }
        }      
    }
    return DefaultHttpRejectActionStatusCode;
}

/**
 * @param {import('@supertenant/superbrain').Action} actionRef 
 * @returns {SqlRejectActionResult}
 */
function getMysqlRejectActionResult(actionRef) {
    if (actionRef.Definition !== null && typeof actionRef.Definition === 'object') {
        /** @type {import('@supertenant/superbrain').SqlRejectActionDefinition} */
        // @ts-expect-error
        let def = actionRef.Definition;
        const sqlReject = def.SqlReject;
        if (sqlReject !== null && typeof sqlReject === 'object') {
            let errno = 1317;
            if (sqlReject.sql_rc !== null && sqlReject.sql_rc !== undefined) {
                try {
                    errno = typeof sqlReject.sql_rc === 'number' ? sqlReject.sql_rc : parseInt(sqlReject.sql_rc);
                } catch (e) {
                    dedupReportError('actions:SQL_REJECT_PARSE_ERRNO', 'failed to parse sql errno', e);
                    return DefaultMysqlRejectActionResult;
                }
            }
            return {
              errno: errno,
              sqlMessage: sqlReject.error_message || 'Query execution was interrupted by SuperTenant',
              sqlState: sqlReject.sql_state || 'HY000' // generic client-side error
            };
        }
    }
    return DefaultMysqlRejectActionResult;
}

module.exports = {
    getWaitPollInterval,
    getHttpRejectActionStatusCode,
    getMysqlRejectActionResult
};