public void TestFirstRun(string cronExpression, string nowDateTime, string expectedDateTime)
        {
            // Arrange

            var cron          = CronExpression.Parse(cronExpression);
            var lastOccurence = default(DateTime?);
            var now           = DateTime.Parse(nowDateTime).AsUtc();

            // Act

            var missedOccurrences = _provider.GetMissedOccurrenceAsync(cron, lastOccurence, now);

            // Assert

            var expected = DateTime.Parse(expectedDateTime).AsUtc();

            Assert.NotNull(missedOccurrences);
            Assert.Equal(1, missedOccurrences.Count);
            Assert.Equal(expected, missedOccurrences.Single());
        }
Пример #2
0
        public async Task ExecuteAsync(string scheduleCronExpression)
        {
            if (!await Lock.WaitAsync(TimeSpan.FromMilliseconds(100)))
            {
                _log.Info("Job execution has been skipped because previous job is still executing");
                return;
            }

            try
            {
                var scheduleCron = CronExpression.Parse(scheduleCronExpression);

                _log.Info
                (
                    $"Executing the job. Schedule CRON: {scheduleCron}, balances interval: {_reportSettings.BalancesIntervalFromSchedule}"
                );

                var lastOccurrence = await _lastReportOccurrenceRepository.GetLastOccurrenceOrDefaultAsync();

                var now = DateTime.UtcNow;

                _log.Info($"Last report occurence is [{lastOccurrence:s}]");

                var missedOccurrencesProvider = new JobMissedOccurrencesProvider();
                var missedOccurrences         = missedOccurrencesProvider.GetMissedOccurrenceAsync(scheduleCron, lastOccurrence, now);

                _log.Info("Missed occurrences", missedOccurrences);

                foreach (var occurrence in missedOccurrences)
                {
                    await _reportBuilder.BuildAsync(occurrence + _reportSettings.BalancesIntervalFromSchedule);

                    await _lastReportOccurrenceRepository.SaveLastOccurrenceAsync(occurrence);
                }
            }
            finally
            {
                Lock.Release();

                _log.Info("Job execution has been finished");
            }
        }