/// <summary> /// Implementation of the refresh method on RBroker interface /// /// </summary> /// <param name="config">Pooled Broker Configuration object</param> /// <remarks></remarks> public new void refresh(RBrokerConfig config) { if (!status().isIdle) { throw new Exception("RBroker is not idle, refresh not permitted."); } if (!(config is PooledBrokerConfig)) { throw new Exception("PooledTaskBroker refresh requires PooledBrokerConfig."); } PooledBrokerConfig pooledConfig = (PooledBrokerConfig)config; try { /* * Temporarily disable RBroker to permit * configuration refresh. */ Interlocked.Exchange(ref m_refreshingConfig, 1); ProjectExecutionOptions options = ROptionsTranslator.migrate(pooledConfig.poolCreationOptions); foreach (Object resourceToken in m_resourceTokenPool) { RProject rProject = (RProject)resourceToken; /* * Recycle project to remove all existing * workspace objects and directory files. */ rProject.recycle(); /* * Execute code to cause workspace and directory * preloads and adoptions to take place. */ rProject.executeCode("# Refresh project on PooledTaskBroker.", options); } } catch (Exception rex) { throw new Exception("RBroker refresh failed with unexpected error=" + rex.ToString()); } finally { /* * Re-enabled RBroker following * configuration refresh. */ Interlocked.Exchange(ref m_refreshingConfig, 0); } }
/// <summary> /// Constructor for specifying a Pooled Instance of RBroker /// </summary> /// <param name="brokerConfig">Pooled Broker Configuration object</param> /// <remarks></remarks> public PooledTaskBroker(PooledBrokerConfig brokerConfig) : base((RBrokerConfig)brokerConfig) { m_rClient = RClientFactory.createClient(brokerConfig.deployrEndpoint, brokerConfig.maxConcurrentTaskLimit); m_rUser = m_rClient.login(brokerConfig.userCredentials); if (brokerConfig.poolCreationOptions != null) { if (brokerConfig.poolCreationOptions.releaseGridResources == true) { m_rUser.releaseProjects(); } } ProjectCreationOptions options = ROptionsTranslator.translate(brokerConfig.poolCreationOptions); List <RProject> deployrProjectPool = m_rUser.createProjectPool(brokerConfig.maxConcurrentTaskLimit, options); /* * Prep the base RBrokerEngine. */ initEngine(deployrProjectPool.Count()); /* * Initialize the resourceTokenPool with RProject. */ foreach (RProject rProject in deployrProjectPool) { m_resourceTokenPool.TryAdd(rProject); } try { Task.Factory.StartNew(() => HTTPKeepAliveManager(m_rUser)); } catch (Exception rex) { shutdown(); throw new Exception("Broker failed to start HTTP keep-alive manager, cause: " + rex.ToString()); } }
/// <summary> /// Run the acutal discrete task using Project API /// </summary> /// <returns>Results of the pooled task</returns> /// <remarks></remarks> public RTaskResult call() { RTaskResult taskResult = null; long timeOnCall = 0L; //long timeOnServer = 0L; try { ProjectExecutionOptions options = ROptionsTranslator.translate(m_task.options); long startTime = Environment.TickCount; RProjectExecution execResult = m_rProject.executeScript(m_task.filename, m_task.directory, m_task.author, m_task.version, options); timeOnCall = Environment.TickCount - startTime; String generatedConsole = execResult.about().console; List <String> generatedPlots = new List <String>(); if (execResult.about().results != null) { foreach (RProjectResult result in execResult.about().results) { generatedPlots.Add(result.about().url); } } List <String> generatedFiles = new List <String>(); if (execResult.about().artifacts != null) { foreach (RProjectFile artifact in execResult.about().artifacts) { generatedFiles.Add(artifact.about().url); } } List <RData> generatedObjects = execResult.about().workspaceObjects; List <String> storedFiles = new List <String>(); if (execResult.about().repositoryFiles != null) { foreach (RRepositoryFile repoFile in execResult.about().repositoryFiles) { storedFiles.Add(repoFile.about().url); } } taskResult = new RTaskResultImpl(execResult.about().id, RTaskType.POOLED, true, execResult.about().timeCode, execResult.about().timeTotal, timeOnCall, null, false, generatedConsole, generatedPlots, generatedFiles, generatedObjects, storedFiles); } catch (Exception ex) { //if(ex.getCause() instanceof InterruptedException) //{ try { /* * If RTaskToken.cancel() raises InterruptedException * then ensure any corresponding execution on RProject is * also cancelled. */ m_rProject.interruptExecution(); } catch (Exception iex) { throw new Exception("Project cancel Exception occurred: " + iex.ToString()); } //} taskResult = new RTaskResultImpl(null, RTaskType.POOLED, false, 0L, 0L, 0L, ex); } finally { /* * Callback to PooledTaskBroker to release * RProject back into pool for other tasks. */ m_rBroker.callback(m_task, taskResult); } return(taskResult); }
/// <summary> /// Run the acutal discrete task using Client API /// </summary> /// <returns>Results of the discrete task</returns> /// <remarks></remarks> public RTaskResult call() { RTaskResult taskResult = null; long timeOnCall = 0L; //long timeOnServer = 0L; try { AnonymousProjectExecutionOptions options = ROptionsTranslator.translate(m_task.options); long startTime = Environment.TickCount; RScriptExecution execResult = m_rClient.executeScript(m_task.filename, m_task.directory, m_task.author, m_task.version, options); timeOnCall = Environment.TickCount - startTime; String generatedConsole = execResult.about().console; List <String> generatedPlots = new List <String>(); if (execResult.about().results != null) { foreach (RProjectResult result in execResult.about().results) { generatedPlots.Add(result.about().url); } } List <String> generatedFiles = new List <String>(); if (execResult.about().artifacts != null) { foreach (RProjectFile artifact in execResult.about().artifacts) { generatedFiles.Add(artifact.about().url); } } List <RData> generatedObjects = execResult.about().workspaceObjects; List <String> storedFiles = new List <String>(); if (execResult.about().repositoryFiles != null) { foreach (RRepositoryFile repoFile in execResult.about().repositoryFiles) { storedFiles.Add(repoFile.about().url); } } taskResult = new RTaskResultImpl(execResult.about().id, RTaskType.DISCRETE, true, execResult.about().timeCode, execResult.about().timeTotal, timeOnCall, null, false, generatedConsole, generatedPlots, generatedFiles, generatedObjects, storedFiles); } catch (Exception ex) { //if(ex.getCause() == typeof(InterruptedException)) //{ /* * RTaskToken.cancel() can raise an InterruptedException. * When an InterruptedException is detected the DiscreteTask * executing on the server should be aborted at this point. * However, there is no way to obtain DeployR reference, such * as a projectId, for an stateless execution in-progress, so * aborting the current RTask operation is not possible. */ //} taskResult = new RTaskResultImpl(null, RTaskType.DISCRETE, false, 0L, 0L, 0L, ex); } finally { /* * Callback to PooledTaskBroker to release * RProject back into pool for other tasks. */ m_rBroker.callback(m_task, taskResult); } return(taskResult); }
/// <summary> /// Run the acutal background task using Job API /// </summary> /// <returns>Results of the background task</returns> /// <remarks></remarks> public RTaskResult call() { RTaskResult taskResult = null; RJob rJob = null; long timeOnCall = 0L; //long timeOnServer = 0L; try { long startTime = Environment.TickCount; JobExecutionOptions options = ROptionsTranslator.translate(m_task.options, m_isPriorityTask); if(m_task.code != "") { rJob = m_rUser.submitJobCode(m_task.name, m_task.description, m_task.code, options); } else { if(m_task.external != "") { rJob = m_rUser.submitJobExternal(m_task.external, m_task.description, m_task.code, options); } else { rJob = m_rUser.submitJobScript(m_task.name, m_task.description, m_task.filename, m_task.directory, m_task.author, m_task.version, options); } } timeOnCall = Environment.TickCount - startTime; taskResult = new RTaskResultImpl(rJob.about().id, RTaskType.BACKGROUND, true, 0L, 0L, timeOnCall, null); } catch (Exception ex) { //if(ex. instanceof InterruptedException) //{ try { /* * If RTaskToken.cancel() call raises InterruptedException * then ensure any corresponding scheduled RJob is * also cancelled. */ rJob.cancel(); } catch(Exception iex) { throw new Exception("RBroker: could not cancel job, cause: " + iex.ToString()); } //} taskResult = new RTaskResultImpl(null, RTaskType.BACKGROUND, false, 0L, 0L, 0L, ex); } finally { /* * Callback to PooledTaskBroker to release * RProject back into pool for other tasks. */ m_rBroker.callback(m_task, taskResult); } return taskResult; }