예제 #1
0
        public Task HandleAsync(ScheduleCreated @event)
        =>
        _handler
        .Handle(async() =>
        {
            _logger.LogInformation($"[{nameof(ScheduleCreatedHandler)}] - Creating first drawing for new schedule ({@event.Id}) and marking as active the closest");
            var schedule     = await _scheduleRepository.GetAsync(@event.Id);
            var daysOfWeek   = schedule.Days.Select(x => x.DayOfWeek).ToArray();
            var estimatedDay = _dateEstimatorProvider.GetClosestDate(daysOfWeek, schedule.StartTime);
            var drawing      = _drawingService.CreateDrawing(estimatedDay);
            schedule.AddDrawing(await drawing);
            schedule.MarkAsActive(await drawing);

            await _scheduleRepository.UpdateAsync(schedule);
        })
        .OnSuccess(async() =>
        {
            _logger.LogInformation($"[{nameof(ScheduleCreatedHandler)}] - First drawing created for schedule {@event.Id}.");
            var schedule      = await _scheduleRepository.GetAsync(@event.Id);
            var activeDrawing = schedule.GetActiveDrawing();
            await _busPublisher.PublishAsync(new DrawingCreated(activeDrawing.Id, activeDrawing.StartDate));
        })
        .OnCustomError(async(ex) =>
        {
            _logger.LogError($"[{nameof(ScheduleCreatedHandler)}] - Custom error occurred while creating first drawing for new schedule. {ex.Message}");
            await Task.FromException(ex);
        })
        .OnError(async(ex) =>
        {
            _logger.LogError($"[{nameof(ScheduleCreatedHandler)}] - Error occurred while creating first drawing for new schedule. Message: {ex.Message}. StackTrace: {ex.StackTrace}");
            await Task.FromException(ex);
        })
        .ExecuteAsync();
예제 #2
0
        public async Task HandleAsync(Messages.DomainEvents.ScheduleUpdated @event)
        {
            var newDrawingCreated = false;
            await _handler
            .Handle(async() =>
            {
                _logger.LogInformation($"[{nameof(ScheduleUpdatedHandler)}] - Updating drawing for schedule {@event.Id}.");
                var schedule     = await _scheduleRepository.GetAsync(@event.Id);
                var daysOfWeek   = schedule.Days.Select(x => x.DayOfWeek).ToArray();
                var estimatedDay = _dateEstimatorProvider.GetClosestDate(daysOfWeek, schedule.StartTime);
                _logger.LogInformation($"[{nameof(ScheduleUpdatedHandler)}] - Estimated date: {estimatedDay}. Provided days {string.Join(',', daysOfWeek)}, time {schedule.StartTime}.");
                if (schedule.HasActiveDrawing())
                {
                    var drawing = schedule.GetActiveDrawing();
                    drawing.UpdateStartDate(estimatedDay);
                }
                else
                {
                    var drawing = _drawingService.CreateDrawing(estimatedDay);
                    schedule.AddDrawing(await drawing);
                    schedule.MarkAsActive(await drawing);
                    newDrawingCreated = true;
                }

                await _scheduleRepository.UpdateAsync(schedule);
            })
            .OnSuccess(async() =>
            {
                var schedule = await _scheduleRepository.GetAsync(@event.Id);
                var drawing  = schedule.GetActiveDrawing();

                _logger.LogInformation($"[{nameof(ScheduleUpdatedHandler)}] - Drawing updated ({drawing.Id}) for schedule {schedule.Id}.");

                if (newDrawingCreated)
                {
                    await _busPublisher.PublishAsync(new DrawingCreated(drawing.Id, drawing.StartDate));
                }
                else
                {
                    await _busPublisher.PublishAsync(new DrawingUpdated(drawing.Id, drawing.StartDate,
                                                                        drawing.Numbers.Where(x => !x.Extra).Select(x => x.Value).ToArray(),
                                                                        drawing.Numbers.Where(x => x.Extra).Select(x => x.Value).ToArray()));
                }
            })
            .OnCustomError(async(ex) =>
            {
                _logger.LogError($"[{nameof(ScheduleCreatedHandler)}] - Custom error occurred while updating drawing for schedule. {ex.Message}");
                await Task.FromException(ex);
            })
            .OnError(async(ex) =>
            {
                _logger.LogError($"[{nameof(ScheduleCreatedHandler)}] - Error occurred while updating drawing for schedule. Message: {ex.Message}. StackTrace: {ex.StackTrace}");
                await Task.FromException(ex);
            })
            .ExecuteAsync().ConfigureAwait(false);
        }
        public Task HandleAsync(ShutdownDisabled @event)
        =>
        _handler
        .Handle(async() =>
        {
            _logger.LogInformation($"[{nameof(ShutdownDisabledHandler)}] - shutdown end. Prepare next drawing.");

            var schedule = await _scheduleRepository.GetAsync();

            if (schedule == null)
            {
                throw new ServiceException("Cannot create a drawing because there is no schedule set.");
            }

            if (schedule.HasActiveDrawing())
            {
                throw new ServiceException("Cannot create a drawing because there is already an active drawing.");
            }

            var daysOfWeek = schedule.Days.Select(x => x.DayOfWeek).ToArray();

            if (daysOfWeek.Length > 0)
            {
                _logger.LogInformation($"[{nameof(ShutdownDisabledHandler)}] - Estimating closest date.");
                var estimatedDay = _drawingSettings.IntervalInMinutes.HasValue ?
                                   DateTime.UtcNow.AddMinutes(_drawingSettings.IntervalInMinutes.Value)
                                                : _dateEstimatorProvider.GetClosestDate(daysOfWeek, schedule.StartTime);

                _logger.LogInformation($"[{nameof(ShutdownDisabledHandler)}] - Creating next drawing for schedule.");
                var drawing = _drawingService.CreateDrawing(estimatedDay);
                schedule.AddDrawing(await drawing);

                _logger.LogInformation($"[{nameof(ShutdownDisabledHandler)}] - Marking as active the closes drawing.");
                schedule.MarkAsActive(await drawing);
                await _scheduleRepository.UpdateAsync(schedule);
            }
            else
            {
                throw new ServiceException("Cannot create drawing because there are no schedule days set.");
            }
        })
        .OnSuccess(async() =>
        {
            var schedule      = await _scheduleRepository.GetAsync();
            var activeDrawing = schedule.GetActiveDrawing();

            _logger.LogInformation($"[{nameof(ShutdownDisabledHandler)}] - New drawing created ({activeDrawing.Id}).");

            await _busPublisher.PublishAsync(new Common.Messages.Events.Draw.DrawingCreated(
                                                 activeDrawing.Id, activeDrawing.StartDate));
        })
        .OnCustomError(async(ex) =>
        {
            _logger.LogError($"[{nameof(ShutdownDisabledHandler)}] - Custom error occurred while creating new drawing after shutdown end. {ex.Message}");
            await Task.FromException(ex);
        })
        .OnError(async(ex) =>
        {
            _logger.LogError($"[{nameof(ShutdownDisabledHandler)}] - Error occurred while creating new drawing after shutdown end. Message: {ex.Message}. StackTrace: {ex.StackTrace}");
            await Task.FromException(ex);
        })
        .ExecuteAsync();