public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, HttpMethods.Delete, Route = "category/{id:guid}")] HttpRequest req,
            [NotificationSubscriber] string subscriber,
            string id,
            ILogger log)
        {
            var correlationId = _guidProvider.GenerateGuid();

            _correlationInitializer.Initialize(correlationId);
            log.LogProgress(OperationStatus.Started, "Deleting category process started", correlationId);

            var categoryDeletedEvent = new DeleteCategoryProcessStartedEvent(id, correlationId);

            await _subscriber.Register(correlationId, subscriber);

            var saveEventResult = await _eventStore.AppendEvent(id, categoryDeletedEvent);

            if (!saveEventResult.Success)
            {
                log.LogFailedOperation(OperationStatus.Failed, "Deleting category process failed", saveEventResult.Errors, correlationId);
                return(new BadRequestResult());
            }

            log.LogProgress(OperationStatus.InProgress, "Request accepted to further processing.", correlationId);
            await _eventPublisher.PublishEvent(categoryDeletedEvent);

            log.LogProgress(OperationStatus.InProgress, $"{nameof(DeleteCategoryProcessStartedEvent)} published", correlationId);

            return(new AcceptedWithCorrelationIdHeaderResult(correlationId));
        }
Esempio n. 2
0
        public async Task RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            ILogger log)
        {
            _correlationInitializer.Initialize(context.InstanceId);

            try
            {
                var command = context.GetInput <RecompensateApplicationProcessCommand>();
                log.LogInformation(
                    $"Starting recompensation process(instanceId: {context.InstanceId} for ${command.Id}");

                var deleteCvCommand = new DeleteFileCommand(
                    _fileNameProvider.GetFileName(command.Id, command.Cv.Extension),
                    FileStore.CvsContainer);

                var deletePhotoCommand = new DeleteFileCommand(
                    _fileNameProvider.GetFileName(command.Id, command.Photo.Extension),
                    FileStore.PhotosContainer);

                await context.CallActivityAsync(nameof(FileDeleter), deleteCvCommand);

                await context.CallActivityAsync(nameof(FileDeleter), deletePhotoCommand);

                log.LogInformation(
                    $"Finished recompensation process(instanceId: {context.InstanceId} for ${command.Id}");
            }
            catch (Exception)
            {
                // Allow somebody from support to handle it manually
                throw;
            }
        }
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, HttpMethods.Post, Route = "application")] HttpRequest req,
            [DurableClient] IDurableOrchestrationClient processStarter,
            [NotificationSubscriber] string subscriber,
            ILogger log)
        {
            var instanceId = _guidProvider.GenerateGuid();
            _correlationInitializer.Initialize(instanceId);

            var mappingResult = await _fromFormToApplicationAddDtoRequestMapper.MapRequest(req);

            log.LogProgress(OperationStatus.Started, "Application process started", instanceId);

            if (!mappingResult.Success)
            {
                log.LogInformation($"Invalid data provided to the application process with instanceId: {instanceId}");
                return new BadRequestObjectResult(mappingResult.Errors);
            }

            await _subscriber.Register(instanceId, subscriber);

            var command = await _commandBuilder.Build(mappingResult.Value);

            await processStarter.StartNewAsync(nameof(ApplicationProcessOrchestrator), instanceId, command);

            log.LogProgress(OperationStatus.InProgress, "Started processing of application process", instanceId);
            return new AcceptedWithCorrelationIdHeaderResult(instanceId);
        }
