UNPKG

gpii-universal

Version:

Cross platform, core components of the GPII personalization infrastructure.

71 lines (59 loc) 2.96 kB
/*! GPII Cloud Based Flow Manager Ready Check Handler Copyright 2018 OCAD University Licensed under the New BSD license. You may not use this file except in compliance with this License. You may obtain a copy of the License at https://github.com/GPII/universal/blob/master/LICENSE.txt */ "use strict"; var fluid = require("infusion"), gpii = fluid.registerNamespace("gpii"); // TODO: Discuss how/whether to test this. fluid.defaults("gpii.flowManager.cloudBased.ready.handler", { gradeNames: ["kettle.request.http"], invokers: { handleRequest: { funcName: "gpii.flowManager.cloudBased.ready.handler.isLive", args: ["{gpii.flowManager.cloudBased}.prefsServerDataSource", "{that}"] } } }); gpii.flowManager.cloudBased.ready.handler.reportError = function (that, prefsServerURL) { fluid.log ("Error connecting to Preferences Sever: '" + prefsServerURL + "'"); that.events.onError.fire({ isError: true, message: "Error connecting to Preferences Server", statusCode: 503 }); }; gpii.flowManager.cloudBased.ready.handler.isSameHost = function (flowManagerHost, prefsServerHost) { // Convert "http://localhost:8081" to "127.0.0.1:8081" prefsServerHost = prefsServerHost.replace(/http:\/\/|https:\/\//, ""); prefsServerHost = prefsServerHost.replace("localhost", "127.0.0.1"); // Remove the trailing slash if any prefsServerHost = prefsServerHost.replace(/\/$/, ""); flowManagerHost = flowManagerHost.replace(/\/$/, ""); return flowManagerHost === prefsServerHost; }; gpii.flowManager.cloudBased.ready.handler.isLive = function (prefsServerDataSource, that) { var isSameHost = gpii.flowManager.cloudBased.ready.handler.isSameHost(that.req.headers.host, prefsServerDataSource.options.prefsServerURL); // Reaching code here means the cloud based flow manager and the preferences server are running as separate kettle servers. // If the given preferences server host is same as the host of the cloud based flow manager, sending the liveness check to // the preferences server will result in the infinite loop. This case occurs when the preferences server host defined in the // config file is wrong, so report "preferences server is not alive" error. if (isSameHost) { gpii.flowManager.cloudBased.ready.handler.reportError(that, prefsServerDataSource.options.prefsServerURL); } else { var isLivePromise = prefsServerDataSource.isLive(); isLivePromise.then(function (result) { if (result.isReady) { that.events.onSuccess.fire(result); } else { gpii.flowManager.cloudBased.ready.handler.reportError(that, prefsServerDataSource.options.prefsServerURL); } }, function () { gpii.flowManager.cloudBased.ready.handler.reportError(that, prefsServerDataSource.options.prefsServerURL); }); } };