コード例 #1
0
        public async Task RunAsync(HttpApiClient api)
        {
            var result = await Database.GetPlusRecordedUsersAsync();

            if (!result.Success)
            {
                FLogger.LogInBackground("更新 PP+ 数据时访问数据库失败。");
                FLogger.LogException(result.Exception);
                return;
            }

            IEnumerable <int> todo = result.Result;

            Logger.Info($"找到{todo.Count()}个查询过的玩家。");
            int retry = 10;

            do
            {
                const int threads = 10;
                Logger.Debug($"开始查询,线程数为{threads.ToString(CultureInfo.InvariantCulture)}");
                var failed  = new ConcurrentBag <int>();
                var results = new ConcurrentBag <IUserPlus>();

                Parallel.ForEach(todo, new ParallelOptions
                {
                    MaxDegreeOfParallelism = threads,
                }, userId =>
                {
                    try
                    {
                        var user = s_spider.GetUserPlusAsync(userId).ConfigureAwait(false).GetAwaiter().GetResult();
                        if (user != null)
                        {
                            results.Add(user);
                        }
                    }
                    catch (Exception)
                    {
                        failed.Add(userId);
                    }
                });
                Logger.Info($"查询成功{results.Count}条。");
                Logger.Info($"失败{failed.Count}条,首个失败是{failed.FirstOrDefault()}");
                var addResult = await Database.AddPlusHistoryRangeAsync(results);

                if (!addResult.Success)
                {
                    FLogger.LogInBackground("添加新的 PP+ 数据失败。");
                    FLogger.LogException(addResult.Exception);
                }

                todo = failed.ToList();
                await Task.Delay(600_000);

                retry--;
            } while (todo.Any() && retry > 0);
        }