From 77bc00e4105f0aae31d64abb817516d8375e0a94 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 16 Dec 2014 18:34:39 -0800 Subject: [PATCH] a simple model for persistent scripts in domain-server --- .../resources/describe-settings.json | 27 +++++++++ domain-server/resources/web/js/settings.js | 8 ++- domain-server/src/DomainServer.cpp | 59 ++++++++++--------- domain-server/src/DomainServer.h | 2 +- 4 files changed, 64 insertions(+), 32 deletions(-) diff --git a/domain-server/resources/describe-settings.json b/domain-server/resources/describe-settings.json index 50de26c518..ae6f116537 100644 --- a/domain-server/resources/describe-settings.json +++ b/domain-server/resources/describe-settings.json @@ -76,6 +76,33 @@ } ] }, + { + "name": "scripts", + "label": "Scripts", + "settings": [ + { + "name": "persistent_scripts", + "type": "table", + "label": "Persistent Scripts", + "help": "Add the URLs for scripts that you would like to ensure are always running in your domain.", + "columns": [ + { + "name": "url", + "label": "Script URL" + }, + { + "name": "num_instances", + "label": "# instances", + "default": 1 + }, + { + "name": "pool", + "label": "Pool" + } + ] + } + ] + }, { "name": "audio_env", "label": "Audio Environment", diff --git a/domain-server/resources/web/js/settings.js b/domain-server/resources/web/js/settings.js index a27965abce..141bd72a26 100644 --- a/domain-server/resources/web/js/settings.js +++ b/domain-server/resources/web/js/settings.js @@ -363,7 +363,8 @@ function makeTableInputs(setting) { _.each(setting.columns, function(col) { html += "\ - \ + \ " }) @@ -389,8 +390,9 @@ function badgeSidebarForDifferences(changedElement) { // badge for any settings we have that are not the same or are not present in initialValues for (var setting in panelJSON) { - if (!_.isEqual(panelJSON[setting], initialPanelJSON[setting]) - && (panelJSON[setting] !== "" || _.has(initialPanelJSON, setting))) { + if ((!_.has(initialPanelJSON, setting) && panelJSON[setting] !== "") || + (!_.isEqual(panelJSON[setting], initialPanelJSON[setting]) + && (panelJSON[setting] !== "" || _.has(initialPanelJSON, setting)))) { badgeValue += 1 } } diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index bf9505671b..470b69be01 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -233,6 +233,9 @@ void DomainServer::setupNodeListAndAssignments(const QUuid& sessionUUID) { parseAssignmentConfigs(parsedTypes); populateDefaultStaticAssignmentsExcludingTypes(parsedTypes); + + // check for scripts the user wants to persist from their domain-server config + populateStaticScriptedAssignmentsFromSettings(); LimitedNodeList* nodeList = LimitedNodeList::createInstance(domainServerPort, domainServerDTLSPort); @@ -451,8 +454,6 @@ void DomainServer::parseAssignmentConfigs(QSet& excludedTypes) if (assignmentType != Assignment::AgentType) { createStaticAssignmentsForType(assignmentType, assignmentList); - } else { - createScriptedAssignmentsFromList(assignmentList); } excludedTypes.insert(assignmentType); @@ -468,35 +469,37 @@ void DomainServer::addStaticAssignmentToAssignmentHash(Assignment* newAssignment _allAssignments.insert(newAssignment->getUUID(), SharedAssignmentPointer(newAssignment)); } -void DomainServer::createScriptedAssignmentsFromList(const QVariantList &configList) { - foreach(const QVariant& configVariant, configList) { - if (configVariant.canConvert(QMetaType::QVariantMap)) { - QVariantMap configMap = configVariant.toMap(); - - // make sure we were passed a URL, otherwise this is an invalid scripted assignment - const QString ASSIGNMENT_URL_KEY = "url"; - QString assignmentURL = configMap[ASSIGNMENT_URL_KEY].toString(); - - if (!assignmentURL.isEmpty()) { - // check the json for a pool - const QString ASSIGNMENT_POOL_KEY = "pool"; - QString assignmentPool = configMap[ASSIGNMENT_POOL_KEY].toString(); - - // check for a number of instances, if not passed then default is 1 - const QString ASSIGNMENT_INSTANCES_KEY = "instances"; - int numInstances = configMap[ASSIGNMENT_INSTANCES_KEY].toInt(); - numInstances = (numInstances == 0 ? 1 : numInstances); - - qDebug() << "Adding a static scripted assignment from" << assignmentURL; - - for (int i = 0; i < numInstances; i++) { +void DomainServer::populateStaticScriptedAssignmentsFromSettings() { + const QString PERSISTENT_SCRIPTS_KEY_PATH = "scripts.persistent_scripts"; + const QVariant* persistentScriptsVariant = valueForKeyPath(_settingsManager.getSettingsMap(), PERSISTENT_SCRIPTS_KEY_PATH); + + if (persistentScriptsVariant) { + QVariantList persistentScriptsList = persistentScriptsVariant->toList(); + foreach(const QVariant& persistentScriptVariant, persistentScriptsList) { + QVariantMap persistentScript = persistentScriptVariant.toMap(); + + const QString PERSISTENT_SCRIPT_URL_KEY = "url"; + const QString PERSISTENT_SCRIPT_NUM_INSTANCES_KEY = "num_instances"; + const QString PERSISTENT_SCRIPT_POOL_KEY = "pool"; + + if (persistentScript.contains(PERSISTENT_SCRIPT_URL_KEY)) { + // check how many instances of this script to add + + int numInstances = persistentScript[PERSISTENT_SCRIPT_NUM_INSTANCES_KEY].toInt(); + QString scriptURL = persistentScript[PERSISTENT_SCRIPT_URL_KEY].toString(); + + QString scriptPool = persistentScript.value(PERSISTENT_SCRIPT_POOL_KEY).toString(); + + qDebug() << "Adding" << numInstances << "of persistent script at URL" << scriptURL << "- pool" << scriptPool; + + for (int i = 0; i < numInstances; ++i) { // add a scripted assignment to the queue for this instance Assignment* scriptAssignment = new Assignment(Assignment::CreateCommand, Assignment::AgentType, - assignmentPool); - scriptAssignment->setPayload(assignmentURL.toUtf8()); - - // scripts passed on CL or via JSON are static - so they are added back to the queue if the node dies + scriptPool); + scriptAssignment->setPayload(scriptURL.toUtf8()); + + // add it to static hash so we know we have to keep giving it back out addStaticAssignmentToAssignmentHash(scriptAssignment); } } diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index 76ab562f74..f910534eb1 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -100,9 +100,9 @@ private: void parseAssignmentConfigs(QSet& excludedTypes); void addStaticAssignmentToAssignmentHash(Assignment* newAssignment); - void createScriptedAssignmentsFromList(const QVariantList& configList); void createStaticAssignmentsForType(Assignment::Type type, const QVariantList& configList); void populateDefaultStaticAssignmentsExcludingTypes(const QSet& excludedTypes); + void populateStaticScriptedAssignmentsFromSettings(); SharedAssignmentPointer matchingQueuedAssignmentForCheckIn(const QUuid& checkInUUID, NodeType_t nodeType); SharedAssignmentPointer deployableAssignmentForRequest(const Assignment& requestAssignment);