Exemplo n.º 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            // Контроллеры
            services.AddControllers();

            // Документация
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title       = "Менеджер метрик",
                    Description = "Позволяет отслеживать и анализировать параметры системы",
                    Contact     = new OpenApiContact
                    {
                        Name = "Заярная Анастасия"
                    },
                    Version = "v1"
                });
            });

            // БД
            services.AddSingleton <IDataConnector, SQLLite>();
            // миграции
            var dbConnector = new SQLLite();

            services.AddFluentMigratorCore()
            .ConfigureRunner(
                rb => rb.AddSQLite()
                .WithGlobalConnectionString(dbConnector.GetStringConnection())
                .ScanIn(typeof(Startup).Assembly).For.Migrations()
                ).AddLogging(logger => logger.AddFluentMigratorConsole());

            // Мапперы
            var mapperConfiguration = new MapperConfiguration(mp => mp.AddProfile(new MapperProfile()));
            var mapper = mapperConfiguration.CreateMapper();

            services.AddSingleton(mapper);

            // Репозитории
            services.AddSingleton <ICPUMetricsRepository, CPUMetricsRepository>();
            services.AddSingleton <IDotNetMetricsRepository, DotNetMetricsRepository>();
            services.AddSingleton <IHddMetricsRepository, HddMetricsRepository>();
            services.AddSingleton <INetworkMetricsRepository, NetworkMetricsRepository>();
            services.AddSingleton <IRamMetricsRepository, RamMetricsRepository>();

            // Сервисы по рассписанию
            services.AddSingleton <IJobFactory, SingletonJobFactory>();
            services.AddSingleton <ISchedulerFactory, StdSchedulerFactory>();
            services.AddSingleton <CpuMetricJob>();
            services.AddSingleton <DotNetMetricJob>();
            services.AddSingleton <HddMetricJob>();
            services.AddSingleton <NetworkMetricJob>();
            services.AddSingleton <RamMetricJob>();
            services.AddSingleton(new JobSchedule(jobType: typeof(CpuMetricJob), cronExpression: "0/5 * * * * ?"));
            services.AddSingleton(new JobSchedule(jobType: typeof(DotNetMetricJob), cronExpression: "0/5 * * * * ?"));
            services.AddSingleton(new JobSchedule(jobType: typeof(HddMetricJob), cronExpression: "0/5 * * * * ?"));
            services.AddSingleton(new JobSchedule(jobType: typeof(NetworkMetricJob), cronExpression: "0/5 * * * * ?"));
            services.AddSingleton(new JobSchedule(jobType: typeof(RamMetricJob), cronExpression: "0/5 * * * * ?"));
            services.AddHostedService <QuartzHostedService>();
        }
Exemplo n.º 2
0
        public void ConfigureServices(IServiceCollection services)
        {
            // Контроллеры
            services.AddControllers();

            // Документация
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title       = "Менеджер агентов метрик",
                    Description = "Позволяет управлять агентами",
                    Contact     = new OpenApiContact
                    {
                        Name = "Заярная Анастасия"
                    },
                    Version = "v1"
                });
            });

            // БД
            services.AddSingleton <IDataConnector, SQLLite>();
            // миграции
            var dbConnector = new SQLLite();

            services.AddFluentMigratorCore()
            .ConfigureRunner(
                rb => rb.AddSQLite()
                .WithGlobalConnectionString(dbConnector.GetStringConnection())
                .ScanIn(typeof(Startup).Assembly).For.Migrations()
                ).AddLogging(logger => logger.AddFluentMigratorConsole());

            // Мапперы
            var mapperConfiguration = new MapperConfiguration(mp => mp.AddProfile(new MapperProfile()));
            var mapper = mapperConfiguration.CreateMapper();

            services.AddSingleton(mapper);

            // Репозитории
            services.AddSingleton <ICPUMetricsRepository, CPUMetricsRepository>();
            services.AddSingleton <IDotNetMetricsRepository, DotNetMetricsRepository>();
            services.AddSingleton <IHddMetricsRepository, HddMetricsRepository>();
            services.AddSingleton <INetworkMetricsRepository, NetworkMetricsRepository>();
            services.AddSingleton <IRamMetricsRepository, RamMetricsRepository>();
            services.AddSingleton <IAgentsRepository, AgentsRepository>();

            // Http
            services.AddHttpClient <IMetricsClient, MetricsClient>()
            // Повторная попытка запроса, в случае не удачи
            .AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(1000)));

            // Сервисы по расписанию
            services.AddSingleton <IJobFactory, SingletonJobFactory>();
            services.AddSingleton <ISchedulerFactory, StdSchedulerFactory>();
            services.AddSingleton <CpuMetricJob>();
            services.AddSingleton <DotNetMetricJob>();
            services.AddSingleton <HddMetricJob>();
            services.AddSingleton <NetworkMetricJob>();
            services.AddSingleton <RamMetricJob>();
            services.AddSingleton(new JobSchedule(jobType: typeof(CpuMetricJob), cronExpression: "0/5 * * * * ?"));
            services.AddSingleton(new JobSchedule(jobType: typeof(DotNetMetricJob), cronExpression: "0/5 * * * * ?"));
            services.AddSingleton(new JobSchedule(jobType: typeof(HddMetricJob), cronExpression: "0/5 * * * * ?"));
            services.AddSingleton(new JobSchedule(jobType: typeof(NetworkMetricJob), cronExpression: "0/5 * * * * ?"));
            services.AddSingleton(new JobSchedule(jobType: typeof(RamMetricJob), cronExpression: "0/5 * * * * ?"));
            services.AddHostedService <QuartzHostedService>();
        }