|
| 1 | +/* |
| 2 | + * Copyright 2022 MONAI Consortium |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using System.IO.Abstractions; |
| 18 | +using Ardalis.GuardClauses; |
| 19 | +using Microsoft.Extensions.DependencyInjection; |
| 20 | +using Monai.Deploy.Storage.API; |
| 21 | +using Monai.Deploy.Storage.Configuration; |
| 22 | + |
| 23 | +namespace Monai.Deploy.Storage |
| 24 | +{ |
| 25 | + public static class IHealthChecksBuilderExtensions |
| 26 | + { |
| 27 | + /// <summary> |
| 28 | + /// Configures health check for the MONAI Deploy Storage Service. |
| 29 | + /// </summary> |
| 30 | + /// <param name="builder">Instance of <see cref="IHealthChecksBuilder"/>.</param> |
| 31 | + /// <param name="serviceProvider">Instance of <see cref="IServiceProvider"/>.</param> |
| 32 | + /// <param name="fullyQualifiedTypeName">Fully qualified type name of the service to use.</param> |
| 33 | + /// <returns>Instance of <see cref="IHealthChecksBuilder"/>.</returns> |
| 34 | + /// <exception cref="ConfigurationException"></exception> |
| 35 | + public static IHealthChecksBuilder AddMOnaiDeployStorageHealthCheck(this IHealthChecksBuilder builder, IServiceProvider serviceProvider, string fullyQualifiedTypeName) |
| 36 | + => AddMOnaiDeployStorageHealthCheck(builder, serviceProvider, fullyQualifiedTypeName, new FileSystem()); |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Configures health check for the MONAI Deploy Storage Service. |
| 40 | + /// </summary> |
| 41 | + /// <param name="builder">Instance of <see cref="IHealthChecksBuilder"/>.</param> |
| 42 | + /// <param name="serviceProvider">Instance of <see cref="IServiceProvider"/>.</param> |
| 43 | + /// <param name="fullyQualifiedTypeName">Fully qualified type name of the service to use.</param> |
| 44 | + /// <param name="fileSystem">Instance of <see cref="IFileSystem"/>.</param> |
| 45 | + /// <returns>Instance of <see cref="IHealthChecksBuilder"/>.</returns> |
| 46 | + /// <exception cref="ConfigurationException"></exception> |
| 47 | + public static IHealthChecksBuilder AddMOnaiDeployStorageHealthCheck(this IHealthChecksBuilder builder, IServiceProvider serviceProvider, string fullyQualifiedTypeName, IFileSystem fileSystem) |
| 48 | + { |
| 49 | + Guard.Against.NullOrWhiteSpace(fullyQualifiedTypeName, nameof(fullyQualifiedTypeName)); |
| 50 | + Guard.Against.Null(fileSystem, nameof(fileSystem)); |
| 51 | + |
| 52 | + ResolveEventHandler resolveEventHandler = (sender, args) => |
| 53 | + { |
| 54 | + return IServiceCollectionExtensions.CurrentDomain_AssemblyResolve(args, fileSystem); |
| 55 | + }; |
| 56 | + |
| 57 | + AppDomain.CurrentDomain.AssemblyResolve += resolveEventHandler; |
| 58 | + |
| 59 | + var storageServiceAssembly = IServiceCollectionExtensions.LoadAssemblyFromDisk(IServiceCollectionExtensions.GetAssemblyName(fullyQualifiedTypeName), fileSystem); |
| 60 | + var serviceRegistrationType = storageServiceAssembly.GetTypes().FirstOrDefault(p => p.IsSubclassOf(typeof(HealthCheckRegistrationBase))); |
| 61 | + |
| 62 | + if (serviceRegistrationType is null || Activator.CreateInstance(serviceRegistrationType, fullyQualifiedTypeName) is not HealthCheckRegistrationBase healthCheckBuilder) |
| 63 | + { |
| 64 | + throw new ConfigurationException($"Service registrar cannot be found for the configured plug-in '{fullyQualifiedTypeName}'."); |
| 65 | + } |
| 66 | + |
| 67 | + if (!IServiceCollectionExtensions.IsSupportedType(fullyQualifiedTypeName, storageServiceAssembly)) |
| 68 | + { |
| 69 | + throw new ConfigurationException($"The configured type '{fullyQualifiedTypeName}' does not implement the {typeof(IStorageService).Name} interface."); |
| 70 | + } |
| 71 | + |
| 72 | + healthCheckBuilder.Configure(builder, serviceProvider); |
| 73 | + |
| 74 | + AppDomain.CurrentDomain.AssemblyResolve -= resolveEventHandler; |
| 75 | + return builder; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments