public void CanInjectAndExecuteTask() { // to enforce that shell task dll be copied to output directory. ShellTask tmpTask = new ShellTask("invalid"); Assert.NotNull(tmpTask); string tmp = Directory.GetCurrentDirectory(); Assert.NotNull(tmp); AvroConfigurationSerializer serializer = new AvroConfigurationSerializer(); AvroConfiguration avroConfiguration = serializer.AvroDeserializeFromFile("evaluator.conf"); Assert.NotNull(avroConfiguration); ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); cb.AddConfiguration(TaskConfiguration.ConfigurationModule .Set(TaskConfiguration.Identifier, "Test_CLRContext_task") .Set(TaskConfiguration.Task, GenericType<ShellTask>.Class) .Build()); cb.BindNamedParameter<ShellTask.Command, string>(GenericType<ShellTask.Command>.Class, "dir"); IConfiguration taskConfiguration = cb.Build(); string taskConfig = serializer.ToString(taskConfiguration); ITask task = null; TaskConfiguration config = new TaskConfiguration(taskConfig); Assert.NotNull(config); try { IInjector injector = TangFactory.GetTang().NewInjector(config.TangConfig); task = (ITask)injector.GetInstance(typeof(ITask)); } catch (Exception e) { throw new InvalidOperationException("unable to inject task with configuration: " + taskConfig, e); } byte[] bytes = task.Call(null); string result = System.Text.Encoding.Default.GetString(bytes); // a dir command is executed in the container directory, which includes the file "evaluator.conf" Assert.True(result.Contains("evaluator.conf")); }
/// <summary> /// Launch an Task. /// </summary> /// <param name="startTaskProto"></param> private void StartTask(StartTaskProto startTaskProto) { lock (_contextLock) { ContextRuntime currentActiveContext = _topContext; string expectedContextId = startTaskProto.context_id; if (!expectedContextId.Equals(currentActiveContext.Id, StringComparison.OrdinalIgnoreCase)) { var e = new InvalidOperationException( string.Format(CultureInfo.InvariantCulture, "Task expected context '{0}' but the active context has Id '{1}'", expectedContextId, currentActiveContext.Id)); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } TaskConfiguration taskConfiguration = new TaskConfiguration(startTaskProto.configuration); currentActiveContext.StartTask(taskConfiguration, expectedContextId, _heartBeatManager); } }
/// <summary> /// Launches an Task on this context. /// </summary> /// <param name="taskConfiguration"></param> /// <param name="contextId"></param> /// <param name="heartBeatManager"></param> public void StartTask(TaskConfiguration taskConfiguration, string contextId, HeartBeatManager heartBeatManager) { lock (_contextLifeCycle) { bool taskPresent = _task.IsPresent(); bool taskEnded = taskPresent && _task.Value.HasEnded(); LOGGER.Log(Level.Info, "ContextRuntime::StartTask(TaskConfiguration)" + "task is present: " + taskPresent + " task has ended: " + taskEnded); if (taskPresent) { LOGGER.Log(Level.Info, "Task state: " + _task.Value.GetTaskState()); } if (taskEnded) { // clean up state _task = Optional<TaskRuntime>.Empty(); taskPresent = false; } if (taskPresent) { 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 taskInjector = _contextInjector.ForkInjector(taskConfiguration.TangConfig); LOGGER.Log(Level.Info, "Trying to inject task with configuration" + taskConfiguration.ToString()); TaskRuntime taskRuntime = new TaskRuntime(taskInjector, contextId, taskConfiguration.TaskId, heartBeatManager); // taskInjector.getInstance(TaskRuntime.class); taskRuntime.Initialize(); System.Threading.Tasks.Task.Run(new Action(taskRuntime.Start)); _task = Optional<TaskRuntime>.Of(taskRuntime); } catch (Exception e) { var ex = new TaskClientCodeException(taskConfiguration.TaskId, Id, "Unable to instantiate the new task", e); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.CaughtAndThrow(ex, Level.Error, "Task start error.", LOGGER); } } }