Beispiel #1
0
        static void Main(string[] args)
        {
            var configBuilder = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile(@"config/appsettings.json", optional: true, reloadOnChange: true);

            var config = configBuilder.Build();

            var serviceProvider = new ServiceCollection()
                                  .ConfigureWritable <SchedulerConfig>(config, "schedulerConfig")
                                  .Configure <SmtpConfig>(config.GetSection("smtpConfig"))
                                  .AddSingleton <ISmtpService, SmtpService>()
                                  .AddSingleton <ICsvService, CsvService>()
                                  .AddSingleton <IEmailGenerator, EmailGenerator>()
                                  .AddSingleton <BackgroundWorker>()
                                  .BuildServiceProvider();

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .WriteTo.ColoredConsole()
                         .WriteTo.Async(c => c.File(@"mail-spammer-log.txt", rollingInterval: RollingInterval.Day))
                         .CreateLogger();

            AppLogger.Information($"APPLICATION INITIALIZED {DateTime.Now}");

            var worker = serviceProvider.GetService <BackgroundWorker>();

            worker.Run();

            Log.CloseAndFlush();
        }
Beispiel #2
0
        public void Run()
        {
            var disposable = Observable.Defer(() =>
            {
                AppLogger.Information("Checking for new data...");

                return(_csvService
                       .GetCollectionFromFile <Person, CsvPersonMapper>(@"database.csv",
                                                                        _schedulerConfig.Value.StartingPoint)
                       .Buffer(_schedulerConfig.Value.PackageSize)
                       .Select(people =>
                {
                    var timer = Observable.Timer(TimeSpan.FromSeconds(_schedulerConfig.Value.TimeLimit))
                                .DoOnComplete(() => { AppLogger.Information($"Time limit have passed."); });

                    var sending = people.Select(person => _smtpService.SendEmail(person))
                                  .Concat()
                                  .DoOnNext(_ => { _schedulerConfig.Update(config => { config.StartingPoint += 1; }); })
                                  .Buffer(people.Count)
                                  .DoOnComplete(() => { AppLogger.Information($"Package sent."); });

                    return timer.Zip(sending, (time, result) => new OperationResult.Success())
                    .DoOnComplete(() =>
                    {
                        AppLogger.Information(
                            $"{_schedulerConfig.Value.PackageSize} mails sent in max of {_schedulerConfig.Value.TimeLimit} seconds.");
                    });
                })
                       .Concat());
            }).RepeatWhen(observable => observable.Delay(TimeSpan.FromSeconds(3)));

            using (disposable.Subscribe())
            {
                Console.Read();
            }
        }