Esempio n. 4
0
        public async Task Run(
            [ActivityTrigger] IDurableActivityContext context)
        {
            _correlationInitializer.Initialize(context.InstanceId);

            var command = context.GetInput <DeleteFileCommand>();
            await _fileDeleter.Delete(command.ContainerName, command.FileName);
        }
        public async Task Run(
            [ActivityTrigger] IDurableActivityContext context)
        {
            _correlationInitializer.Initialize(context.InstanceId);
            await _reindexManager.FinishReindexing();

            await _eventPublisher.PublishEvent(new CategoriesReindexFinishedEvent(context.InstanceId));
        }
        public async Task <IReadOnlyCollection <string> > Run(
            [ActivityTrigger] IDurableActivityContext context)
        {
            _correlationInitializer.Initialize(context.InstanceId);
            var application = await _streamLogProvider.GetStreams();

            return(application);
        }
        public async Task Run(
            [ActivityTrigger] IDurableActivityContext context)
        {
            _correlationInitializer.Initialize(context.InstanceId);
            await _reindexManager.StartReindex();

            await _eventPublisher.PublishEvent(new ApplicationReindexStartedEvent(context.InstanceId));
        }
        public async Task Run(
            [ActivityTrigger] IDurableActivityContext context)
        {
            _correlationInitializer.Initialize(context.InstanceId);
            var status = context.GetInput <string>();
            var applicationFailedEvent = new CreatingApplicationFailedEvent(context.InstanceId, status, context.InstanceId);
            await _eventStore.AppendEvent(context.InstanceId, applicationFailedEvent);

            await _eventPublisher.PublishEvent(applicationFailedEvent);
        }
        public async Task Run(
            [ActivityTrigger] IDurableActivityContext context)
        {
            var command = context.GetInput <TrackProcessStatusCommand>();

            _correlationInitializer.Initialize(command.CorrelationId);
            await _processStatusHandler.TrackStatus(
                command.CorrelationId,
                command.Status,
                command.Errors);
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, HttpMethods.Post, Route = "application/admin")] HttpRequest req,
            [DurableClient] IDurableOrchestrationClient processStarter,
            [NotificationSubscriber] string subscriber,
            ILogger log)
        {
            var correlationId = _guidProvider.GenerateGuid();

            _correlationInitializer.Initialize(correlationId);
            await _subscriber.Register(correlationId, subscriber);

            await processStarter.StartNewAsync(nameof(RebuildReadModelProcessOrchestrator), correlationId);

            log.LogProgress(OperationStatus.Started, "Started applications read model rebuild", correlationId);
            return(new AcceptedWithCorrelationIdHeaderResult(correlationId));
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, HttpMethods.Put, Route = "category/{id:guid}")] UpdateCategoryHttpRequest updateCategoryRequest,
            string id,
            [NotificationSubscriber] string subscriber,
            ILogger log)
        {
            var correlationId = _guidProvider.GenerateGuid();

            _correlationInitializer.Initialize(correlationId);
            var validationResult = await _updateCommandValidator.ValidateAsync(new UpdateCategoryCommand(id, updateCategoryRequest));

            log.LogProgress(OperationStatus.Started, "Updating category process started", correlationId);

            if (!validationResult.IsValid)
            {
                log.LogProgress(OperationStatus.Failed, "Validation failed", correlationId);
                return(new BadRequestObjectResult(validationResult.Errors));
            }

            await _subscriber.Register(correlationId, subscriber);

            var categoryChangedEvent = new UpdateCategoryProcessStartedEvent(
                id,
                updateCategoryRequest.Name,
                updateCategoryRequest.Description,
                updateCategoryRequest.SortOrder,
                correlationId);

            var saveEventResult = await _eventStore.AppendEvent(id, categoryChangedEvent);

            if (!saveEventResult.Success)
            {
                log.LogFailedOperation(OperationStatus.Failed, "Creating category process failed", saveEventResult.Errors, correlationId);
                return(new BadRequestResult());
            }

            log.LogProgress(OperationStatus.InProgress, "Request accepted to further processing.", correlationId);
            await _eventPublisher.PublishEvent(categoryChangedEvent);

            log.LogProgress(OperationStatus.InProgress, $"{nameof(UpdateCategoryProcessStartedEvent)} published", correlationId);

            return(new AcceptedWithCorrelationIdHeaderResult(correlationId));
        }
Esempio n. 12
0
        public async Task Run(
            [DurableClient] IDurableOrchestrationClient client,
            [ActivityTrigger] IDurableActivityContext context)
        {
            _correlationInitializer.Initialize(context.InstanceId);

            var command      = context.GetInput <UploadCvCommand>();
            var saveCvResult = await _fileWriter.Write(
                FileStore.CvsContainer,
                command.Content,
                command.ContentType,
                _fileNameProvider.GetFileName(context.InstanceId, command.Extension));

            if (!saveCvResult.Success)
            {
                var failedEvent = new CvUploadFailedInternalFunctionEvent(saveCvResult.Errors);
                await client.RaiseEventAsync(context.InstanceId, nameof(CvUploadFailedInternalFunctionEvent), failedEvent);
            }

            var eventToDispatch = new CvUploadedInternalFunctionEvent(saveCvResult.Value);
            await client.RaiseEventAsync(context.InstanceId, nameof(CvUploadedInternalFunctionEvent), eventToDispatch);
        }
Esempio n. 13
0
        public async Task EventNotificationHandler(
            [EventGridTrigger] EventGridEvent @event,
            [SignalR(HubName = NOTIFICATION_HUB_NAME)] IAsyncCollector <SignalRMessage> signalRMessages)
        {
            _correlationInitializer.Initialize(@event.Subject);
            var messageToPushResult =
                await _notificationFromEventBuilder.BuildNotification(@event.EventType, @event.Data as string);

            if (!messageToPushResult.Success)
            {
                return;
            }

            var signalRMessage = new SignalRMessage
            {
                Target    = nameof(messageToPushResult),
                UserId    = messageToPushResult.Value.To,
                Arguments = new object[] { messageToPushResult.Value.Content },
            };

            await signalRMessages.AddAsync(signalRMessage);
        }
