Пример #1
0
 public Task StopAsync(CancellationToken cancellationToken)
 {
     KraftLogger.LogInformation("CoreKraft-Background-Service is stopped.");
     foreach (Timer timer in _Timers)
     {
         timer?.Change(Timeout.Infinite, 0);
     }
     return(Task.CompletedTask);
 }
Пример #2
0
 private void DoWork(object state)
 {
     lock (_Lock)
     {
         if (state is List <string> signals)
         {
             foreach (string signal in signals)
             {
                 Stopwatch stopWatch = Stopwatch.StartNew();
                 ExecuteSignals("null", signal);
                 KraftLogger.LogInformation($"Executing signal: {signal} for {stopWatch.ElapsedMilliseconds} ms");
             }
             KraftLogger.LogInformation("Batch of CoreKraft-Background-Services executed.");
         }
     }
 }
Пример #3
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            KraftLogger.LogInformation("CoreKraft-Background-Service is starting.");
            using (IServiceScope scope = _ScopeFactory.CreateScope())
            {
                _KraftGlobalConfigurationSettings = scope.ServiceProvider.GetRequiredService <KraftGlobalConfigurationSettings>();
                _ServiceProvider = scope.ServiceProvider;
                int minutes = _KraftGlobalConfigurationSettings.GeneralSettings.HostingServiceSettings.Interval;
                if (minutes > 0)
                {
                    _Timer = new Timer(DoWork, _ScopeFactory, TimeSpan.Zero, TimeSpan.FromMinutes(minutes));
                }
            }

            return(Task.CompletedTask);
        }
Пример #4
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            KraftLogger.LogInformation("CoreKraft-Background-Service is starting.");
            _ServiceProvider = _ScopeFactory.CreateScope().ServiceProvider;
            _KraftGlobalConfigurationSettings = _ServiceProvider.GetRequiredService <KraftGlobalConfigurationSettings>();

            foreach (HostingServiceSetting item in _KraftGlobalConfigurationSettings.GeneralSettings.HostingServiceSettings)
            {
                int minutes = item.IntervalInMinutes;
                if (minutes > 0)
                {
                    Timer t = new Timer(DoWork, item.Signals, TimeSpan.FromMinutes(minutes), TimeSpan.FromMinutes(minutes));
                    _Timers.Add(t);
                }
            }
            return(Task.CompletedTask);
        }
Пример #5
0
        private void DoWork(object state)
        {
            IServiceScopeFactory scopeFactory = state as IServiceScopeFactory;

            if (scopeFactory != null)
            {
                using (IServiceScope scope = scopeFactory.CreateScope())
                {
                    _KraftGlobalConfigurationSettings = scope.ServiceProvider.GetRequiredService <KraftGlobalConfigurationSettings>();
                    _ServiceProvider = scope.ServiceProvider;
                    foreach (string signal in _KraftGlobalConfigurationSettings.GeneralSettings.HostingServiceSettings.Signals)
                    {
                        Stopwatch stopWatch = Stopwatch.StartNew();
                        ExecuteSignals("null", signal);
                        KraftLogger.LogInformation($"Executing signal: {signal} for {stopWatch.ElapsedMilliseconds} ms");
                    }
                }
            }
            KraftLogger.LogInformation("CoreKraft-Background-Service executed.");
        }