コード例 #1
0
        public async Task Handle(AccountCreatedEvent notification, CancellationToken cancellationToken)
        {
            try
            {
                var desiredState = notification.Account.Machines.First().States.First(x => x.Desired);
                _context.Set <HistoricalDesiredState>().Add(Mapper.Map <HistoricalDesiredState>(desiredState));

                await _context.SaveChangesAsync(cancellationToken);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
            }
        }
コード例 #2
0
        public async Task <Unit> Handle(ForcePopulateForMachineCommand command, CancellationToken cancellationToken)
        {
            var machine = await _context.Set <Machine>()
                          .Include(x => x.CloudInstances)
                          .FirstOrDefaultAsync(x => x.Id == command.Id, cancellationToken);

            if (machine == null)
            {
                throw new EntityNotFoundException(nameof(Machine), command.Id);
            }

            if (!machine.CloudInstances.Any())
            {
                throw new CommandException("Machine has no instance");
            }

            foreach (var instance in machine.CloudInstances)
            {
                if (command.PopulateLauncher)
                {
                    instance.LauncherPopulated = false;
                }

                if (command.PopulateSiteMaster)
                {
                    instance.SiteMasterPopulated = false;
                }

                instance.AltString = command.AltString;
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #3
0
        public async Task <Unit> Handle(DeleteSiteCommand request, CancellationToken cancellationToken)
        {
            var account = await _context.Set <Account>()
                          .FirstOrDefaultAsync(x => !x.IsDeleted && x.UrlFriendlyName == request.AccountUrlFriendlyName, cancellationToken);

            if (account == null)
            {
                throw new EntityNotFoundException(nameof(Account), request.AccountUrlFriendlyName);
            }

            var site = await _context.Set <Site>()
                       .Include(x => x.Machine)
                       .FirstOrDefaultAsync(x => x.UrlFriendlyName == request.UrlFriendlyName, cancellationToken);

            if (site == null)
            {
                throw new EntityNotFoundException(nameof(Site), request.UrlFriendlyName);
            }

            var machine = site.Machine;

            machine.Terminate = true;

            _context.Set <Site>().Remove(site);

            await _context.SaveChangesAsync(cancellationToken);

            return(await Task.FromResult(Unit.Value));
        }
        public async Task <long> Handle(CreateOrUpdateInstanceSettingsTemplateCommand request, CancellationToken cancellationToken)
        {
            MachineConfig instanceSettingsTemplate;

            if (request.Id > 0)
            {
                instanceSettingsTemplate = await _context.Set <MachineConfig>()
                                           .FirstOrDefaultAsync(x => x.IsTemplate && x.Id == request.Id, cancellationToken);

                if (instanceSettingsTemplate == null)
                {
                    throw new EntityNotFoundException(nameof(MachineConfig), request.Id);
                }
            }
            else
            {
                instanceSettingsTemplate = new MachineConfig()
                {
                    IsTemplate = true
                };
                _context.Set <MachineConfig>().Add(instanceSettingsTemplate);
            }

            Mapper.Map(request, instanceSettingsTemplate);

            await _context.SaveChangesAsync(cancellationToken);

            return(instanceSettingsTemplate.Id);
        }
コード例 #5
0
        public async Task <long> Handle(CreateOrUpdateLicenseTemplateCommand request, CancellationToken cancellationToken)
        {
            LicenseConfig licenseTemplate;

            if (request.Id > 0)
            {
                licenseTemplate = await _context.Set <LicenseConfig>()
                                  .FirstOrDefaultAsync(x => x.IsTemplate && x.Id == request.Id, cancellationToken);

                if (licenseTemplate == null)
                {
                    throw new EntityNotFoundException(nameof(LicenseConfig), request.Id);
                }
            }
            else
            {
                licenseTemplate = new LicenseConfig()
                {
                    IsTemplate = true
                };
                _context.Set <LicenseConfig>().Add(licenseTemplate);
            }

            Mapper.Map(request, licenseTemplate);
            await _context.SaveChangesAsync(cancellationToken);

            return(licenseTemplate.Id);
        }
コード例 #6
0
        public async Task <long> Handle(CreateOrUpdateGeneralTemplateCommand command, CancellationToken cancellationToken)
        {
            var transaction = _context.GetTransaction();

            try
            {
                Account accountTemplate;
                if (command.Id > 0)
                {
                    accountTemplate = await UpdateTemplate(command, cancellationToken);
                }
                else
                {
                    accountTemplate = await CreateTemplate(command, cancellationToken);
                }

                await _context.SaveChangesAsync(cancellationToken);

                transaction.Commit();
                return(accountTemplate.Id);
            }
            catch (Exception e)
            {
                transaction.Rollback();
                throw;
            }
        }
コード例 #7
0
 private async Task AddUserOperation(string type, long machineId, string user, CancellationToken cancellationToken)
 {
     _context.Set <UserOperation>().Add(new UserOperation
     {
         TypeName  = type,
         MachineId = machineId,
         Timestamp = DateTimeOffset.UtcNow,
         User      = user,
     });
     await _context.SaveChangesAsync(cancellationToken);
 }
コード例 #8
0
        public async Task <Unit> Handle(DeleteLibraryFileCommand command, CancellationToken cancellationToken)
        {
            var file = await _context.Set <File>().FirstOrDefaultAsync(x => x.Id == command.Id, cancellationToken);

            if (file == null)
            {
                throw new EntityNotFoundException(nameof(File), command.Id);
            }

            _context.Set <File>().Remove(file);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #9
0
        public async Task <Unit> Handle(ChangeInstanceTypeForMachineCommand command, CancellationToken cancellationToken)
        {
            var machine = await _context.Set <Machine>().FirstOrDefaultAsync(x => x.Id == command.Id, cancellationToken);

            if (machine == null)
            {
                throw new EntityNotFoundException(nameof(Machine), command.Id);
            }

            machine.CloudInstanceTypeId = command.InstanceTypeId;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #10
0
        public async Task <Unit> Handle(ResetMachineFailureStateCommand request, CancellationToken cancellationToken)
        {
            var machine = _context.Set <Machine>().Find(request.Id);

            if (machine == null)
            {
                throw new EntityNotFoundException(nameof(Machine), request.Id);
            }

            machine.NeedsAdmin  = false;
            machine.FailCounter = 0;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #11
0
        public async Task <Unit> Handle(QueueOperationsCommand command, CancellationToken cancellationToken)
        {
            var machine = await _context.Set <Machine>()
                          .FirstOrDefaultAsync(x => x.Id == command.Id, cancellationToken);

            if (machine == null)
            {
                throw new EntityNotFoundException(nameof(Machine), command.Id);
            }

            var runningOperation = await _context.Set <Operation>().FirstOrDefaultAsync(x => x.MachineId == machine.Id && (x.Active || x.Status.ToLower() == "running"), cancellationToken);

            if (runningOperation != null)
            {
                throw new CommandException();
            }

            var operationTypeName = command.Operations.FirstOrDefault();
            var operationType     = await _context.Set <OperationType>()
                                    .FirstOrDefaultAsync(x => x.Name == operationTypeName, cancellationToken);

            if (operationType == null || !(operationType.CanBeManual.HasValue && operationType.CanBeManual.Value))
            {
                throw new CommandException();
            }

            var forcedOperation = new Operation()
            {
                Type      = operationType,
                Active    = true,
                Timestamp = DateTimeOffset.Now,
                Status    = "FORCED",
                MachineId = command.Id,
                TypeName  = operationTypeName
            };

            _context.Set <Operation>().Add(forcedOperation);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #12
0
        public async Task <Unit> Handle(SendMessageCommand command, CancellationToken cancellationToken)
        {
            var machineIds = command.Machines ?? new List <long>();

            if (command.Accounts != null && command.Accounts.Any())
            {
                var machines = await _context.Set <Machine>()
                               .Where(x => x.AccountId.HasValue && command.Accounts.Contains(x.AccountId.Value))
                               .ToListAsync();

                machineIds = machineIds.Union(machines.Select(x => x.Id)).Distinct();
            }

            if (command.AccountUrlNames != null && command.AccountUrlNames.Any())
            {
                var machines = await _context.Set <Machine>()
                               .Include(x => x.Account)
                               .Where(x => x.AccountId.HasValue && command.AccountUrlNames.Contains(x.Account.UrlFriendlyName))
                               .ToListAsync();

                machineIds = machineIds.Union(machines.Select(x => x.Id)).Distinct();
            }

            var messages = machineIds.Select(x => new Message()
            {
                MachineId    = x,
                Title        = command.Title,
                Body         = command.Body,
                ExpiresAfter = 10,
                Timestamp    = DateTimeOffset.Now
            });

            foreach (var message in messages)
            {
                _context.Set <Message>().Add(message);
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #13
0
        public async Task <Unit> Handle(DeactivateAccountCommand command, CancellationToken cancellationToken)
        {
            var account = await _context.Set <Account>()
                          .Include(x => x.Machines)
                          .FirstOrDefaultAsync(x => !x.IsDeleted && !x.IsTemplate && x.Id == command.Id, cancellationToken);

            if (account == null)
            {
                throw new EntityNotFoundException(nameof(Account), command.Id);
            }

            account.IsActive = false;
            foreach (var machine in account.Machines)
            {
                machine.Stop = true;
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #14
0
        public async Task <Unit> Handle(ResetLastOperationCommand request, CancellationToken cancellationToken)
        {
            var machine = await _context.Set <Machine>().FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (machine == null)
            {
                throw new EntityNotFoundException(nameof(Machine), request.Id);
            }

            var activeOperation = await _context.Set <Operation>().OrderByDescending(x => x.Timestamp)
                                  .FirstOrDefaultAsync(x => x.MachineId == request.Id && x.Active, cancellationToken);

            if (activeOperation == null)
            {
                throw new CommandException($"MachineDto {request.Id} has no active operation");
            }

            _context.Set <Operation>().Remove(activeOperation);

            await _context.SaveChangesAsync(cancellationToken);

            return(await Task.FromResult(Unit.Value));
        }
コード例 #15
0
        public async Task <Unit> Handle(SendCreationMailCommand request, CancellationToken cancellationToken)
        {
            var account = await _context.Set <Account>()
                          .Include(x => x.Machines)
                          .FirstOrDefaultAsync(x => x.Id == request.AccountId, cancellationToken);

            if (account == null)
            {
                throw new EntityNotFoundException(nameof(Account), request.AccountId);
            }

            var launcherMachine = account.Machines.FirstOrDefault(x => x.IsLauncher);

            if (launcherMachine == null)
            {
                throw new CommandException("No launcher machineDto can be found");
            }

            launcherMachine.CreationMailSent = false;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }