Beispiel #1
0
 public JobExecutor(ILogger <JobExecutor> logger,
                    DiscordSocketClient client,
                    IBrobotService brobotService,
                    IServiceProvider serviceProvider,
                    IOptions <JobsSettings> options)
 {
     _logger          = logger;
     _client          = client;
     _brobotService   = brobotService;
     _serviceProvider = serviceProvider;
     _jobsSettings    = options.Value;
 }
Beispiel #2
0
 public JobsWorker(ILogger <JobsWorker> logger,
                   IBrobotService brobotService,
                   IServiceProvider serviceProvider,
                   IHostApplicationLifetime hostApplicationLifetime,
                   IOptions <JobsSettings> options,
                   DiscordSocketClient discordClient)
 {
     _logger                  = logger;
     _brobotService           = brobotService;
     _serviceProvider         = serviceProvider;
     _hostApplicationLifetime = hostApplicationLifetime;
     _discordClient           = discordClient;
     _jobsSettings            = options.Value;
     _jobTasks                = new List <JobTaskBase>();
 }
Beispiel #3
0
        public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureHostConfiguration(config =>
        {
            var development = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
            var isDev       = string.IsNullOrWhiteSpace(development) || development.ToLower() == "development";

            if (isDev)
            {
                config.AddUserSecrets(Assembly.GetExecutingAssembly());
            }
        })
        .ConfigureServices((hostContext, services) =>
        {
            var jobsSettings = new JobsSettings();
            hostContext.Configuration.GetSection(nameof(JobsSettings)).Bind(jobsSettings);

            services.Configure <JobsSettings>(hostContext.Configuration.GetSection(nameof(JobsSettings)));
            services.AddHttpClient <IBrobotService, BrobotService>(configure =>
            {
                configure.BaseAddress = new Uri(jobsSettings.BaseUrl);
                configure.DefaultRequestHeaders.Add("x-api-key", jobsSettings.ApiKey);
            });

            services.AddHttpClient <ITwitterService, TwitterService>(configure =>
            {
                configure.BaseAddress = new Uri(jobsSettings.TwitterApiBaseUrl);
                configure.DefaultRequestHeaders.Add("Authorization", $"Bearer {jobsSettings.TwitterBearerToken}");
            });

            var config = new DiscordSocketConfig
            {
                AlwaysDownloadUsers = true
            };
            var client = new DiscordSocketClient(config);
            services.AddSingleton <DiscordSocketClient>(client);

            if (args.Contains("-e"))
            {
                services.AddSingleton <JobExecutor>();
            }
            else
            {
                services.AddHostedService <JobsWorker>();
            }
        });