Exemple #1
0
        public void Execute(ITask task, CancellationToken cancelationToken)
        {
            if (!this.isExecuting && !cancelationToken.IsCancellationRequested)
            {
                this.isExecuting = true;

                this.logger.LogInformation($"Start prosessing task \"{task.KeyWord}\"");

                var steps = this.stepManager.GetStepsFor(task.KeyWord);

                if (steps.Count < 1)
                {
                    //TODO: Have an option to use to the next keyword???
                    this.logger.LogWarning($"No suitable steps found for keyword \"{task.KeyWord}\"");
                    return;
                }

                var allSteps = steps.SelectMany(m => m.CreateSteps()).ToList();
                this.countdown = new CountdownEvent(allSteps.Count);
                Console.WriteLine("Signals: " + allSteps.Count);
                System.Threading.Tasks.Task.Factory.StartNew(() => {
                    this.countdown.Wait();
                    this.isExecuting = false;
                    this.logger.LogInformation($"Task \"{task.KeyWord}\" completed");
                }, cancelationToken);

                allSteps.ForEach(step => System.Threading.Tasks.Task.Factory.StartNew(() => {
                    // in theory the condition could modify the task,
                    try
                    {
                        if (step.Condition(task) && !cancelationToken.IsCancellationRequested)
                        {
                            this.logger.LogInformation($"Process {task.KeyWord} with {task.Records.Count} records.");
                            this.createdTasks.Consume(step.Process((ITask)task.Clone(), cancelationToken));
                        }
                    }
                    finally
                    {
                        this.countdown.Signal();
                        Console.WriteLine("Signal");
                    }
                }, cancelationToken));
            }
        }
            public ITaskCollection Process(ITask task, CancellationToken token)
            {
                var collection = new TaskCollection();

                var newTask = (ITask)task.Clone();

                newTask.KeyWord = "";

                if (!string.IsNullOrWhiteSpace(this.next))
                {
                    newTask.KeyWord = this.next;
                }

                var record = newTask.Records.FirstOrDefault();

                newTask.Records.Clear();

                newTask.Records.Add(new Core.Data.Record(record.Key, record.ContentHint, record.Content + this.add));

                collection.Add(newTask);

                return(collection);
            }