Esempio n. 14
0
        public async Task Run(
            [EventGridTrigger] EventGridEvent eventGridEvent,
            [DurableClient] IDurableEntityClient client)
        {
            _correlationInitializer.Initialize(eventGridEvent.Subject);
            var dispatchedEvents = await _eventDispatcher.Dispatch(eventGridEvent);

            foreach (var dispatchedEvent in dispatchedEvents)
            {
                var shouldQueueResult =
                    await _queueingEventGuard.ShouldQueueEvent(dispatchedEvent.Content);

                if (shouldQueueResult.Success)
                {
                    await client.SignalEntityAsync <ICategoryEntity>(shouldQueueResult.Value,
                                                                     p => p.QueueEvent(new PendingEvent(
                                                                                           eventGridEvent.Data as string,
                                                                                           dispatchedEvent.Type,
                                                                                           eventGridEvent.EventTime)));
                }
            }
        }
        public async Task RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            ILogger log)
        {
            _correlationInitializer.Initialize(context.InstanceId);
            await context.CallActivityAsync(nameof(StartReindex), null);

            var categoryIds =
                await context.CallActivityAsync <IReadOnlyCollection <string> >(nameof(ReadCategoriesToRebuild), null);

            var rebuildTasks =
                categoryIds.Select(id => context.CreateEntityProxy <ICategoryEntity>(id).Reindex());

            await Task.WhenAll(rebuildTasks);

            log.LogProgress(OperationStatus.InProgress, "Rebuild of events stored in event store finished", context.InstanceId);

            categoryIds =
                await context.CallActivityAsync <IReadOnlyCollection <string> >(nameof(ReadCategoriesToRebuild), null);


            var applyEventTasks =
                categoryIds.Select(id => context.CreateEntityProxy <ICategoryEntity>(id).ApplyPendingEvents());

            await Task.WhenAll(applyEventTasks);

            log.LogProgress(OperationStatus.InProgress, "Applied pending events", context.InstanceId);


            var deleteStateTasks =
                categoryIds.Select(id => context.CreateEntityProxy <ICategoryEntity>(id).Delete());

            await Task.WhenAll(deleteStateTasks);

            await context.CallActivityAsync(nameof(FinishReindex), null);

            log.LogProgress(OperationStatus.Finished, string.Empty, context.InstanceId);
        }
Esempio n. 16
0
        public async Task Run(
            [DurableClient] IDurableOrchestrationClient client,
            [ActivityTrigger] IDurableActivityContext context,
            ILogger log)
        {
            _correlationInitializer.Initialize(context.InstanceId);
            var command = context.GetInput <CreateApplicationCommand>();

            var applicationCreatedEvent = _mapper.Map <CreateApplicationCommand, CreateApplicationProcessStartedEvent>(command);

            var result = await _eventStore.AppendEvent(applicationCreatedEvent.Id, applicationCreatedEvent);

            if (!result.Success)
            {
                var applicationSaveFailedEvent = new ApplicationSaveFailedInternalFunctionEvent(result.Errors);
                await client.RaiseEventAsync(context.InstanceId, nameof(ApplicationSaveFailedInternalFunctionEvent), applicationSaveFailedEvent);
            }

            log.LogProgress(OperationStatus.InProgress, "Application accepted", context.InstanceId);
            await _eventPublisher.PublishEvent(applicationCreatedEvent);

            var applicationSavedEvent = new ApplicationSavedInternalFunctionEvent();
            await client.RaiseEventAsync(context.InstanceId, nameof(ApplicationSavedInternalFunctionEvent), applicationSavedEvent);
        }
