private async Task MarkScheduledReleaseStatusAsSuperseded(NotifyChangeMessage message)
        {
            // There may be an existing scheduled ReleaseStatus entry if this release has been validated before
            // If so, mark it as superseded
            var scheduled = await _releaseStatusService.GetAllByOverallStage(message.ReleaseId, Scheduled);

            foreach (var releaseStatus in scheduled)
            {
                await _releaseStatusService.UpdateStateAsync(message.ReleaseId, releaseStatus.Id, SupersededState);
            }
        }
        // ReSharper disable once UnusedMember.Global
        public async Task NotifyChange(
            [QueueTrigger(NotifyChangeQueue)] NotifyChangeMessage message,
            ExecutionContext executionContext,
            ILogger logger)
        {
            logger.LogInformation("{0} triggered: {1}",
                                  executionContext.FunctionName,
                                  message);
            var lease = await _fileStorageService.AcquireLease(message.ReleaseId.ToString());

            try
            {
                await MarkScheduledReleaseStatusAsSuperseded(message);

                if (await _validationService.ValidatePublishingState(message.ReleaseId))
                {
                    await _validationService.ValidateRelease(message.ReleaseId)
                    .OnSuccessDo(async() =>
                    {
                        if (message.Immediate)
                        {
                            var releaseStatus =
                                await CreateReleaseStatusAsync(message, ImmediateReleaseStartedState);
                            await _queueService.QueuePublishReleaseFilesMessageAsync(releaseStatus.ReleaseId,
                                                                                     releaseStatus.Id);
                        }
                        else
                        {
                            await CreateReleaseStatusAsync(message, ScheduledState);
                        }
                    })
                    .OnFailureDo(async logMessages =>
                    {
                        await CreateReleaseStatusAsync(message, InvalidState, logMessages);
                    });
                }
            }
            finally
            {
                await lease.Release();
            }

            logger.LogInformation("{0} completed",
                                  executionContext.FunctionName);
        }
 private async Task <ReleaseStatus> CreateReleaseStatusAsync(NotifyChangeMessage message,
                                                             ReleaseStatusState state, IEnumerable <ReleaseStatusLogMessage> logMessages = null)
 {
     return(await _releaseStatusService.CreateAsync(message.ReleaseId, state, message.Immediate, logMessages));
 }