<commit_before>﻿using Bit.Core.Models;
using Bit.Hangfire.Contracts;
using Hangfire;
using Hangfire.Logging;
using System.Collections.Generic;
using System.Linq;

namespace Bit.Hangfire.Implementations
{
    public abstract class JobSchedulerBaseBackendConfiguration<TStorage> : IJobSchedulerBackendConfiguration
        where TStorage : JobStorage
    {
        public virtual JobActivator JobActivator { get; set; }

        public virtual ILogProvider LogProvider { get; set; }

        public virtual IEnumerable<IHangfireOptionsCustomizer> Customizers { get; set; }

        protected virtual BackgroundJobServer BackgroundJobServer { get; set; }

        protected abstract TStorage BuildStorage();

        public virtual void Init()
        {
            var storage = BuildStorage();

            JobStorage.Current = storage;

            GlobalConfiguration.Configuration
                .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings()
                .UseStorage(storage)
                .UseLogProvider(LogProvider);

            if (GlobalJobFilters.Filters.Any(f => f.Instance is AutomaticRetryAttribute))
            {
                GlobalConfiguration.Configuration.UseFilter(new DefaultAutomaticRetryAttribute { });
                GlobalJobFilters.Filters.Remove(GlobalJobFilters.Filters.ExtendedSingle("Finding automatic retry job filter attribute to remove it from global job filters", f => f.Instance is AutomaticRetryAttribute).Instance);
            }

            BackgroundJobServerOptions options = new BackgroundJobServerOptions
            {
                Activator = JobActivator
            };

            foreach (IHangfireOptionsCustomizer customizer in Customizers)
            {
                customizer.Customize(GlobalConfiguration.Configuration, options, storage);
            }

            BackgroundJobServer = new BackgroundJobServer(options, storage);
        }

        public virtual void Dispose()
        {
            BackgroundJobServer?.Stop(force: true);
            BackgroundJobServer?.Dispose();
        }
    }
}
<commit_msg>ability to prevent running background job server easily<commit_after>﻿using Bit.Core.Models;
using Bit.Hangfire.Contracts;
using Hangfire;
using Hangfire.Logging;
using System.Collections.Generic;
using System.Linq;

namespace Bit.Hangfire.Implementations
{
    public abstract class JobSchedulerBaseBackendConfiguration<TStorage> : IJobSchedulerBackendConfiguration
        where TStorage : JobStorage
    {
        public virtual JobActivator JobActivator { get; set; }

        public virtual ILogProvider LogProvider { get; set; }

        public virtual IEnumerable<IHangfireOptionsCustomizer> Customizers { get; set; }

        protected virtual BackgroundJobServer BackgroundJobServer { get; set; }

        protected abstract TStorage BuildStorage();

        public virtual void Init()
        {
            var storage = BuildStorage();

            JobStorage.Current = storage;

            GlobalConfiguration.Configuration
                .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings()
                .UseStorage(storage)
                .UseLogProvider(LogProvider);

            if (GlobalJobFilters.Filters.Any(f => f.Instance is AutomaticRetryAttribute))
            {
                GlobalConfiguration.Configuration.UseFilter(new DefaultAutomaticRetryAttribute { });
                GlobalJobFilters.Filters.Remove(GlobalJobFilters.Filters.ExtendedSingle("Finding automatic retry job filter attribute to remove it from global job filters", f => f.Instance is AutomaticRetryAttribute).Instance);
            }

            BackgroundJobServerOptions options = new BackgroundJobServerOptions
            {
                Activator = JobActivator
            };

            foreach (IHangfireOptionsCustomizer customizer in Customizers)
            {
                customizer.Customize(GlobalConfiguration.Configuration, options, storage);
            }

            BuildBackgroundJobServer(storage, options);
        }

        protected virtual void BuildBackgroundJobServer(TStorage storage, BackgroundJobServerOptions options)
        {
            BackgroundJobServer = new BackgroundJobServer(options, storage);
        }

        public virtual void Dispose()
        {
            BackgroundJobServer?.Stop(force: true);
            BackgroundJobServer?.Dispose();
        }
    }
}

{'fraction_non_alphanumeric': 0.06574692821728821, 'fraction_numerical': 0.0012933821944384565, 'mean_word_length': 2.552833078101072, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6081212', 'n_tokens_mistral': 1072, 'n_tokens_neox': 1028, 'n_words': 244}