Esempio n. 17
0
        public async Task Run(
            [DurableClient] IDurableOrchestrationClient client,
            [ActivityTrigger] IDurableActivityContext context,
            ILogger log)
        {
            _correlationInitializer.Initialize(context.InstanceId);
            var command = context.GetInput <UploadPhotoCommand>();

            var photoSaveResult = await _fileWriter.Write(
                FileStore.PhotosContainer,
                command.Content,
                command.ContentType,
                _fileNameProvider.GetFileName(context.InstanceId, command.Extension));

            if (!photoSaveResult.Success)
            {
                log.LogError($"Uploading photo failed instanceId: {context.InstanceId}", photoSaveResult.Errors, context.InstanceId);
                var failedEvent = new CvUploadFailedInternalFunctionEvent(photoSaveResult.Errors);
                await client.RaiseEventAsync(context.InstanceId, nameof(CvUploadFailedInternalFunctionEvent), failedEvent);
            }

            var eventToDispatch = new PhotoUploadedInternalFunctionEvent(photoSaveResult.Value);
            await client.RaiseEventAsync(context.InstanceId, nameof(PhotoUploadedInternalFunctionEvent), eventToDispatch);
        }
        public async Task RunOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            [DurableClient] IDurableOrchestrationClient processStarter,
            ILogger log)
        {
            _correlationInitializer.Initialize(context.InstanceId);

            var beginProcessCommand = this.BuildBeginProcessCommand(context);
            await context.CallActivityAsync <Task>(nameof(StatusTracker), beginProcessCommand);

            var command = context.GetInput <RegisterApplicationCommand>();

            var uploadCvCommand = new UploadCvCommand(
                command.Cv.File,
                command.Cv.ContentType,
                command.Cv.Extension);

            var uploadPhotoCommand = new UploadPhotoCommand(
                command.Photo.File,
                command.Photo.ContentType,
                command.Photo.Extension);

            await Task.WhenAll(
                context.CallActivityAsync <Task>(nameof(CvUploader), uploadCvCommand),
                context.CallActivityAsync <Task>(nameof(PhotoUploader), uploadPhotoCommand));

            var cvUploadedEventTask     = context.WaitForExternalEvent <CvUploadedInternalFunctionEvent>(nameof(CvUploadedInternalFunctionEvent));
            var cvUploadFailedEventTask = context.WaitForExternalEvent <CvUploadFailedInternalFunctionEvent>(nameof(CvUploadFailedInternalFunctionEvent));

            var photoUploadedEventTask     = context.WaitForExternalEvent <PhotoUploadedInternalFunctionEvent>(nameof(PhotoUploadedInternalFunctionEvent));
            var photoUploadFailedEventTask = context.WaitForExternalEvent <PhotoUploadFailedInternalFunctionEvent>(nameof(PhotoUploadFailedInternalFunctionEvent));

            var cvUploadEventTask = await Task.WhenAny(cvUploadedEventTask, cvUploadFailedEventTask);

            var photoUploadEventTask = await Task.WhenAny(photoUploadedEventTask, photoUploadFailedEventTask);

            var cvUploadedSuccessfully    = cvUploadEventTask == cvUploadedEventTask;
            var photoUploadedSuccessfully = photoUploadEventTask == photoUploadedEventTask;

            if (!cvUploadedSuccessfully || !photoUploadedSuccessfully)
            {
                await this.HandleUploadFilesFailure(
                    context,
                    processStarter,
                    log,
                    photoUploadFailedEventTask,
                    cvUploadFailedEventTask,
                    command);

                return;
            }

            log.LogProgress(OperationStatus.InProgress, "Finished the files uploading", context.InstanceId);
            var cvUri    = cvUploadedEventTask.Result.CvUri;
            var photoUri = photoUploadedEventTask.Result.PhotoUri;

            var saveApplicationCommand = new CreateApplicationCommand(
                context.InstanceId,
                command.Candidate.FirstName,
                command.Candidate.LastName,
                photoUri,
                cvUri,
                command.Candidate.Category,
                command.CreationTime,
                command.Candidate.EducationLevel,
                command.Candidate.Address,
                command.Candidate.FinishedSchools,
                command.Candidate.ConfirmedSkills,
                command.Candidate.WorkExperiences,
                context.InstanceId);

            await context.CallActivityAsync <Task>(nameof(ApplicationSaver), saveApplicationCommand);

            var applicationSavedEvent = context.WaitForExternalEvent <ApplicationSavedInternalFunctionEvent>(nameof(ApplicationSavedInternalFunctionEvent));
            var applicationSaveFailed = context.WaitForExternalEvent <ApplicationSaveFailedInternalFunctionEvent>(nameof(ApplicationSaveFailedInternalFunctionEvent));
            var applicationSaveEvent  = await Task.WhenAny(applicationSavedEvent, applicationSaveFailed);

            var applicationSavedSuccessfully = applicationSaveEvent == applicationSavedEvent;

            if (!applicationSavedSuccessfully)
            {
                log.LogFailedOperation(OperationStatus.Failed, "Storing application failed", applicationSaveFailed.Result.Errors, context.InstanceId);
                var failedProcessCommand = this.BuildFailedProcessCommand(context, applicationSaveFailed.Result.Errors);
                await context.CallActivityAsync <Task>(nameof(StatusTracker), failedProcessCommand);

                await this.StartRecompensateProcess(processStarter, context, command, log);

                return;
            }

            var finishProcessCommand = this.BuildFinishedProcessCommand(context);
            await context.CallActivityAsync <Task>(nameof(StatusTracker), finishProcessCommand);
        }