<commit_before>using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MR.AspNetCore.Jobs.Models;
using MR.AspNetCore.Jobs.Server;

namespace MR.AspNetCore.Jobs
{
	public abstract class BootstrapperBase : IBootstrapper
	{
		private IApplicationLifetime _appLifetime;
		private CancellationTokenSource _cts;
		private CancellationTokenRegistration _ctsRegistration;
		private Task _bootstrappingTask;

		public BootstrapperBase(
			JobsOptions options,
			IStorage storage,
			IProcessingServer server,
			IApplicationLifetime appLifetime,
			IServiceProvider provider)
		{
			Options = options;
			Storage = storage;
			Server = server;
			_appLifetime = appLifetime;
			Provider = provider;

			_cts = new CancellationTokenSource();
			_ctsRegistration = appLifetime.ApplicationStopping.Register(() =>
			{
				_cts.Cancel();
				try
				{
					_bootstrappingTask?.Wait();
				}
				catch (OperationCanceledException)
				{
				}
			});
		}

		protected JobsOptions Options { get; }

		protected IStorage Storage { get; }

		protected IProcessingServer Server { get; }

		public IServiceProvider Provider { get; private set; }

		public Task BootstrapAsync()
		{
			return (_bootstrappingTask = BootstrapTaskAsync());
		}

		private async Task BootstrapTaskAsync()
		{
			await Storage.InitializeAsync(_cts.Token);
			if (_cts.IsCancellationRequested) return;

			await WorkOutCronJobs();
			if (_cts.IsCancellationRequested) return;

			await BootstrapCoreAsync();
			if (_cts.IsCancellationRequested) return;

			Server.Start();

			_ctsRegistration.Dispose();
			_cts.Dispose();
		}

		public async Task WorkOutCronJobs()
		{
			var entries = Options.CronJobRegistry?.Build() ?? new CronJobRegistry.Entry[0];
			using (var scope = Provider.CreateScope())
			{
				var provider = scope.ServiceProvider;
				var connection = provider.GetService<IStorageConnection>();

				var currentJobs = await connection.GetCronJobsAsync();
				await WorkOutCronJobsCore(connection, entries, currentJobs);
			}
		}

		public virtual async Task WorkOutCronJobsCore(
			IStorageConnection connection,
			CronJobRegistry.Entry[] entries,
			CronJob[] currentJobs)
		{<commit_msg>Minor renaming<commit_after>using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using MR.AspNetCore.Jobs.Models;
using MR.AspNetCore.Jobs.Server;

namespace MR.AspNetCore.Jobs
{
	public abstract class BootstrapperBase : IBootstrapper
	{
		private IApplicationLifetime _appLifetime;
		private CancellationTokenSource _cts;
		private CancellationTokenRegistration _ctsRegistration;
		private Task _bootstrappingTask;

		public BootstrapperBase(
			JobsOptions options,
			IStorage storage,
			IProcessingServer server,
			IApplicationLifetime appLifetime,
			IServiceProvider provider)
		{
			Options = options;
			Storage = storage;
			Server = server;
			Provider = provider;
			_appLifetime = appLifetime;

			_cts = new CancellationTokenSource();
			_ctsRegistration = appLifetime.ApplicationStopping.Register(() =>
			{
				_cts.Cancel();
				try
				{
					_bootstrappingTask?.Wait();
				}
				catch (OperationCanceledException)
				{
				}
			});
		}

		protected JobsOptions Options { get; }

		protected IStorage Storage { get; }

		protected IProcessingServer Server { get; }

		protected IServiceProvider Provider { get; }

		public Task BootstrapAsync()
		{
			return (_bootstrappingTask = BootstrapTaskAsync());
		}

		private async Task BootstrapTaskAsync()
		{
			await Storage.InitializeAsync(_cts.Token);
			if (_cts.IsCancellationRequested) return;

			await SyncCronJobsAsync();
			if (_cts.IsCancellationRequested) return;

			await BootstrapCoreAsync();
			if (_cts.IsCancellationRequested) return;

			Server.Start();

			_ctsRegistration.Dispose();
			_cts.Dispose();
		}

		public async Task SyncCronJobsAsync()
		{
			var entries = Options.CronJobRegistry?.Build() ?? new CronJobRegistry.Entry[0];
			using (var scope = Provider.CreateScope())
			{
				var provider = scope.ServiceProvider;
				var connection = provider.GetService<IStorageConnection>();

				var currentJobs = await connection.GetCronJobsAsync();
				await SyncCronJobsCoreAsync(connection, entries, currentJobs);
			}
		}

		public virtual async Task SyncCronJobsCoreAsync(
			IStorageConnection connection,
			CronJobRegistry.Entry[] entries,
			CronJob[] currentJobs)
		{
{'fraction_non_alphanumeric': 0.08682439765574126, 'fraction_numerical': 0.0004341219882787063, 'mean_word_length': 4.296551724137931, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6604059', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1379, 'n_words': 303}