public HostingShutdown( ApplicationShutdown appShutdown, IServiceBeacon serviceBeacon, IServiceLocator serviceLocator, IVostokApplicationIdentity identity, IMetricContext instanceMetrics, ILog log, CancellationToken token, TimeSpan totalTimeout, TimeSpan beaconTimeout, bool beaconWaitEnabled, bool sendAnnotation) { this.appShutdown = appShutdown; this.serviceBeacon = serviceBeacon; this.serviceLocator = serviceLocator; this.identity = identity; this.instanceMetrics = instanceMetrics; this.log = log.ForContext <HostingShutdown>(); this.totalTimeout = totalTimeout; this.beaconTimeout = beaconTimeout; this.beaconWaitEnabled = beaconWaitEnabled; this.sendAnnotation = sendAnnotation; hostShutdownBudget = TimeBudget.CreateNew(totalTimeout); tokenRegistration = token.Register(OnHostShutdownTriggered); }
private async Task <bool> WaitForServiceBeaconRegistrationIfNeededAsync(IServiceBeacon beacon) { if (!RequirementDetector.RequiresPort(settings.Application) || !settings.BeaconRegistrationWaitEnabled || !(beacon is ServiceBeacon convertedBeacon)) { return(true); } return(await convertedBeacon.WaitForInitialRegistrationAsync() .TryWaitAsync(settings.BeaconRegistrationTimeout) .ConfigureAwait(false)); }
internal VostokHostingEnvironment( [NotNull] HostingShutdown hostingShutdown, [NotNull] ApplicationShutdown applicationShutdown, [NotNull] IVostokApplicationIdentity applicationIdentity, [NotNull] IVostokApplicationLimits applicationLimits, [NotNull] Func <IVostokApplicationReplicationInfo> replicationInfoProvider, [NotNull] IVostokApplicationMetrics metrics, [NotNull] IVostokApplicationDiagnostics diagnostics, [NotNull] ILog log, [NotNull] ITracer tracer, [NotNull] IHerculesSink herculesSink, [NotNull] IConfigurationSource configurationSource, [NotNull] IConfigurationSource secretConfigurationSource, [NotNull] IConfigurationProvider configurationProvider, [NotNull] IConfigurationProvider secretConfigurationProvider, [NotNull] IClusterConfigClient clusterConfigClient, [NotNull] IServiceBeacon serviceBeacon, [CanBeNull] int?port, [NotNull] IServiceLocator serviceLocator, [NotNull] IContextGlobals contextGlobals, [NotNull] IContextProperties contextProperties, [NotNull] IContextConfiguration contextConfiguration, [NotNull] IDatacenters datacenters, [NotNull] IVostokHostExtensions hostExtensions, [NotNull] Action onDispose) { this.hostingShutdown = hostingShutdown ?? throw new ArgumentNullException(nameof(hostingShutdown)); this.applicationShutdown = applicationShutdown ?? throw new ArgumentNullException(nameof(applicationShutdown)); this.replicationInfoProvider = replicationInfoProvider ?? throw new ArgumentNullException(nameof(replicationInfoProvider)); this.onDispose = onDispose ?? throw new ArgumentNullException(nameof(onDispose)); ApplicationIdentity = applicationIdentity ?? throw new ArgumentNullException(nameof(applicationIdentity)); ApplicationLimits = applicationLimits ?? throw new ArgumentNullException(nameof(applicationLimits)); Metrics = metrics ?? throw new ArgumentNullException(nameof(metrics)); Diagnostics = diagnostics ?? throw new ArgumentNullException(nameof(diagnostics)); Log = log ?? throw new ArgumentNullException(nameof(log)); Tracer = tracer ?? throw new ArgumentNullException(nameof(tracer)); HerculesSink = herculesSink ?? throw new ArgumentNullException(nameof(herculesSink)); ConfigurationSource = configurationSource ?? throw new ArgumentNullException(nameof(configurationSource)); ConfigurationProvider = configurationProvider ?? throw new ArgumentNullException(nameof(configurationProvider)); SecretConfigurationSource = secretConfigurationSource ?? throw new ArgumentNullException(nameof(secretConfigurationSource)); SecretConfigurationProvider = secretConfigurationProvider ?? throw new ArgumentNullException(nameof(secretConfigurationProvider)); ClusterConfigClient = clusterConfigClient ?? throw new ArgumentNullException(nameof(clusterConfigClient)); ServiceBeacon = serviceBeacon ?? throw new ArgumentNullException(nameof(serviceBeacon)); Port = port; ServiceLocator = serviceLocator ?? throw new ArgumentNullException(nameof(serviceLocator)); ContextGlobals = contextGlobals ?? throw new ArgumentNullException(nameof(contextGlobals)); ContextProperties = contextProperties ?? throw new ArgumentNullException(nameof(contextProperties)); ContextConfiguration = contextConfiguration ?? throw new ArgumentNullException(nameof(contextConfiguration)); Datacenters = datacenters ?? throw new ArgumentNullException(nameof(datacenters)); HostExtensions = hostExtensions ?? throw new ArgumentNullException(nameof(hostExtensions)); }
public static (HostingShutdown hosting, ApplicationShutdown application) Create( IServiceBeacon serviceBeacon, IServiceLocator serviceLocator, IVostokApplicationIdentity identity, IMetricContext instanceMetrics, ILog log, int?port, IReadOnlyList <CancellationToken> tokens, TimeSpan totalTimeout, TimeSpan beaconTimeout, bool beaconWaitEnabled, bool sendAnnotation) { var hasRealBeacon = serviceBeacon is ServiceBeacon; var hasRealLocator = serviceLocator is ServiceLocator; // (iloktionov): No point in waiting for beacon deregistration for apps without external port or when SD is disabled. beaconWaitEnabled &= port.HasValue && hasRealBeacon && hasRealLocator; // (iloktionov): No point in reducing app shutdown timeout right from the start when SD is disabled. beaconTimeout = hasRealBeacon ? TimeSpanArithmetics.Min(beaconTimeout, totalTimeout.Divide(3)) : TimeSpan.Zero; // (iloktionov): Artificially reduce initial app shutdown timeout by beacon shutdown timeout so that it's value doesn't drop abruptly on shutdown. var applicationShutdown = new ApplicationShutdown(log, totalTimeout - beaconTimeout); var hostingToken = tokens.Any() ? CancellationTokenSource.CreateLinkedTokenSource(tokens.ToArray()).Token : default; var hostingShutdown = new HostingShutdown( applicationShutdown, serviceBeacon, serviceLocator, identity, instanceMetrics, log, hostingToken, totalTimeout, beaconTimeout, beaconWaitEnabled, sendAnnotation); return(hostingShutdown, applicationShutdown); }
public static IEnumerable <Substitution> Provide( IVostokApplicationIdentity identity, IClusterConfigClient clusterConfig, IServiceBeacon beacon, IDatacenters datacenters) { if (identity != null) { yield return(new Substitution(VostokConfigurationPlaceholders.IdentityProject, () => identity.Project)); yield return(new Substitution(VostokConfigurationPlaceholders.IdentitySubproject, () => identity.Subproject)); yield return(new Substitution(VostokConfigurationPlaceholders.IdentityEnvironment, () => identity.Environment)); yield return(new Substitution(VostokConfigurationPlaceholders.IdentityApplication, () => identity.Application)); yield return(new Substitution(VostokConfigurationPlaceholders.IdentityInstance, () => identity.Instance)); } if (beacon != null) { yield return(new Substitution(VostokConfigurationPlaceholders.ServiceDiscoveryEnvironment, () => beacon.ReplicaInfo.Environment)); yield return(new Substitution(VostokConfigurationPlaceholders.ServiceDiscoveryApplication, () => beacon.ReplicaInfo.Application)); } if (datacenters != null) { yield return(new Substitution(VostokConfigurationPlaceholders.LocalDatacenter, datacenters.GetLocalDatacenter)); } if (clusterConfig != null) { yield return(new Substitution(VostokConfigurationPlaceholders.ClusterConfigZone, () => (clusterConfig as ClusterConfigClient)?.Zone ?? "default")); } }