Skip to content

Commit

Permalink
fix(SUP-42476):Bloomberg - Fallback not working player V7 eCDN (#238)
Browse files Browse the repository at this point in the history
issue:
ecdn not working in V7 - the cdn urls are not changing as expected

fix:
revert the code to last version it worked - change async await back to
promises and resolved

solved [SUP-42476](https://kaltura.atlassian.net/browse/SUP-42476)


[SUP-42476]:
https://kaltura.atlassian.net/browse/SUP-42476?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
inbalvasserman committed May 8, 2024
1 parent 27435b2 commit 262d6db
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 54 deletions.
95 changes: 51 additions & 44 deletions src/k-provider/ovp/regex-action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,66 +27,73 @@ class RegexActionHandler {
}

/**
* Ping the ECDN url to check if alive
* @function _pingECDNUrl
* Ping the ECDN url and replace the host urls if needed
* @function _pingECDNAndReplaceHostUrls
* @param {ProviderMediaConfigObject} mediaConfig - The media config
* @param {KalturaAccessControlModifyRequestHostRegexAction} regexAction - The regex action
* @param {string} cdnUrl - The CDN url
* @returns {Promise<boolean>} - Whether the media config sources urls should be modified or not
* @returns {Promise<ProviderMediaConfigObject>} - The media config with old or modified urls
* @static
* @private
*/
private static async _isECDNUrlAlive(regexAction: KalturaAccessControlModifyRequestHostRegexAction, cdnUrl: string): Promise<boolean> {
const urlPing = cdnUrl + '/api_v3/service/system/action/ping/format/1';
const req = new XMLHttpRequest();
req.open('GET', urlPing);
req.timeout = regexAction.checkAliveTimeoutMs;
req.onreadystatechange = (): boolean => {
if (req.readyState === 4) {
if (req.status === 200) {
return true;
private static _pingECDNAndReplaceHostUrls(
mediaConfig: ProviderMediaConfigObject,
regexAction: KalturaAccessControlModifyRequestHostRegexAction,
cdnUrl: string
): Promise<ProviderMediaConfigObject> {
return new Promise(resolve => {
const urlPing = cdnUrl + '/api_v3/service/system/action/ping/format/1';
const req = new XMLHttpRequest();
req.open('GET', urlPing);
req.timeout = regexAction.checkAliveTimeoutMs;
req.onreadystatechange = ():void => {
if (req.readyState === 4) {
if (req.status === 200) {
RegexActionHandler._replaceHostUrls(mediaConfig, regexAction);
}
resolve(mediaConfig);
}
return false;
}
return false;
};
req.ontimeout = (): boolean => {
RegexActionHandler._logger.warn(`Got timeout while pinging the ECDN url. the ping url: ${urlPing}`);
return false;
};
req.send();
return false;
};
req.ontimeout = ():void => {
RegexActionHandler._logger.warn(`Got timeout while pinging the ECDN url. the ping url: ${urlPing}`);
resolve(mediaConfig);
};
req.send();
});
}

/**
* Handles regex action
* @function handleRegexAction
* @param {ProviderMediaConfigObject} mediaConfig - The media config
* @param {Map<string, Function>} rawResponse - The raw response data from backend
* @returns {Promise<ProviderMediaConfigObject>} - The media config with old or modified urls
* @returns {ProviderMediaConfigObject} - The media config with old or modified urls
* @static
*/
public static async handleRegexAction(mediaConfig: ProviderMediaConfigObject, rawResponse: Map<string, ILoader>): Promise<ProviderMediaConfigObject> {
const cdnUrl = OVPConfiguration.get().cdnUrl;
const regexAction = RegexActionHandler._extractRegexActionFromData(rawResponse);
const regExp = RegexActionHandler._getRegExp(regexAction);
public static handleRegexAction(mediaConfig: ProviderMediaConfigObject, rawResponse: Map<string, ILoader>): Promise<ProviderMediaConfigObject> {
return new Promise(resolve => {
const cdnUrl = OVPConfiguration.get().cdnUrl;
const regexAction = RegexActionHandler._extractRegexActionFromData(rawResponse);
const regExp = RegexActionHandler._getRegExp(regexAction);

if (
cdnUrl &&
regexAction &&
regExp &&
cdnUrl.match(regExp) &&
// we need to make the replacement in all cases except for when the checkAliveTimeoutMs > 0 && the ping has failed
!(
regexAction.checkAliveTimeoutMs > 0 &&
!(await RegexActionHandler._isECDNUrlAlive(regexAction, cdnUrl.replace(regExp, regexAction.replacement)))
)
) {
RegexActionHandler._replaceHostUrls(mediaConfig, regexAction);
return mediaConfig;
}

RegexActionHandler._logger.debug('exiting handleRegexAction - not applying regex action.');
return mediaConfig;
if(
cdnUrl &&
regexAction &&
regExp &&
cdnUrl.match(regExp)
) {
if (regexAction.checkAliveTimeoutMs > 0) {
RegexActionHandler._logger.debug('executing ping request...');
RegexActionHandler._pingECDNAndReplaceHostUrls(mediaConfig, regexAction, cdnUrl.replace(regExp, regexAction.replacement)).then(resolve);
} else {
RegexActionHandler._replaceHostUrls(mediaConfig, regexAction);
resolve(mediaConfig);
}
} else {
RegexActionHandler._logger.debug('exiting handleRegexAction - not applying regex action.');
resolve(mediaConfig);
}
});
}

/**
Expand Down
24 changes: 14 additions & 10 deletions test/src/k-provider/ovp/regex-action-handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import RegexActionHandler from '../../../../src/k-provider/ovp/regex-action-hand
import OVPMediaEntryLoader from '../../../../src/k-provider/ovp/loaders/media-entry-loader';
import OVPConfiguration from '../../../../src/k-provider/ovp/config';

describe('handleRegexAction', function () {
let data = new Map();
describe('handleRegexAction', ()=> {
const data = new Map();
let mediaEntryLoader;
let mediaConfigForTest = {...mediaConfig};

Expand All @@ -20,17 +20,19 @@ describe('handleRegexAction', function () {
});

afterEach(() => {
RegexActionHandler._isECDNUrlAlive.restore();
RegexActionHandler._pingECDNAndReplaceHostUrls.restore();
});

it('should modify all URLs', done => {
sinon.stub(RegexActionHandler, '_isECDNUrlAlive').callsFake(function () {
sinon.stub(RegexActionHandler, '_pingECDNAndReplaceHostUrls').callsFake((mediaConfig, regexAction) => {
return new Promise(resolve => {
resolve(true);
RegexActionHandler._replaceHostUrls(mediaConfig, regexAction);
resolve(mediaConfig);
});
});

OVPConfiguration.set({replaceHostOnlyManifestUrls: false});
mediaConfigForTest = JSON.parse(JSON.stringify({...mediaConfig}));
mediaConfigForTest = JSON.parse(JSON.stringify({...mediaConfig}));
RegexActionHandler.handleRegexAction(mediaConfigForTest, data).then(
mediaConfigRes => {
try {
Expand All @@ -47,11 +49,13 @@ describe('handleRegexAction', function () {
});

it('should modify only the manifest URLs', done => {
sinon.stub(RegexActionHandler, '_isECDNUrlAlive').callsFake(function () {
sinon.stub(RegexActionHandler, '_pingECDNAndReplaceHostUrls').callsFake((mediaConfig, regexAction) => {
return new Promise(resolve => {
resolve(true);
RegexActionHandler._replaceHostUrls(mediaConfig, regexAction);
resolve(mediaConfig);
});
});

OVPConfiguration.set({replaceHostOnlyManifestUrls: true});
mediaConfigForTest = JSON.parse(JSON.stringify({...mediaConfig}));
RegexActionHandler.handleRegexAction(mediaConfigForTest, data).then(
Expand All @@ -70,9 +74,9 @@ describe('handleRegexAction', function () {
});

it('should not modify the sources URLs', done => {
sinon.stub(RegexActionHandler, '_isECDNUrlAlive').callsFake(function () {
sinon.stub(RegexActionHandler, '_pingECDNAndReplaceHostUrls').callsFake((mediaConfig) => {
return new Promise(resolve => {
resolve(false);
resolve(mediaConfig);
});
});
mediaConfigForTest = JSON.parse(JSON.stringify({...mediaConfig}));
Expand Down

0 comments on commit 262d6db

Please sign in to comment.