public static IApplicationBuilder RegisterHangfireJobs(this IApplicationBuilder app)
    {
        HangfireUtil.DeleteAllJobs();

        var serviceProvider = app.GetServiceProvider();
        var jobService      = serviceProvider.GetRequiredService <JobsService>();

        serviceProvider.GetRequiredService <StorageService>().ResetHangfireRedisStorage().WaitAndUnwrapException();
        serviceProvider.GetRequiredService <RssFeedService>().RegisterJobAllRssScheduler().InBackground();
        serviceProvider.GetRequiredService <EpicGamesService>().RegisterJobEpicGamesBroadcaster().InBackground();
        serviceProvider.GetRequiredService <ShalatTimeNotifyService>().RegisterJobShalatTimeAsync().InBackground();

        jobService.ClearPendingJobs();
        jobService.RegisterJobChatCleanUp().InBackground();
        jobService.RegisterJobClearLog();
        jobService.RegisterJobDeleteOldStep();
        jobService.RegisterJobDeleteOldRssHistory();
        jobService.RegisterJobDeleteOldMessageHistory();
        jobService.RegisterJobRunMysqlBackup();
        jobService.RegisterJobRunDeleteOldUpdates();

        var botService     = app.GetRequiredService <BotService>();
        var botEnvironment = botService.CurrentEnvironment()
                             .Result;

        // This job enabled for non Production,
        // Daily demote for free Administrator at Testing Group
        if (botEnvironment != BotEnvironmentLevel.Production)
        {
            serviceProvider.GetRequiredService <JobsService>()
            .RegisterJobAdminCleanUp();
        }

        return(app);
    }
Пример #2
0
    public static IServiceCollection AddHangfireServerAndConfig(this IServiceCollection services)
    {
        Log.Debug("Adding Hangfire Service");

        var scope = services.BuildServiceProvider();

        var hangfireConfig = scope.GetRequiredService <IOptionsSnapshot <HangfireConfig> >().Value;
        var connStrings    = scope.GetRequiredService <IOptionsSnapshot <ConnectionStrings> >().Value;

        // services.AddHangfireServer(options => {
        //     options.WorkerCount = Environment.ProcessorCount * hangfireConfig.WorkerMultiplier;
        //     options.Queues = hangfireConfig.Queues;
        // });

        services.AddHangfire
        (
            config => {
            switch (hangfireConfig.DataStore)
            {
            case HangfireDataStore.MySql:
                var hangfireMysql = hangfireConfig.MysqlConnection;
                if (hangfireMysql.IsNullOrEmpty())
                {
                    hangfireMysql = connStrings.MySql;
                }

                config.UseStorage(HangfireUtil.GetMysqlStorage(hangfireMysql));
                break;

            case HangfireDataStore.Sqlite:
                config.UseStorage(HangfireUtil.GetSqliteStorage(hangfireConfig.Sqlite));
                break;

            case HangfireDataStore.Litedb:
                config.UseStorage(HangfireUtil.GetLiteDbStorage(hangfireConfig.LiteDb));
                break;

            case HangfireDataStore.Redis:
                // config.UseStorage(HangfireUtil.GetRedisStorage(hangfireConfig.Redis));
                config.UseRedisStorage(HangfireUtil.GetRedisConnectionMultiplexer(hangfireConfig.Redis));
                break;

            case HangfireDataStore.Memory:
                config.UseMemoryStorage();
                break;

            default:
                Log.Warning("Unknown Hangfire DataStore");
                break;
            }

            config.UseDarkDashboard()
            .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
            .UseHeartbeatPage(TimeSpan.FromSeconds(15))
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings()
            .UseColouredConsoleLogProvider()
            .UseSerilogLogProvider();
        }
        );

        Log.Debug("Hangfire Service added..");

        return(services);
    }