Ejemplo n.º 1
0
        public async Task Run(IAsyncCollector <ChangeMessage> notifyChanges)
        {
            using (var context = new ShipHubContext()) {
                // Get all the tokens
                var tokens = await context.Tokens
                             .AsNoTracking()
                             .Where(x => x.Version < TokenVersion)
                             .ToArrayAsync();

                if (tokens.Any())
                {
                    Log.Info($"{tokens.Length} tokens need to be rolled.");

                    foreach (var token in tokens)
                    {
                        var speedLimit = Task.Delay(1000);
                        try {
                            var newToken = await ResetToken(token.Token);

                            if (newToken == null)
                            {
                                // Delete the single token
                                await context.DeleteUserAccessToken(token.UserId, token.Token);

                                Log.Info("Deleted expired token.");
                            }
                            else
                            {
                                // Replace the token
                                await context.RollUserAccessToken(token.UserId, token.Token, newToken, TokenVersion);

                                Log.Info("Updated valid token.");
                            }

                            var cs = new ChangeSummary();
                            cs.Add(userId: token.UserId);
                            await notifyChanges.AddAsync(new ChangeMessage(cs));
                        } catch (Exception e) {
                            Log.Exception(e, $"Error rolling token for {token.UserId}:{token.Version}");
                        }

                        await speedLimit;
                    }

                    Log.Info($"Done processing tokens.");
                }
            }
        }