protected override async Task ProcessMessage(DomainEventNotification notification, CancellationToken cancellationToken) { string lockName = $"domainevt:{notification.Id}"; if (!await _distributedLock.TryAcquireLock(lockName, cancellationToken)) { return; } try { // TODO : Add transaction !!! var assm = Assembly.GetExecutingAssembly(); var lstTask = new List <Task>(); foreach (var record in notification.Evts) { var genericType = assm.GetType(record.Type); var messageBrokerType = typeof(IDomainEvtConsumerGeneric <>).MakeGenericType(genericType); var lst = (IEnumerable <object>)_serviceProvider.GetService(typeof(IEnumerable <>).MakeGenericType(messageBrokerType)); var message = JsonConvert.DeserializeObject(record.Content, genericType); foreach (var r in lst) { await(Task) messageBrokerType.GetMethod("Handle").Invoke(r, new object[] { message, cancellationToken }); } } } finally { await _distributedLock.ReleaseLock(lockName, cancellationToken); } }
protected override async Task ProcessMessage(ExternalEventNotification notification, CancellationToken cancellationToken) { string lockName = $"extevt:{notification.Id}"; if (!await _distributedLock.TryAcquireLock(lockName, cancellationToken)) { return; } try { var subscriber = await _subscriberRepository.Get(notification.CasePlanInstanceId, notification.CasePlanElementInstanceId, notification.EvtName, cancellationToken); if (subscriber == null) { throw new InvalidOperationException("subscriber doesn't exist"); } subscriber.IsCaptured = true; subscriber.Parameters = notification.Parameters; await _subscriberRepository.Update(subscriber, cancellationToken); await MessageBroker.QueueCasePlanInstance(notification.CasePlanInstanceId, cancellationToken); } finally { await _distributedLock.ReleaseLock(lockName, cancellationToken); } }
public async Task <SCIMRepresentation> Handle(PatchRepresentationCommand patchRepresentationCommand) { CheckParameter(patchRepresentationCommand.PatchRepresentation); var lockName = $"representation-{patchRepresentationCommand.Id}"; await _distributedLock.WaitLock(lockName, CancellationToken.None); try { var existingRepresentation = await _scimRepresentationQueryRepository.FindSCIMRepresentationById(patchRepresentationCommand.Id); if (existingRepresentation == null) { throw new SCIMNotFoundException(string.Format(Global.ResourceNotFound, patchRepresentationCommand.Id)); } existingRepresentation.ApplyPatches(patchRepresentationCommand.PatchRepresentation.Operations, _options.IgnoreUnsupportedCanonicalValues); existingRepresentation.SetUpdated(DateTime.UtcNow); using (var transaction = await _scimRepresentationCommandRepository.StartTransaction()) { await _scimRepresentationCommandRepository.Update(existingRepresentation); await transaction.Commit(); } return(existingRepresentation); } finally { await _distributedLock.ReleaseLock(lockName, CancellationToken.None); } }
protected override async Task ProcessMessage(CasePlanInstanceNotification notification, CancellationToken cancellationToken) { string lockName = $"caseplaninstance:{notification.Id}"; if (!await _distributedLock.TryAcquireLock(lockName, cancellationToken)) { return; } var casePlanInstance = await _eventStoreRepository.GetLastAggregate <CasePlanInstanceAggregate>(notification.CasePlanInstanceId, CasePlanInstanceAggregate.GetStreamName(notification.CasePlanInstanceId)); try { if (casePlanInstance == null || string.IsNullOrWhiteSpace(casePlanInstance.AggregateId)) { throw new InvalidOperationException($"case plan instance '{notification.CasePlanInstanceId}' doesn't exist"); } await _casePlanInstanceProcessor.Execute(casePlanInstance, cancellationToken); await _commitAggregateHelper.Commit(casePlanInstance, casePlanInstance.GetStreamName(), cancellationToken); } finally { await _distributedLock.ReleaseLock(lockName, cancellationToken); } }
protected override async Task ProcessMessage(MessageNotification notification, CancellationToken cancellationToken) { string lockName = $"messages:{notification.Id}"; if (!await _distributedLock.TryAcquireLock(lockName, cancellationToken)) { return; } var processInstance = await _eventStoreRepository.GetLastAggregate <ProcessInstanceAggregate>(notification.ProcessInstanceId, ProcessInstanceAggregate.GetStreamName(notification.ProcessInstanceId)); try { if (processInstance == null || string.IsNullOrWhiteSpace(processInstance.AggregateId)) { throw new InvalidOperationException($"process instance '{notification.ProcessInstanceId}' doesn't exist"); } processInstance.ConsumeMessage(new MessageToken { Name = notification.MessageName, MessageContent = notification.Content }); await _commitAggregateHelper.Commit(processInstance, processInstance.GetStreamName(), cancellationToken); await MessageBroker.QueueProcessInstance(processInstance.AggregateId, false, cancellationToken); } finally { await _distributedLock.ReleaseLock(lockName, cancellationToken); } }
protected override async Task ProcessMessage(StateTransitionNotification message, CancellationToken cancellationToken) { string lockName = $"statetransitions:{message.Id}"; if (!await _distributedLock.TryAcquireLock(lockName, cancellationToken)) { return; } var processInstance = await _eventStoreRepository.GetLastAggregate <ProcessInstanceAggregate>(message.ProcessInstanceId, ProcessInstanceAggregate.GetStreamName(message.ProcessInstanceId)); try { if (processInstance == null || string.IsNullOrWhiteSpace(processInstance.AggregateId)) { throw new InvalidOperationException($"process instance '{message.ProcessInstanceId}' doesn't exist"); } processInstance.ConsumeStateTransition(message); await _commitAggregateHelper.Commit(processInstance, processInstance.GetStreamName(), cancellationToken); await MessageBroker.QueueProcessInstance(processInstance.AggregateId, false, cancellationToken); _logger.LogInformation($"Make transition '{message.State}' on the user task instance '{message.FlowNodeInstanceId}'"); } finally { await _distributedLock.ReleaseLock(lockName, cancellationToken); } }
private static void _doWork(string semaphore) { var isThreadLocked = _locker.AcquireLock(semaphore); Console.WriteLine($"Did we get a lock => {isThreadLocked}"); if (isThreadLocked) { Console.WriteLine("Working..."); Thread.Sleep(_random.Next(1000, 3000)); _locker.ReleaseLock(semaphore); } }
public async Task <SCIMRepresentation> Handle(PatchRepresentationCommand patchRepresentationCommand) { CheckParameter(patchRepresentationCommand.PatchRepresentation); var lockName = $"representation-{patchRepresentationCommand.Id}"; await _distributedLock.WaitLock(lockName, CancellationToken.None); try { var existingRepresentation = await _scimRepresentationQueryRepository.FindSCIMRepresentationById(patchRepresentationCommand.Id); if (existingRepresentation == null) { throw new SCIMNotFoundException(string.Format(Global.ResourceNotFound, patchRepresentationCommand.Id)); } var oldRepresentation = existingRepresentation.Clone() as SCIMRepresentation; var patchResult = existingRepresentation.ApplyPatches(patchRepresentationCommand.PatchRepresentation.Operations, _options.IgnoreUnsupportedCanonicalValues); existingRepresentation.SetUpdated(DateTime.UtcNow); var references = await _representationReferenceSync.Sync(patchRepresentationCommand.ResourceType, existingRepresentation, patchResult, patchRepresentationCommand.Location); using (var transaction = await _scimRepresentationCommandRepository.StartTransaction()) { await _scimRepresentationCommandRepository.Update(existingRepresentation); foreach (var reference in references.Representations) { await _scimRepresentationCommandRepository.Update(reference); } await transaction.Commit(); } await Notify(references); existingRepresentation.ApplyEmptyArray(); return(existingRepresentation); } finally { await _distributedLock.ReleaseLock(lockName, CancellationToken.None); } }
public async Task <SCIMRepresentation> Handle(ReplaceRepresentationCommand replaceRepresentationCommand) { var requestedSchemas = replaceRepresentationCommand.Representation.Schemas; if (!requestedSchemas.Any()) { throw new SCIMBadSyntaxException(string.Format(Global.AttributeMissing, SCIMConstants.StandardSCIMRepresentationAttributes.Schemas)); } var schema = await _scimSchemaQueryRepository.FindRootSCIMSchemaByResourceType(replaceRepresentationCommand.ResourceType); var allSchemas = new List <string> { schema.Id }; allSchemas.AddRange(schema.SchemaExtensions.Select(s => s.Schema)); var unsupportedSchemas = requestedSchemas.Where(s => !allSchemas.Contains(s)); if (unsupportedSchemas.Any()) { throw new SCIMBadSyntaxException(string.Format(Global.SchemasAreUnknown, string.Join(",", unsupportedSchemas))); } var schemas = await _scimSchemaQueryRepository.FindSCIMSchemaByIdentifiers(requestedSchemas); var lockName = $"representation-{replaceRepresentationCommand.Id}"; await _distributedLock.WaitLock(lockName, CancellationToken.None); try { var existingRepresentation = await _scimRepresentationQueryRepository.FindSCIMRepresentationById(replaceRepresentationCommand.Id); if (existingRepresentation == null) { throw new SCIMNotFoundException(string.Format(Global.ResourceNotFound, replaceRepresentationCommand.Id)); } var updatedRepresentation = _scimRepresentationHelper.ExtractSCIMRepresentationFromJSON(replaceRepresentationCommand.Representation.Attributes, replaceRepresentationCommand.Representation.ExternalId, schemas.ToList()); existingRepresentation.RemoveAttributes(updatedRepresentation.Attributes.Select(_ => _.SchemaAttribute.Id)); foreach (var updatedAttribute in updatedRepresentation.Attributes) { if (updatedAttribute.SchemaAttribute.Mutability == SCIMSchemaAttributeMutabilities.IMMUTABLE) { throw new SCIMImmutableAttributeException(string.Format(Global.AttributeImmutable, updatedAttribute.Id)); } if (updatedAttribute.SchemaAttribute.Mutability == SCIMSchemaAttributeMutabilities.WRITEONLY || updatedAttribute.SchemaAttribute.Mutability == SCIMSchemaAttributeMutabilities.READWRITE) { existingRepresentation.AddAttribute(updatedAttribute); } } existingRepresentation.SetExternalId(updatedRepresentation.ExternalId); existingRepresentation.SetUpdated(DateTime.UtcNow); using (var transaction = await _scimRepresentationCommandRepository.StartTransaction()) { await _scimRepresentationCommandRepository.Update(existingRepresentation); await transaction.Commit(); } return(existingRepresentation); } finally { await _distributedLock.ReleaseLock(lockName, CancellationToken.None); } }
public async Task <SCIMRepresentation> Handle(ReplaceRepresentationCommand replaceRepresentationCommand) { var requestedSchemas = replaceRepresentationCommand.Representation.Schemas; if (!requestedSchemas.Any()) { throw new SCIMBadSyntaxException(string.Format(Global.AttributeMissing, StandardSCIMRepresentationAttributes.Schemas)); } var schema = await _scimSchemaQueryRepository.FindRootSCIMSchemaByResourceType(replaceRepresentationCommand.ResourceType); var allSchemas = new List <string> { schema.Id }; allSchemas.AddRange(schema.SchemaExtensions.Select(s => s.Schema)); var unsupportedSchemas = requestedSchemas.Where(s => !allSchemas.Contains(s)); if (unsupportedSchemas.Any()) { throw new SCIMBadSyntaxException(string.Format(Global.SchemasAreUnknown, string.Join(",", unsupportedSchemas))); } var schemas = await _scimSchemaQueryRepository.FindSCIMSchemaByIdentifiers(requestedSchemas); var lockName = $"representation-{replaceRepresentationCommand.Id}"; await _distributedLock.WaitLock(lockName, CancellationToken.None); try { var existingRepresentation = await _scimRepresentationQueryRepository.FindSCIMRepresentationById(replaceRepresentationCommand.Id); if (existingRepresentation == null) { throw new SCIMNotFoundException(string.Format(Global.ResourceNotFound, replaceRepresentationCommand.Id)); } var oldRepresentation = (SCIMRepresentation)existingRepresentation.Clone(); var mainSchema = schemas.First(s => s.Id == schema.Id); var extensionSchemas = schemas.Where(s => s.Id != schema.Id).ToList(); var updatedRepresentation = _scimRepresentationHelper.ExtractSCIMRepresentationFromJSON( replaceRepresentationCommand.Representation.Attributes, replaceRepresentationCommand.Representation.ExternalId, mainSchema, extensionSchemas); var allExistingAttributes = existingRepresentation.HierarchicalAttributes; existingRepresentation.RemoveAttributesBySchemaAttrId(updatedRepresentation.FlatAttributes.Select(_ => _.SchemaAttribute.Id)); foreach (var kvp in updatedRepresentation.HierarchicalAttributes.GroupBy(h => h.FullPath)) { var fullPath = kvp.Key; var filteredExistingAttributes = allExistingAttributes.Where(a => a.FullPath == fullPath); var invalidAttrs = filteredExistingAttributes.Where(fa => !kvp.Any(a => a.IsMutabilityValid(fa))); if (invalidAttrs.Any()) { throw new SCIMImmutableAttributeException(string.Format(Global.AttributeImmutable, string.Join(",", invalidAttrs.Select(a => a.FullPath)))); } foreach (var rootAttr in kvp) { if (rootAttr.SchemaAttribute.Mutability == SCIMSchemaAttributeMutabilities.WRITEONLY || rootAttr.SchemaAttribute.Mutability == SCIMSchemaAttributeMutabilities.READWRITE || rootAttr.SchemaAttribute.Mutability == SCIMSchemaAttributeMutabilities.IMMUTABLE) { var flatAttrs = rootAttr.ToFlat(); foreach (var attr in flatAttrs) { existingRepresentation.AddAttribute(attr); } } } } existingRepresentation.SetDisplayName(updatedRepresentation.DisplayName); existingRepresentation.SetExternalId(updatedRepresentation.ExternalId); existingRepresentation.SetUpdated(DateTime.UtcNow); var isReferenceProperty = await _representationReferenceSync.IsReferenceProperty(replaceRepresentationCommand.Representation.Attributes.GetKeys()); var references = await _representationReferenceSync.Sync(replaceRepresentationCommand.ResourceType, oldRepresentation, existingRepresentation, replaceRepresentationCommand.Location, !isReferenceProperty); using (var transaction = await _scimRepresentationCommandRepository.StartTransaction()) { await _scimRepresentationCommandRepository.Update(existingRepresentation); foreach (var reference in references.Representations) { await _scimRepresentationCommandRepository.Update(reference); } await transaction.Commit(); } await Notify(references); existingRepresentation.ApplyEmptyArray(); return(existingRepresentation); } finally { await _distributedLock.ReleaseLock(lockName, CancellationToken.None); } }