Beispiel #1
0
        /// <summary>
        /// This is the constructor for the task manager.
        /// </summary>
        /// <param name="dispatcher">The dispatcher function that is used to process the specific tasks.</param>
        /// <param name="policy">The task manager policy.</param>
        public TaskManager(Func <TransmissionPayload, Task> dispatcher, TaskManager.Policy policy)
            : base(policy, nameof(TaskManager))
        {
            if (policy == null)
            {
                throw new ArgumentNullException($"{nameof(TaskManager)}: policy can not be null");
            }

            mPauseCheck = new ManualResetEventSlim();

            mAvailability = new TaskAvailability(policy.PriorityLevels, policy.ConcurrentRequestsMax);

            mTasksQueue = new QueueTrackerContainer <QueueTracker>(policy.PriorityLevels);

            mProcessInternalQueue = new ConcurrentQueue <TaskTracker>();

            mProcesses = new ConcurrentDictionary <string, TaskManagerProcessContext>();

            Dispatcher = dispatcher ?? throw new ArgumentNullException($"{nameof(TaskManager)}: dispatcher can not be null");

            if (!mPolicy.ProcessKillOverrunGracePeriod.HasValue)
            {
                mPolicy.ProcessKillOverrunGracePeriod = TimeSpan.FromSeconds(15);
            }

            mTaskRequests = new ConcurrentDictionary <Guid, TaskTracker>();
        }
        /// <summary>
        /// This method builds the task tracker for the listener poll.
        /// </summary>
        /// <param name="context">The client priority holder context.</param>
        /// <returns>Returns a tracker of type listener poll.</returns>
        private void TrackerSubmitFromListener(IListener context)
        {
            TaskTracker tracker = new TaskTracker(TaskTrackerType.ListenerPoll, mPolicy.ListenerRequestTimespan)
            {
                Priority = TaskTracker.PriorityInternal,
                Context  = context,
                Name     = context.ChannelId
            };

            tracker.Execute = async t =>
            {
                var currentContext = ((IListener)tracker.Context);

                await currentContext.Poll();
            };

            tracker.ExecuteComplete = (tr, failed, ex) =>
            {
                var currentContext = ((ClientPriorityHolder)tr.Context);
                TaskAvailability.ReservationRelease(currentContext.Id);
                currentContext.Release(failed);
            };

            TaskSubmit(tracker);
        }
        /// <summary>
        /// This method builds the task tracker for the listener poll.
        /// </summary>
        /// <param name="context">The client priority holder context.</param>
        /// <returns>Returns a tracker of type listener poll.</returns>
        private void TrackerSubmitFromClientPriorityHolder(ClientPriorityHolder context)
        {
            TaskTracker tracker = new TaskTracker(TaskTrackerType.ListenerClientPoll, TimeSpan.FromSeconds(30))
            {
                Priority = TaskTracker.PriorityInternal,
                Context  = context,
                Name     = context.Name
            };

            tracker.Execute = async t =>
            {
                var currentContext = ((ClientPriorityHolder)tracker.Context);

                var payloads = await currentContext.Poll();

                if (payloads != null && payloads.Count > 0)
                {
                    foreach (var payload in payloads)
                    {
                        PayloadSubmit(currentContext.ClientId, payload);
                    }
                }
            };

            tracker.ExecuteComplete = (tr, failed, ex) =>
            {
                var currentContext = ((ClientPriorityHolder)tr.Context);
                TaskAvailability.ReservationRelease(currentContext.Id);
                currentContext.Release(failed);
            };

            TaskSubmit(tracker);
        }
        /// <summary>
        /// This method builds the task tracker for the listener poll.
        /// </summary>
        /// <param name="context">The client priority holder context.</param>
        /// <returns>Returns a tracker of type listener poll.</returns>
        private void TrackerSubmitFromClientPriorityHolder(ClientPriorityHolder context)
        {
            //Create the task that will poll the client for incoming messages.
            TaskTracker tracker = new TaskTracker(TaskTrackerType.ListenerClientPoll, mPolicy.ListenerRequestTimespan)
            {
                Priority = TaskTracker.PriorityInternal,
                Context  = context,
                Name     = context.Name
            };

            //Set the execute function that polls the context and retrieves incoming payloads from the fabric
            //and submits them to be processed.
            tracker.Execute = async t =>
            {
                var currentContext = ((ClientPriorityHolder)tracker.Context);

                var payloads = await currentContext.Poll();

                if (payloads != null && payloads.Count > 0)
                {
                    foreach (var payload in payloads)
                    {
                        PayloadSubmit(currentContext.ClientId, payload);
                    }
                }
            };

            //Set the completion function that releases the slot reservations back in to the pool.
            tracker.ExecuteComplete = (tr, failed, ex) =>
            {
                var currentContext = ((ClientPriorityHolder)tr.Context);
                TaskAvailability.ReservationRelease(currentContext.Id);
                currentContext.Release(failed);
            };

            //Submit the tracker for processing.
            TaskSubmit(tracker);
        }