public void ItShouldBeResolvableByInterfaceAndItsWrappedType()
        {
            var configurationInstanceHolder = new ConfigurationInstanceHolder();

            var configuration = new MyConfiguration(123);
            var namedInstance = new NamedInstance <MyConfiguration>(configuration, "myInstance");

            configurationInstanceHolder.Add(namedInstance);

            ServiceProvider serviceProvider = new ServiceCollection()
                                              .AddConfigurationInstanceHolder(configurationInstanceHolder)
                                              .BuildServiceProvider();

            var myConfiguration = serviceProvider.GetService <MyConfiguration>();

            Assert.NotNull(myConfiguration);
            Assert.Equal(123, myConfiguration.Id);

            var namedFromProviderInstance = serviceProvider.GetService <INamedInstance <MyConfiguration> >();

            Assert.NotNull(namedFromProviderInstance);
            Assert.Equal(123, namedFromProviderInstance.Value.Id);
            Assert.Equal("myInstance", namedFromProviderInstance.Name);
            Assert.Equal(namedInstance, namedFromProviderInstance);
        }
Esempio n. 2
0
        public static void ConfigureEnvironment(ConfigurationInstanceHolder configurationInstanceHolder)
        {
            if (configurationInstanceHolder is null)
            {
                throw new ArgumentNullException(nameof(configurationInstanceHolder));
            }

            var configureEnvironments    = configurationInstanceHolder.CreateInstances <IConfigureEnvironment>();
            var environmentConfiguration = configurationInstanceHolder.Get <EnvironmentConfiguration>();

            if (environmentConfiguration is null)
            {
                var newConfiguration = new EnvironmentConfiguration();
                environmentConfiguration = newConfiguration;

                configurationInstanceHolder.Add(
                    new NamedInstance <EnvironmentConfiguration>(newConfiguration, "default"));
            }

            var ordered = configureEnvironments
                          .Select(environmentConfigurator => (EnvironmentConfigurator: environmentConfigurator,
                                                              Order: environmentConfigurator.GetRegistrationOrder(0))).OrderBy(pair => pair.Order)
                          .Select(pair => pair.EnvironmentConfigurator).ToArray();

            foreach (var configureEnvironment in ordered)
            {
                configureEnvironment.Configure(environmentConfiguration);
            }
        }
Esempio n. 3
0
        public void WhenRegisteringMultipleInstances()
        {
            var holder = new ConfigurationInstanceHolder();

            holder.Add(new NamedInstance <ValidatableOptional>(new ValidatableOptional("abc", 123), "abc-instance"));
            holder.Add(new NamedInstance <ValidatableOptional>(new ValidatableOptional("def", 234), "def-instance"));

            ImmutableDictionary <string, ValidatableOptional?> instances = holder.GetInstances <ValidatableOptional>();

            Assert.Equal(2, instances.Count);

            Assert.Contains("abc-instance", instances.Keys);
            Assert.Equal("abc", instances["abc-instance"].Name);
            Assert.Equal(123, instances["abc-instance"].Value);

            Assert.Contains("def-instance", instances.Keys);
            Assert.Equal("def", instances["def-instance"].Name);
            Assert.Equal(234, instances["def-instance"].Value);
        }
Esempio n. 4
0
        public void WhenRegisteringSingleInstanceTryGet()
        {
            var holder   = new ConfigurationInstanceHolder();
            var instance = new ValidatableOptional("abc", 123);

            holder.Add(new NamedInstance <ValidatableOptional>(instance, "abc-instance"));

            bool found = holder.TryGet("abc-instance", out ValidatableOptional? foundInstance);

            Assert.True(found);
            Assert.Same(instance, foundInstance);
        }
Esempio n. 5
0
        public Task Handle(WorkerCreated notification, CancellationToken cancellationToken)
        {
            if (!_configurationInstanceHolder.TryGet(
                    notification.Worker.TargetId.TargetId,
                    out DeploymentTargetWorker _))
            {
                _configurationInstanceHolder.Add(
                    new NamedInstance <IDeploymentTargetWorker>(notification.Worker, notification.Worker.TargetId.TargetId));
            }

            StartWorker(notification.Worker, cancellationToken);

            return(Task.CompletedTask);
        }
Esempio n. 6
0
        public void WhenRegisteringSingleInstance()
        {
            var holder = new ConfigurationInstanceHolder();

            holder.Add(new NamedInstance <ValidatableOptional>(new ValidatableOptional("abc", 123), "abc-instance"));

            ImmutableDictionary <string, ValidatableOptional?> instances = holder.GetInstances <ValidatableOptional>();

            Assert.Single(instances);

            Assert.Equal("abc-instance", instances.Keys.Single());
            Assert.Equal("abc", instances["abc-instance"].Name);
            Assert.Equal(123, instances["abc-instance"].Value);
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await Task.Yield();

            IReadOnlyCollection <DeploymentTargetId> targetIds;

            try
            {
                if (!int.TryParse(
                        _configuration[DeployerAppConstants.StartupTargetsTimeoutInSeconds],
                        out int startupTimeoutInSeconds) ||
                    startupTimeoutInSeconds <= 0)
                {
                    startupTimeoutInSeconds = 30;
                }

                using CancellationTokenSource startupToken =
                          _timeoutHelper.CreateCancellationTokenSource(TimeSpan.FromSeconds(startupTimeoutInSeconds));
                using var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(
                          stoppingToken,
                          startupToken.Token);
                targetIds =
                    (await _deploymentTargetReadService.GetDeploymentTargetsAsync(stoppingToken: linkedToken.Token))
                    .Select(deploymentTarget => deploymentTarget.Id)
                    .ToArray();

                _logger.Debug("Found deployment target IDs {IDs}", targetIds);
            }
            catch (Exception ex) when(!ex.IsFatal())
            {
                _logger.Warning(ex, "Could not get target ids");
                IsCompleted = true;
                return;
            }

            foreach (var targetId in targetIds)
            {
                var deploymentTargetWorker = new DeploymentTargetWorker(targetId, _logger, _mediator,
                                                                        _workerConfiguration, _timeoutHelper, _clock, _serviceProvider);

                _holder.Add(new NamedInstance <DeploymentTargetWorker>(
                                deploymentTargetWorker,
                                targetId.TargetId));

                await _mediator.Send(new StartWorker(deploymentTargetWorker), stoppingToken);
            }

            IsCompleted = true;
        }
Esempio n. 8
0
        public async Task Handle(TargetCreated notification, CancellationToken cancellationToken)
        {
            var worker = new DeploymentTargetWorker(notification.TargetId,
                                                    _logger,
                                                    _mediator,
                                                    _workerConfiguration,
                                                    _timeoutHelper,
                                                    _clock,
                                                    _serviceProvider);

            // TODO remove old worker

            _configurationInstanceHolder.Add(new NamedInstance <DeploymentTargetWorker>(worker, notification.TargetId.TargetId));

            await _mediator.Publish(new WorkerCreated(worker), cancellationToken);
        }
Esempio n. 9
0
        public void WhenRemovingNonExistingType()
        {
            var holder = new ConfigurationInstanceHolder();

            holder.Add(new NamedInstance <ValidatableOptional>(new ValidatableOptional("abc", 123), "abc-instance"));

            bool found = holder.TryGet("abc-instance", out ValidatableOptional? instance);

            Assert.True(found);

            Assert.NotNull(instance);

            bool isRemoved = holder.TryRemove("abc-instance", typeof(string), out object?removed);

            Assert.False(isRemoved);

            Assert.Null(removed);
        }