コード例 #1
0
        private void OnStarted()
        {
            _running = true;
            _logger.LogInformation("OnStarted has been called.");

            Task.Run(async() =>
            {
                while (_running)
                {
                    try
                    {
                        var localLatest   = await _summary.Latest();
                        var(values, seqn) = await _bNetClient.Do <List <BNetLib.Models.Summary> >(new SummaryCommand());

                        if (localLatest?.Seqn != seqn)
                        {
                            await _summary.Insert(new BTSharedCore.Models.Summary()
                            {
                                Seqn  = seqn,
                                Value = values
                            });
                        }

                        foreach (var item in values)
                        {
                            var previous = localLatest?.Value?.FirstOrDefault(x =>
                                                                              x.Product == item.Product && x.Flags == item.Flags);

                            var exist = await ItemExist(item);

                            if (!exist || previous == null || previous.Seqn != item.Seqn)
                            {
                                _queue.Enqueue(item);
                            }
                        }
                    }
                    catch
                    {
                        // ignored
                    }

                    await Task.Delay(TimeSpan.FromMinutes(1), _cancellationToken);
                }
            }, _cancellationToken);
        }
コード例 #2
0
        private void OnStarted()
        {
            _running = true;

            _logger.LogInformation("OnStarted has been called.");

            _running = true;
            Task.Run(async() =>
            {
                while (_running)
                {
                    if (_queue.Count == 0)
                    {
                        await Task.Delay(TimeSpan.FromSeconds(1));
                        continue;
                    }

                    if (!_queue.TryDequeue(out var item))
                    {
                        await Task.Delay(TimeSpan.FromSeconds(1));
                        continue;
                    }

                    try
                    {
                        _logger.LogInformation($"Updating {item.Product} to {item.Seqn}");
                        switch (item.Flags.ToLower())
                        {
                        case "cdn":
                            var(cdn_value, cdn_seqn) = await _bNetClient.Do <List <BNetLib.Models.CDN> >(
                                new CDNCommand(item.Product.ToLower()));
                            await _cdn.Insert(new BTSharedCore.Models.CDN()
                            {
                                Seqn    = cdn_seqn,
                                Value   = cdn_value,
                                Product = item.Product,
                            });
                            break;

                        case "versions":
                            var(version_value, version_seqn) =
                                await _bNetClient.Do <List <BNetLib.Models.Version> >(
                                    new VersionCommand(item.Product.ToLower()));

                            await _versions.Insert(new BTSharedCore.Models.Version()
                            {
                                Seqn    = version_seqn,
                                Value   = version_value,
                                Product = item.Product,
                            });
                            break;

                        case "bgdl":
                            var(bgdl_value, bgdl_seqn) = await _bNetClient.Do <List <BNetLib.Models.Version> >(
                                new BGDLCommand(item.Product.ToLower()));

                            await _bgdl.Insert(new BTSharedCore.Models.BGDL()
                            {
                                Seqn    = bgdl_seqn,
                                Value   = bgdl_value,
                                Product = item.Product,
                            });
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        _queue.Enqueue(item);
                        await Task.Delay(TimeSpan.FromSeconds(1));
                    }
                }
            }, _cancellationToken);
        }