Exemple #1
0
        /// <summary>
        /// 订阅 Redis的消息队列, 实现消息队列对爬虫的控制
        /// </summary>
        /// <param name="spider">爬虫</param>
        protected void RegisterControl(ISpider spider)
        {
            if (RedisConnection.Default != null)
            {
                try
                {
                    RedisConnection.Default.Subscriber.Subscribe($"{spider.Identity}", (c, m) =>
                    {
                        switch (m)
                        {
                        case "PAUSE":
                            {
                                spider.Pause();
                                break;
                            }

                        case "CONTINUE":
                            {
                                spider.Contiune();
                                break;
                            }

                        case "RUNASYNC":
                            {
                                spider.RunAsync();
                                break;
                            }

                        case "EXIT":
                            {
                                spider.Exit();
                                break;
                            }
                        }
                    });
                }
                catch (Exception e)
                {
                    Logger.Error($"Register contol failed:{e}.");
                }
            }
        }
        private void RegisterControl(ISpider spider)
        {
            if (RedisConnection != null)
            {
                try
                {
                    RedisConnection.Subscriber.Subscribe($"{spider.Identity}", (c, m) =>
                    {
                        switch (m)
                        {
                        case "PAUSE":
                            {
                                spider.Pause();
                                break;
                            }

                        case "CONTINUE":
                            {
                                spider.Contiune();
                                break;
                            }

                        case "RUNASYNC":
                            {
                                spider.RunAsync();
                                break;
                            }

                        case "EXIT":
                            {
                                spider.Exit();
                                break;
                            }
                        }
                    });
                }
                catch (Exception e)
                {
                    spider.Log("Register contol failed.", LogLevel.Error, e);
                }
            }
        }