public async Task <ActionResult> CreateWorker(PoolWorkerCreateModel model) { if (!ModelState.IsValid) { var connections = await PoolReader.GetPoolConnections(); model.AlgoTypes = connections.Select(x => x.AlgoType).Distinct().ToList(); model.Connections = connections; return(View("CreateWorkerModal", model)); } model.FullName = string.Format("{0}.{1}", User.Identity.Name, model.Name); model.TargetDifficulty = PoolExtensions.OptionToTargetDifficulty(model.DifficultyOption, model.TargetDifficulty); var result = await PoolWorkerWriter.CreateWorker(User.Identity.GetUserId(), model); if (!ModelState.IsWriterResultValid(result)) { return(View("CreateWorkerModal", model)); } return(CloseModal(result)); }
public async Task <IWriterResult> CreateWorker(string userId, PoolWorkerCreateModel model) { try { var currentUserId = new Guid(userId); using (var context = PoolDataContextFactory.CreateContext()) { var algoConnection = await context.Connection.FirstOrDefaultNoLockAsync(x => x.AlgoType == model.AlgoType).ConfigureAwait(false); if (algoConnection == null) { return(new WriterResult(false, "Invalid algorithm.")); } var worker = await context.Worker .Where(w => w.UserId == currentUserId && w.Name == model.FullName && w.AlgoType == model.AlgoType) .FirstOrDefaultNoLockAsync().ConfigureAwait(false); if (worker != null && worker.IsEnabled) { return(new WriterResult(false, "Worker '{0}' already exists", model.Name)); } if (worker == null) { worker = new Entity.PoolWorker { Name = model.FullName, UserId = currentUserId, AlgoType = model.AlgoType.Value, }; context.Worker.Add(worker); } worker.TargetPool = algoConnection.DefaultPool; if (model.IsAutoSwitch) { worker.TargetPool = await context.Statistics .Where( x => x.Pool.IsEnabled && x.Pool.AlgoType == model.AlgoType && (x.Pool.Status == Enums.PoolStatus.OK || x.Pool.Status == Enums.PoolStatus.Expiring)) .OrderByDescending(x => x.Profitability) .Select(x => x.Pool.Symbol) .FirstOrDefaultNoLockAsync().ConfigureAwait(false); } worker.Password = model.Password; worker.IsAutoSwitch = model.IsAutoSwitch; worker.TargetDifficulty = model.TargetDifficulty; worker.IsEnabled = true; await context.SaveChangesAsync().ConfigureAwait(false); await CacheService.InvalidateAsync(CacheKey.PoolWorkers(userId)).ConfigureAwait(false); return(new WriterResult(true, $"Successfully created worker '{worker.Name}'")); } } catch (Exception) { return(new WriterResult(false)); } }