Contains state information for an async task
        /// <inheritdoc />
        public TryStartNewResult TryStartNew(Action <object> action, StateInformation state, Action <Task> continueWith, out Task task)
        {
            Guard.NotNull(() => action, action);

            if (SchedulerHasRoom(state))
            {
                lock (_tryStartNewLocker)
                {
                    if (SchedulerHasRoom(state))
                    {
                        try
                        {
                            task = _factory.Value.StartNew(action, state).ContinueWith(continueWith);
                            return(TryStartNewResult.Added);
                        }
                        catch (BulkheadRejectedException e)
                        {
                            //TODO - metrics
                            _log.WarnException("Failed to enqueue task", e);

                            task = null;
                            return(TryStartNewResult.Rejected);
                        }
                    }
                }
            }
            task = null;
            return(TryStartNewResult.Rejected);
        }
        /// <inheritdoc />
        public TryStartNewResult TryStartNew(Action <object> action, StateInformation state, Action <Task> continueWith, out Task task)
        {
            Guard.NotNull(() => action, action);

            if (SchedulerHasRoom(state))
            {
                lock (_tryStartNewLocker)
                {
                    if (SchedulerHasRoom(state))
                    {
                        task = _factory.Value.StartNew(action, state).ContinueWith(continueWith);
                        return(task != null ? TryStartNewResult.Added : TryStartNewResult.Rejected);
                    }
                }
            }
            task = null;
            return(TryStartNewResult.Rejected);
        }
        /// <summary>
        /// Tries to add a new task to the task factory
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="state">The state.</param>
        /// <param name="continueWith">The continue with action.</param>
        /// <param name="task">The task.</param>
        /// <returns>
        /// <see cref="TryStartNewResult"/>
        /// </returns>
        /// <remarks>
        /// The factory may reject the request if it's too busy.
        /// </remarks>
        public TryStartNewResult TryStartNew(Action<object> action, StateInformation state, Action<Task> continueWith, out Task task)
        {
            Guard.NotNull(() => action, action);

            if (SchedulerHasRoom(state))
            {
                lock (_tryStartNewLocker)
                {
                    if (SchedulerHasRoom(state))
                    {
                        task = _factory.Value.StartNew(action, state).ContinueWith(continueWith);
                        return TryStartNewResult.Added;
                    }
                }
            }
            task = null;
            return TryStartNewResult.Rejected;
        }
        /// <summary>
        /// Returns true if the scheduler has room for another task
        /// </summary>
        /// <param name="state">The state.</param>
        /// <returns></returns>
        private bool SchedulerHasRoom(StateInformation state)
        {
            var result = state.Group == null ? Scheduler.RoomForNewTask : Scheduler.RoomForNewWorkGroupTask(state.Group);

            return(result == RoomForNewTaskResult.RoomForTask || result == RoomForNewTaskResult.RoomInQueue);
        }
 /// <summary>
 /// Returns true if the scheduler has room for another task
 /// </summary>
 /// <param name="state">The state.</param>
 /// <returns></returns>
 private bool SchedulerHasRoom(StateInformation state)
 {
     var result = state.Group == null ? Scheduler.RoomForNewTask : Scheduler.RoomForNewWorkGroupTask(state.Group);
     return result == RoomForNewTaskResult.RoomForTask || result == RoomForNewTaskResult.RoomInQueue;
 }
Exemple #6
0
 /// <summary>
 /// A wrapper for executing the task, so that we can return the state information back to the caller
 /// </summary>
 /// <param name="task">The task.</param>
 /// <param name="state">The state.</param>
 /// <returns></returns>
 protected StateInformation TryExecuteTaskWrapped(Task task, StateInformation state)
 {
     TryExecuteTask(task);
     return(state);
 }
 public void AddTask_Workgroup(string value)
 {
     using (var test = Create())
     {
         test.Start();
         var group = test.AddWorkGroup(value, 1);
         var state = new StateInformation(group);
         var t = new Task(state1 => { Thread.Sleep(100); }, state);
         test.AddTask(t);
     }
 }
 /// <summary>
 /// A wrapper for executing the task, so that we can return the state information back to the caller
 /// </summary>
 /// <param name="task">The task.</param>
 /// <param name="state">The state.</param>
 /// <returns></returns>
 protected StateInformation TryExecuteTaskWrapped(Task task, StateInformation state)
 {
     TryExecuteTask(task);
     return state;
 }
 public void Create_With_WorkGroup()
 {
     var group = Substitute.For<IWorkGroup>();
     var test = new StateInformation(group);
     Assert.Equal(group, test.Group);
 }
 public void Create_Null_Constructor_Ok()
 {
     var test = new StateInformation(null);
     Assert.Null(test.Group);
 }