public override void Execute(
            RunScheduleOp operation)
        {
            operation.MustForArg(nameof(operation)).NotBeNull();

            var getDistinctStringSerializedIdsOp = new StandardGetDistinctStringSerializedIdsOp(
                new RecordFilter(
                    objectTypes: new[]
            {
                typeof(ExecuteOpRequestedEvent <ExecuteOpOnScheduleOp>).ToRepresentation(),
            },
                    deprecatedIdTypes: new[]
            {
                operation.DeprecatedIdentifierType
            }));
            var distinctIds = this.registeredScheduleStream.Execute(getDistinctStringSerializedIdsOp);

            foreach (var distinctId in distinctIds)
            {
                var getLatestRecordOp = new StandardGetLatestRecordOp(
                    new RecordFilter(
                        ids: new[]
                {
                    distinctId,
                }));

                var runOnScheduleRecord = this.registeredScheduleStream.Execute(getLatestRecordOp);
                runOnScheduleRecord
                .Payload
                .PayloadTypeRepresentation
                .MustForOp("recordFromRegisteredScheduleStreamExpectedToBeExecuteOpRequestedOfExecuteOpOnSchedule")
                .BeEqualTo(typeof(ExecuteOpRequestedEvent <ExecuteOpOnScheduleOp>).ToRepresentation());

                var executeOpOnScheduleOpEvent =
                    runOnScheduleRecord.Payload.DeserializePayloadUsingSpecificFactory <ExecuteOpRequestedEvent <ExecuteOpOnScheduleOp> >(
                        this.registeredScheduleStream.SerializerFactory);
                var executeOpOnScheduleOp = executeOpOnScheduleOpEvent.Operation;
                this.executeOpOnScheduleProtocol.Execute(executeOpOnScheduleOp);
            }
        }
Пример #2
0
        public override IReadOnlyCollection <StringSerializedIdentifier> Execute(
            StandardGetDistinctStringSerializedIdsOp operation)
        {
            operation.MustForArg(nameof(operation)).NotBeNull();

            var sqlServerLocator = this.TryGetLocator(operation);

            var convertedRecordFilter = this.ConvertRecordFilter(operation.RecordFilter, sqlServerLocator);

            var storedProcOp = StreamSchema.Sprocs.GetDistinctStringSerializedIds.BuildExecuteStoredProcedureOp(
                this.Name,
                convertedRecordFilter);

            var sqlProtocol = this.BuildSqlOperationsProtocol(sqlServerLocator);
            var sprocResult = sqlProtocol.Execute(storedProcOp);

            var resultXml          = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetDistinctStringSerializedIds.OutputParamName.StringIdentifiersOutputXml)].GetValueOfType <string>();
            var resultList         = resultXml.GetTagsFromXmlString();
            var typeIdToTypeRepMap = resultList
                                     .Select(_ => _.Name)
                                     .Distinct()
                                     .ToDictionary(
                k => k,
                v => this.GetTypeById(
                    sqlServerLocator,
                    int.Parse(v, CultureInfo.InvariantCulture),
                    true)
                .WithVersion);

            var result = resultList
                         .Select(
                _ => new StringSerializedIdentifier(
                    _.Value,
                    typeIdToTypeRepMap[_.Name]))
                         .ToList();

            return(result);
        }
Пример #3
0
        public override void Execute(
            RunReactorOp operation)
        {
            operation.MustForArg(nameof(operation)).NotBeNull();

            var getDistinctStringSerializedIdsOp = new StandardGetDistinctStringSerializedIdsOp(
                new RecordFilter(
                    objectTypes: new[]
            {
                typeof(ReactionRegistration).ToRepresentation(),
            },
                    deprecatedIdTypes: new[]
            {
                operation.DeprecatedIdentifierType
            }));
            var distinctIds = this.reactionRegistrationStream.Execute(getDistinctStringSerializedIdsOp);

            Parallel.ForEach(distinctIds,
                             new ParallelOptions {
                MaxDegreeOfParallelism = operation.DegreesOfParallelismForDependencyChecks
            },
                             distinctId =>
            {
                var found = ReactionRegistrationIdToNextEvaluationCutoffMap.TryGetValue(distinctId, out var nextEvaluationCutoff);

                if (!found || DateTime.UtcNow > nextEvaluationCutoff)
                {
                    try
                    {
                        var getLatestRecordOp = new StandardGetLatestRecordOp(
                            new RecordFilter(
                                ids: new[]
                        {
                            distinctId,
                        }));

                        var reactionRegistrationRecord = this.reactionRegistrationStream.Execute(getLatestRecordOp);
                        reactionRegistrationRecord
                        .Payload
                        .PayloadTypeRepresentation
                        .RemoveAssemblyVersions()
                        .MustForOp("recordFromReactionRegistrationStreamExpectedToBeRegisteredReaction")
                        .BeEqualTo(ReactionRegistrationTypeRepWithoutVersion);

                        var reactionRegistration =
                            reactionRegistrationRecord.Payload.DeserializePayloadUsingSpecificFactory <ReactionRegistration>(
                                this.reactionRegistrationStream.SerializerFactory);

                        var evaluateReactionRegistrationOp     = new EvaluateReactionRegistrationOp(reactionRegistration);
                        var evaluateReactionRegistrationResult =
                            this.evaluateReactionRegistrationProtocol.Execute(evaluateReactionRegistrationOp);
                        if (evaluateReactionRegistrationResult.ReactionEvent != null)
                        {
                            var reaction = evaluateReactionRegistrationResult.ReactionEvent;

                            this.reactionStream.PutWithId(reaction.Id, reaction, reaction.Tags);

                            // once we have recorded the reaction then we can finalize the handling cycle.
                            foreach (var recordSetHandlingMemento in evaluateReactionRegistrationResult.RecordSetHandlingMementos)
                            {
                                recordSetHandlingMemento.CompleteSet();
                            }
                        }

                        var newNextEvaluationCutoff = DateTime.UtcNow.Add(reactionRegistration.IdealWaitTimeBetweenEvaluations);
                        ReactionRegistrationIdToNextEvaluationCutoffMap.AddOrUpdate(
                            distinctId,
                            newNextEvaluationCutoff,
                            (_, __) => newNextEvaluationCutoff);
                    }
                    catch (Exception ex)
                    {
                        throw new ReactorException(
                            Invariant($"Failed to process {nameof(ReactionRegistration)} Id: {distinctId}."),
                            ex,
                            operation);
                    }
                }
            });
        }