Example #1
0
        private async Task _executeTask(TaskData taskData)
        {
            if (!taskData.ExecuteImmediately)
            {
                await Task.Delay(_options.Value.Interval);
            }

            await Handler.Handle(taskData);

            Interlocked.Decrement(ref _threadCount);
            if (PoolThreadManager != null)
            {
                await PoolThreadManager.Release();
            }
        }
Example #2
0
        public virtual async Task Start()
        {
            var canStart = false;

            lock (_lock)
            {
                if (!Running)
                {
                    Running  = true;
                    canStart = true;
                }
            }

            if (canStart)
            {
                while (Running)
                {
                    TaskData data = null;
                    lock (_lock)
                    {
                        //todo: the sort of queue data is not stable for now.
                        if (_threadCount < _options.Value.MaxThreads && TryDequeue(out data))
                        {
                            if (PoolThreadManager == null || PoolThreadManager.Lock().ConfigureAwait(false)
                                .GetAwaiter().GetResult())
                            {
                                Interlocked.Increment(ref _threadCount);
                            }
                            else
                            {
                                Enqueue(data);
                            }
                        }
                    }

                    if (data != null)
                    {
                        // Execute asynchronously.
                        _executeTask(data);
                    }
                    else
                    {
                        // Avoid unnecessary cpu usage.
                        await Task.Delay(1);
                    }
                }
            }
        }
Example #3
0
        private async void _executeTask(TaskQueue task, TaskData taskData)
        {
            if (!taskData.ExecuteImmediately)
            {
                await Task.Delay(Math.Max(_options.MinInterval, task.Options.Interval));
            }
            var newTaskData = await task.ExecuteAsync(taskData);

            if (_status == TaskQueuePoolStatus.Running && newTaskData?.Any() == true)
            {
                foreach (var d in newTaskData)
                {
                    Enqueue(d);
                }
            }
            Interlocked.Decrement(ref _currentThreadCount);
            Interlocked.Decrement(ref task.CurrentThreadCount);
        }
Example #4
0
 public void Enqueue(TaskData taskData)
 {
     Interlocked.Increment(ref _queuedTaskDataCount);
     _queuedTaskData.Enqueue(taskData);
 }
Example #5
0
 public abstract bool OnException(TaskData taskData, Exception e);
Example #6
0
 public abstract bool MatchedTaskData(TaskData taskData);
Example #7
0
 public abstract Task <List <TaskData> > ExecuteAsync(TaskData taskData);
Example #8
0
 public virtual bool CanHandle(TaskData data)
 {
     return(data is TTaskData);
 }
Example #9
0
 protected virtual Task OnException(TaskData data, Exception e) => Task.CompletedTask;