Esempio n. 1
0
        /// <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);
                }
            }
        }