コード例 #1
0
        private async Task UpdateWorkerAsync(int index)
        {
            for (;;)
            {
                DateTime lastStart = DateTime.Now;

                IEnumerable <UserState <UserDetails> > users =
                    UserState <UserDetails> .EnumerateAll(x => (x % _numRssUpdateWorkers) == index);

                foreach (var user in users)
                {
                    try
                    {
                        if (!user.Data.AuthValid)
                        {
                            logger.Info($"User {user.Data.UserId} is skipped - auth is not valid");
                            continue;
                        }

                        logger.Info($"Fetching feeds for user {user.Data.UserId}");
                        await RunFetcherAsync(user.Data);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, "failed to run user data");
                    }
                }

                var nextStart = lastStart + _refreshInterval;
                var now       = DateTime.Now;
                if (nextStart < now)
                {
                    logger.Error($"Bot seems overloaded: last update cycle took over {_refreshInterval}");
                    continue;
                }

                var toSleepMillis = (int)(nextStart - now).TotalMilliseconds;
                logger.Info($"Worker {index}: sleeping for {toSleepMillis}ms");
                await Task.Delay(toSleepMillis);
            }
        }