Exemplo n.º 1
0
        public List <Story> GetStories(int topStories)
        {
            var maxStoryId = _repo.GetMaxItemId("story");

            var task = _repo.GetRangeItemRecords("story", topStories, maxStoryId, true);

            task.Wait();

            var itemRecords = task.Result;

            return(TransformItemToStory(itemRecords));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Start the Host as an asynchronous operation.
        /// </summary>
        /// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
        /// <returns>Task.</returns>
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            Log.Info().Message("Starting WebJob via StartAsync").Write();

            while (true)
            {
                try
                {
                    _repo.UpdateRecordStats();

                    var maxItemId        = _repo.GetMaxItemId();
                    var startItemId      = maxItemId + 1;
                    var maxTakeItem      = _settings.MaxTakeItem;
                    var getMaxItemIdTask = _service.GetMaxItemId();
                    getMaxItemIdTask.Wait(cancellationToken);
                    var endItemId = getMaxItemIdTask.Result;
                    var remaining = endItemId - maxItemId;

                    var takeItem = Math.Min(maxTakeItem, remaining);

                    var getItemsTask = _service.GetItems(startItemId, takeItem);
                    getItemsTask.Wait(cancellationToken);
                    var newItems = getItemsTask.Result;

                    _repo.InsertItemRecords(newItems);
                }
                catch (Exception e)
                {
                    Log.Error().Exception(e).Message("Failed to load data from Hr News Web Api.").Write();
                }

                Thread.Sleep(TimeSpan.FromSeconds(2));
            }
        }