Example #1
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var nodes = _seedsLoader.Load();

            if (nodes is null || nodes.Count < 1)
            {
                return;
            }
            for (int i = 0; i < nodes.Count; i++)
            {
                nodes[i].Id = i + 1;
            }
#if DEBUG
            Stopwatch sw = Stopwatch.StartNew();
#endif
            foreach (var node in nodes)
            {
                var nodeCache = await _caller.ExecuteAsync(node);

                _cache.TryAdd(node.Id, nodeCache);
            }
#if DEBUG
            sw.Stop();
            _logger.LogInformation("Use time: {0}", sw.Elapsed.ToString());
#endif
            long groupId  = CreateGroupId();
            var  entities = new List <MatrixItemEntity>(_cache.Count * 32);
            foreach (var cache in _cache.Values)
            {
                var node = cache.Node;
                foreach (var methodItem in cache.MethodsResult)
                {
                    entities.Add(new MatrixItemEntity()
                    {
                        Net       = node.Net,
                        Url       = node.Url,
                        Method    = methodItem.Key,
                        Available = (byte)methodItem.Value.Result,
                        GroupId   = groupId,
                        Error     = methodItem.Value.ToFullErrorMsg()
                    });
                }
            }
            await _dbContext.MatrixItems.AddRangeAsync(entities, cancellationToken);

            try
            {
                await _dbContext.SaveChangesAsync(cancellationToken);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
            }
            long deletedMaxId = await CheckDeleteOldestGroupAsync(groupId, cancellationToken);

            if (deletedMaxId >= 1000_000_000)
            {
                await _dbContext.Database.ExecuteSqlRawAsync($"ALTER TABLE `{nameof(_dbContext.MatrixItems)}` AUTO_INCREMENT = 1;", cancellationToken);
            }
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new MatrixIdentityUser {
                    UserName = Input.Email, Email = Input.Email, CommanderName = Input.CommanderName
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    // Always register new commanders with the default group
                    var defaultGroup = _groupRepo.GetDefaultGroup();
                    _dbContext.CommanderGroups.Add(new CommanderGroup {
                        CommanderName = Input.CommanderName, Group = defaultGroup
                    });
                    await _dbContext.SaveChangesAsync();

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }