Beispiel #1
0
        /// <summary>
        /// Runs the task asynchronously.
        /// </summary>
        public System.Threading.Tasks.Task RunTask()
        {
            if (Interlocked.Exchange(ref _taskRan, 1) != 0)
            {
                // Return if we have already called RunTask
                throw new InvalidOperationException("TaskRun has already been called on TaskRuntime.");
            }

            // Send heartbeat such that user receives a TaskRunning message.
            _currentStatus.SetRunning();

            return(System.Threading.Tasks.Task.Run(() =>
            {
                Logger.Log(Level.Info, "Calling into user's task.");
                return _userTask.Call(null);
            }).ContinueWith(runTask =>
            {
                try
                {
                    // Task failed.
                    if (runTask.IsFaulted)
                    {
                        Logger.Log(Level.Warning,
                                   string.Format(CultureInfo.InvariantCulture, "Task failed caused by exception [{0}]", runTask.Exception));
                        _currentStatus.SetException(runTask.Exception);
                        return;
                    }

                    if (runTask.IsCanceled)
                    {
                        Logger.Log(Level.Warning,
                                   string.Format(CultureInfo.InvariantCulture, "Task failed caused by task cancellation"));
                        return;
                    }

                    // Task completed.
                    var result = runTask.Result;
                    Logger.Log(Level.Info, "Task Call Finished");
                    _currentStatus.SetResult(result);
                    if (result != null && result.Length > 0)
                    {
                        Logger.Log(Level.Info, "Task running result:\r\n" + System.Text.Encoding.Default.GetString(result));
                    }
                }
                finally
                {
                    if (_userTask != null)
                    {
                        _userTask.Dispose();
                    }

                    runTask.Dispose();
                }
            }));
        }
        public void CanInjectAndExecuteTask()
        {
            //To enforce that shell task dll be copied to output directory.
            ShellTask tmpTask = new ShellTask("invalid");

            Assert.IsNotNull(tmpTask);

            string tmp = Directory.GetCurrentDirectory();

            Assert.IsNotNull(tmp);

            AvroConfigurationSerializer serializer        = new AvroConfigurationSerializer();
            AvroConfiguration           avroConfiguration = serializer.AvroDeseriaizeFromFile("evaluator.conf");

            Assert.IsNotNull(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.IsNotNull(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.IsTrue(result.Contains("evaluator.conf"));
        }
Beispiel #3
0
        /// <summary>
        /// Runs the task asynchronously.
        /// </summary>
        public Thread StartTaskOnNewThread()
        {
            if (Interlocked.Exchange(ref _taskRan, 1) != 0)
            {
                // Return if we have already called StartTaskOnNewThread
                throw new InvalidOperationException("TaskRun has already been called on TaskRuntime.");
            }

            _currentStatus.SetInit();

            var taskThread = new Thread(() =>
            {
                // The result of the task execution.
                byte[] resultReturnedByTask = null;

                // Whether or not a result shall be returned to the Driver.
                bool returnResultToDriver = true;

                // Exception thrown during `Dispose`, if any.
                Exception exceptionThrownByTaskDispose = null;

                try
                {
                    // Run the handlers for `TaskStart`
                    Logger.Log(Level.Verbose, "Set running status for task");
                    _currentStatus.RunTaskStartHandlers();

                    // Update the state
                    _currentStatus.SetRunning();

                    // Call the Task
                    Logger.Log(Level.Verbose, "Calling into user's task.");
                    resultReturnedByTask = _userTask.Call(null);
                    Logger.Log(Level.Info, "Task Call Finished");

                    if (!_currentStatus.HasEnded())
                    {
                        Logger.Log(Level.Info, "Run the task stop handlers");
                        _currentStatus.RunTaskStopHandlers();
                        // Log the result
                        const Level resultLogLevel = Level.Verbose;
                        if (Logger.IsLoggable(resultLogLevel) && resultReturnedByTask != null &&
                            resultReturnedByTask.Length > 0)
                        {
                            Logger.Log(resultLogLevel,
                                       "Task running result:\r\n" +
                                       System.Text.Encoding.Default.GetString(resultReturnedByTask));
                        }
                    }
                    else
                    {
                        Logger.Log(Level.Info, "Task not running current state {0}", _currentStatus.State);
                        returnResultToDriver = false;
                    }
                }
                catch (TaskStartHandlerException e)
                {
                    Logger.Log(Level.Info, "TaskRuntime::TaskStartHandlerException");
                    _currentStatus.SetException(e.InnerException);
                    returnResultToDriver = false;
                }
                catch (TaskStopHandlerException e)
                {
                    Logger.Log(Level.Info, "TaskRuntime::TaskStopHandlerException");
                    _currentStatus.SetException(e.InnerException);
                    returnResultToDriver = false;
                }
                catch (Exception e)
                {
                    Logger.Log(Level.Info, "TaskRuntime::Exception {0}", e.GetType());
                    _currentStatus.SetException(e);
                    returnResultToDriver = false;
                }
                finally
                {
                    try
                    {
                        Logger.Log(Level.Info, "Try to dispose of task");
                        _userTask.Dispose();
                        Logger.Log(Level.Info, "task diposed");
                    }
                    catch (Exception e)
                    {
                        exceptionThrownByTaskDispose = new InvalidOperationException("Exception during Task Dispose in task Call()", e);
                    }
                }

                // Inform the driver about the result.
                if (returnResultToDriver)
                {
                    _currentStatus.SetResult(resultReturnedByTask);
                }

                // If the ITask.Dispose() method threw an Exception, crash the Evaluator.
                if (exceptionThrownByTaskDispose != null)
                {
                    throw exceptionThrownByTaskDispose;
                }
            });

            taskThread.Start();
            return(taskThread);
        }
Beispiel #4
0
 private byte[] RunTask(byte[] memento)
 {
     return(_task.Call(memento));
 }
Beispiel #5
0
        /// <summary>
        /// Runs the task asynchronously.
        /// </summary>
        public Thread StartTaskOnNewThread()
        {
            if (Interlocked.Exchange(ref _taskRan, 1) != 0)
            {
                // Return if we have already called StartTaskOnNewThread
                throw new InvalidOperationException("TaskRun has already been called on TaskRuntime.");
            }

            _currentStatus.SetInit();

            var taskThread = new Thread(() =>
            {
                try
                {
                    Logger.Log(Level.Verbose, "Set running status for task");
                    _currentStatus.SetRunning();
                    Logger.Log(Level.Verbose, "Calling into user's task.");
                    var result = _userTask.Call(null);
                    Logger.Log(Level.Info, "Task Call Finished");
                    _currentStatus.SetResult(result);

                    const Level resultLogLevel = Level.Verbose;

                    if (Logger.IsLoggable(resultLogLevel) && result != null && result.Length > 0)
                    {
                        Logger.Log(resultLogLevel,
                                   "Task running result:\r\n" + System.Text.Encoding.Default.GetString(result));
                    }
                }
                catch (TaskStartHandlerException e)
                {
                    Logger.Log(Level.Info, "TaskRuntime::TaskStartHandlerException");
                    _currentStatus.SetException(e.InnerException);
                }
                catch (TaskStopHandlerException e)
                {
                    Logger.Log(Level.Info, "TaskRuntime::TaskStopHandlerException");
                    _currentStatus.SetException(e.InnerException);
                }
                catch (Exception e)
                {
                    Logger.Log(Level.Info, "TaskRuntime::Exception {0}", e.GetType());
                    _currentStatus.SetException(e);
                }
                finally
                {
                    try
                    {
                        if (_userTask != null)
                        {
                            _userTask.Dispose();
                        }
                    }
                    catch (Exception e)
                    {
                        var msg = "Exception during Task Dispose in task Call()";
                        Logger.Log(Level.Error, msg);
                        throw new InvalidOperationException(msg, e);
                    }
                }
            });

            taskThread.Start();
            return(taskThread);
        }