/// <inheritdoc/> protected override async Task <TaskModel> ProtectedHandleAsync(CreateTaskCommand request, CancellationToken cancellationToken) { context.BeginTransaction(); if (request.Command.CommandType != CommandType.ProcessObjectCommand || request.Command.GetType() != typeof(ProcessObjectCommand)) { throw new NotSupportedException("The provided command type is not supported."); } var command = request.Command as ProcessObjectCommand; processMutex = new Mutex(false, this.GetMethodName()); return(await processMutex.Execute(new TimeSpan(0, 0, 1), async() => { context.BeginTransaction(); Guid objectId = Guid.Parse(command.Id); var activeCount = await context.TaskRepository.CountAsync(e => e.ObjectId == objectId && (e.Status == (int)Domain.Enums.TaskStatus.Created || e.Status == (int)Domain.Enums.TaskStatus.Queued || e.Status == (int)Domain.Enums.TaskStatus.Processing)); if (activeCount > 0) { throw new AmiException("The specified object is already actively being processed."); } var entity = new TaskEntity() { Id = idGenService.CreateId(), CreatedDate = DateTime.UtcNow, ModifiedDate = DateTime.UtcNow, Status = (int)Domain.Enums.TaskStatus.Queued, Progress = 0, Position = queue.Count, CommandType = (int)CommandType.ProcessObjectCommand, CommandSerialized = serializer.Serialize(command), ObjectId = objectId }; context.TaskRepository.Add(entity); await context.SaveChangesAsync(cancellationToken); var result = TaskModel.Create(entity, serializer); queue.Add(result); context.CommitTransaction(); return result; })); }
/// <summary> /// /// </summary> /// <typeparam name="TResponse"></typeparam> /// <param name="payload"></param> /// <param name="context"></param> /// <param name="tcs"></param> /// <param name="receivedMessageCallback"></param> public void ClientCall <TResponse>( byte[] payload, SendCallContext <TResponse> context, IReceivedMessageCallback <TResponse> receivedMessageCallback ) where TResponse : class { var task = Task.Factory.StartNew(async() => { await channel.SendAsync(payload); var buffer = await channel.ReceiveAsync(); receivedMessageCallback.OnClientResponse(true, context.Completion, buffer); }); taskQueue.Add(task); }