/// <summary> /// Create a new ContextRuntime. /// </summary> /// <param name="serviceInjector"></param> /// <param name="contextConfiguration">the Configuration for this context.</param> /// <param name="parentContext"></param> public ContextRuntime( IInjector serviceInjector, IConfiguration contextConfiguration, Optional <ContextRuntime> parentContext) { ContextConfiguration config = contextConfiguration as ContextConfiguration; if (config == null) { var e = new ArgumentException("contextConfiguration is not of type ContextConfiguration"); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } _contextLifeCycle = new ContextLifeCycle(config.Id); _serviceInjector = serviceInjector; _parentContext = parentContext; try { _contextInjector = serviceInjector.ForkInjector(); } catch (Exception e) { Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); Optional <string> parentId = ParentContext.IsPresent() ? Optional <string> .Of(ParentContext.Value.Id) : Optional <string> .Empty(); ContextClientCodeException ex = new ContextClientCodeException(ContextClientCodeException.GetId(contextConfiguration), parentId, "Unable to spawn context", e); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } // Trigger the context start events on contextInjector. _contextLifeCycle.Start(); }
/// <summary> /// Spawns a new context. /// The new context will have a serviceInjector that is created by forking the one in this object with the given /// serviceConfiguration. The contextConfiguration is used to fork the contextInjector from that new serviceInjector. /// </summary> /// <param name="contextConfiguration">the new context's context (local) Configuration.</param> /// <param name="serviceConfiguration">the new context's service Configuration.</param> /// <returns>a child context.</returns> public ContextRuntime SpawnChildContext(IConfiguration contextConfiguration, IConfiguration serviceConfiguration) { ContextRuntime childContext = null; lock (_contextLifeCycle) { if (_task.IsPresent()) { var e = new InvalidOperationException( string.Format(CultureInfo.InvariantCulture, "Attempting to spawn a child context when an Task with id '{0}' is running", _task.Value.TaskId)); // note: java code is putting thread id here Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } if (_childContext.IsPresent()) { var e = new InvalidOperationException("Attempting to instantiate a child context on a context that is not the topmost active context."); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } try { IInjector childServiceInjector = _serviceInjector.ForkInjector(new IConfiguration[] { serviceConfiguration }); childContext = new ContextRuntime(childServiceInjector, contextConfiguration, Optional <ContextRuntime> .Of(this)); _childContext = Optional <ContextRuntime> .Of(childContext); return(childContext); } catch (Exception e) { Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); Optional <string> parentId = ParentContext.IsPresent() ? Optional <string> .Of(ParentContext.Value.Id) : Optional <string> .Empty(); ContextClientCodeException ex = new ContextClientCodeException(ContextClientCodeException.GetId(contextConfiguration), parentId, "Unable to spawn context", e); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } } return(childContext); }