<commit_before>﻿using System;
using System.Threading.Tasks;
using Akka.Actor;
using GridDomain.Common;
using GridDomain.CQRS;
using GridDomain.Logging;
using GridDomain.Scheduling.Akka.Messages;

namespace GridDomain.Scheduling.FutureEvents
{
    /// <summary>
    ///     Listening to scheduling events from aggregates and modify quartz jobs accordinally
    /// </summary>
    internal class FutureEventsSchedulingMessageHandler : IHandler<FutureEventScheduledEvent>,
                                                         IHandler<FutureEventCanceledEvent>
    {
        private readonly IActorRef _schedulerActorRef;
        private readonly ProcessEntry _schedulingFutureEventProcessEntry;

        public FutureEventsSchedulingMessageHandler(IActorRef schedulingActor)
        {
            _schedulerActorRef = schedulingActor;

            _schedulingFutureEventProcessEntry = new ProcessEntry(GetType().
                    Name,
                "Scheduling raise future event command",
                "FutureEventScheduled event occured");
        }

        public Task Handle(FutureEventCanceledEvent evt, IMessageMetadata metadata)
        {
            var key = CreateScheduleKey(evt.FutureEventId, evt.SourceId, evt.SourceName);
            return _schedulerActorRef.Ask<Unscheduled>(new Unschedule(key));
        }

        public Task Handle(FutureEventScheduledEvent message, IMessageMetadata messageMetadata)
        {
            var scheduleId = message.Id;
            var succesEventType = message.Event.GetType();

            var description = $"Source {message.SourceName} scheduled future event "
                              + $"{scheduleId} with payload type {succesEventType. Name} on time {message.RaiseTime}\r\n"
                              + $"Future event: {message.ToPropsString()}";

            var scheduleKey = CreateScheduleKey(scheduleId, message.SourceId, message.SourceName, description);

            var command = new RaiseScheduledDomainEventCommand(message.Id, message.SourceId, Guid.NewGuid());
            var metadata = messageMetadata.CreateChild(command.Id,_schedulingFutureEventProcessEntry);

            var scheduleEvent = new ScheduleCommandExecution(command,
                                                    scheduleKey,
                                                    ExecutionOptions.ForCommand(message.RaiseTime, succesEventType),
                                                    metadata);

            return _schedulerActorRef.Ask<Scheduled>(scheduleEvent);
        }

        internal static ScheduleKey CreateScheduleKey(Guid scheduleId, Guid sourceId, string sourceName, string description=null)
        {
            return new ScheduleKey($"{sourceName}_{sourceId:N}_fe_{scheduleId:N}",
                $"{sourceName}_{sourceId:N}_future_events",
                description);
        }
    }
}<commit_msg>configured quartz jobs fields to not exceed max column length in Sql database<commit_after>﻿using System;
using System.Threading.Tasks;
using Akka.Actor;
using GridDomain.Common;
using GridDomain.CQRS;
using GridDomain.Logging;
using GridDomain.Scheduling.Akka.Messages;

namespace GridDomain.Scheduling.FutureEvents
{
    /// <summary>
    ///     Listening to scheduling events from aggregates and modify quartz jobs accordinally
    /// </summary>
    internal class FutureEventsSchedulingMessageHandler : IHandler<FutureEventScheduledEvent>,
                                                          IHandler<FutureEventCanceledEvent>
    {
        private readonly IActorRef _schedulerActorRef;
        private readonly ProcessEntry _schedulingFutureEventProcessEntry;

        public FutureEventsSchedulingMessageHandler(IActorRef schedulingActor)
        {
            _schedulerActorRef = schedulingActor;

            _schedulingFutureEventProcessEntry = new ProcessEntry(GetType()
                                                                      .Name,
                                                                  "Scheduling raise future event command",
                                                                  "FutureEventScheduled event occured");
        }

        public Task Handle(FutureEventCanceledEvent evt, IMessageMetadata metadata)
        {
            var key = CreateScheduleKey(evt.FutureEventId, evt.SourceId, evt.SourceName);
            return _schedulerActorRef.Ask<Unscheduled>(new Unschedule(key));
        }

        public Task Handle(FutureEventScheduledEvent message, IMessageMetadata messageMetadata)
        {
            var scheduleId = message.Id;
            var succesEventType = message.Event.GetType();

            var description = $"Source {message.SourceName} id={message.SourceId} scheduled future event {message.Event.GetType().Name} id={scheduleId} o n{message.RaiseTime}\r\n";

            var scheduleKey = CreateScheduleKey(scheduleId, message.SourceId, message.SourceName, description);

            var command = new RaiseScheduledDomainEventCommand(message.Id, message.SourceId, Guid.NewGuid());
            var metadata = messageMetadata.CreateChild(command.Id, _schedulingFutureEventProcessEntry);

            var scheduleEvent = new ScheduleCommandExecution(command,
                                                             scheduleKey,
                                                             ExecutionOptions.ForCommand(message.RaiseTime, succesEventType),
                                                             metadata);

            return _schedulerActorRef.Ask<Scheduled>(scheduleEvent);
        }

        internal static ScheduleKey CreateScheduleKey(Guid scheduleId, Guid sourceId, string sourceName, string description = null)
        {
            return new ScheduleKey($"{scheduleId:N}",
                                   $"{sourceName}_{sourceId:N}_future_events",
                                   description);
        }
    }
}
{'fraction_non_alphanumeric': 0.06974393530997304, 'fraction_numerical': 0.0, 'mean_word_length': 2.079356846473029, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5529566', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1289, 'n_words': 297}