Example #1
0
        /// <inheritdoc />
        public ICrawlerTaskManager Create()
        {
            var crawlerTaskMetadatas = new List <CrawlerTaskMetadata>();

            // 遍历所有已经加载的 CLR 类型
            _logger.LogInformation("探查后台任务类型...");
            foreach (var loadedAssembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (var t in loadedAssembly.GetTypes())
                {
                    var annotation = t.GetCustomAttribute <CrawlerTaskAttribute>();
                    if (annotation == null)
                    {
                        continue;
                    }

                    if (t.GetInterface(nameof(ICrawlerTask)) == null)
                    {
                        continue;
                    }

                    // t 带有 CrawlerTaskAttribute 注解且实现了 ICrawlerTask
                    _logger.LogInformation("找到后台任务类型:{0},启动间隔为 {1} 分钟", t, annotation.Interval);
                    var metadata = new CrawlerTaskMetadata(t, annotation);
                    crawlerTaskMetadatas.Add(metadata);
                }
            }

            return(new DefaultCrawlerTaskManager(crawlerTaskMetadatas, _serviceProvider, _logger));
        }
 public CrawlerTaskScheduleDescriptor(int nextRunTimestamp, CrawlerTaskMetadata metadata,
                                      IServiceProvider serviceProvider)
 {
     NextRunTimestamp = nextRunTimestamp;
     Metadata         = metadata;
     TaskObject       = new Lazy <ICrawlerTask>(() => Metadata.ActivateTaskObject(serviceProvider));
 }