Cronbot.cs 997 Bytes
Newer Older
Ken's avatar
Ken committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
using Microsoft.Extensions.Logging;
using News_site.Provider;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace News_site.Services
{
	public class Cronbot : ICronbot
	{
		private readonly ILogger<Cronbot> _logger;
		private readonly CrawlDataProvider _dataProvider = new CrawlDataProvider();

		public Cronbot(ILogger<Cronbot> logger)
		{
			_logger = logger;
		}

		public async Task DoCrawlData(CancellationToken cancellationToken)
		{
			while (!cancellationToken.IsCancellationRequested)
			{
				/* 30 minutes */
				var duration = 1000 * 60 * 30;
				var startCrawlingTime = DateTime.Now;
				_logger.LogInformation($"Start crawling at: {startCrawlingTime:HH:mm}");

				await _dataProvider.CrawlCategories();
				await _dataProvider.CrawlLatestNews();

				var stopCrawlingTime = DateTime.Now;
				_logger.LogInformation($"Stop task, crawling in {(stopCrawlingTime - startCrawlingTime).TotalSeconds} seconds");

				await Task.Delay(duration);
			}
		}
	}
}