Add queue hardening by modifying DeploymentJobTargets, DeploymentJobSteps, and DeploymentJobs tables
- Drop existing indexes on DeploymentJobTargets and DeploymentJobSteps - Alter Status column to nvarchar(450) in DeploymentJobTargets, DeploymentJobSteps, and DeploymentJobs - Add new columns: Finished, OutputMetadataJson, Started to DeploymentJobTargets; ErrorMessage, Finished, OutputMetadataJson, Started to DeploymentJobSteps; CorrelationId, HeartbeatAt, Priority, RowVersion, ScheduledAt, WorkerName to DeploymentJobs - Create new indexes for improved query performance - Add constraints to ensure OutputMetadataJson is valid JSON - Record migration in __EFMigrationsHistory
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using Microsoft.SelfService.Portal.Core.API.Context;
|
||||
using Microsoft.SelfService.Portal.Core.API.Interfaces;
|
||||
using Microsoft.SelfService.Portal.Core.API.Models;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
{
|
||||
@@ -16,7 +17,12 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
|
||||
public ICollection<DeploymentGroupModel> GetDeploymentBatches()
|
||||
{
|
||||
return _context.DeploymentGroups.ToList();
|
||||
return _context.DeploymentGroups
|
||||
.Include(batch => batch.TemplateSelections)
|
||||
.ThenInclude(selection => selection.TemplateVersion)
|
||||
.ThenInclude(version => version.Template)
|
||||
.Include(batch => batch.TargetAssignments)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public DeploymentGroupModel? GetDeploymentBatchById(Guid id)
|
||||
@@ -42,16 +48,33 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
deploymentBatch.Id = Guid.NewGuid();
|
||||
}
|
||||
|
||||
var template = _context.Templates
|
||||
.Include(existing => existing.TemplateCategory)
|
||||
.ThenInclude(existing => existing.Service)
|
||||
.FirstOrDefault(existing => existing.Id == deploymentBatch.TemplateId);
|
||||
var requestedTemplateVersionId = deploymentBatch.TemplateSelections
|
||||
.OrderBy(selection => selection.SortOrder)
|
||||
.Select(selection => (Guid?)selection.TemplateVersionId)
|
||||
.FirstOrDefault();
|
||||
|
||||
var templateVersion = requestedTemplateVersionId.HasValue
|
||||
? _context.TemplateVersions
|
||||
.Include(version => version.Template)
|
||||
.ThenInclude(template => template.TemplateCategory)
|
||||
.ThenInclude(category => category.Service)
|
||||
.FirstOrDefault(version => version.Id == requestedTemplateVersionId.Value)
|
||||
: null;
|
||||
|
||||
var template = templateVersion?.Template
|
||||
?? _context.Templates
|
||||
.Include(existing => existing.TemplateCategory)
|
||||
.ThenInclude(existing => existing.Service)
|
||||
.Include(existing => existing.TemplateVersions)
|
||||
.FirstOrDefault(existing => existing.Id == deploymentBatch.TemplateId);
|
||||
|
||||
if (template == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
deploymentBatch.TemplateId = template.Id;
|
||||
|
||||
if (deploymentBatch.DeploymentRuleId.HasValue)
|
||||
{
|
||||
var ruleExists = _context.DeploymentRules.Any(existing => existing.Id == deploymentBatch.DeploymentRuleId.Value);
|
||||
@@ -63,6 +86,7 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
|
||||
var isCloudService = template.TemplateCategory.Service.IsCloudService;
|
||||
var selectedTargetIds = (targetIds ?? Array.Empty<Guid>())
|
||||
.Concat(deploymentBatch.TargetAssignments.Select(assignment => assignment.TargetId))
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
@@ -86,6 +110,53 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
|
||||
_context.Add(deploymentBatch);
|
||||
|
||||
if (deploymentBatch.TemplateSelections.Count == 0)
|
||||
{
|
||||
templateVersion ??= template.TemplateVersions?
|
||||
.OrderByDescending(version => version.IsPublished)
|
||||
.ThenByDescending(version => version.PublishedAt)
|
||||
.ThenByDescending(version => version.Created)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (templateVersion == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
deploymentBatch.TemplateSelections.Add(new DeploymentTemplateSelectionModel
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
DeploymentGroupId = deploymentBatch.Id,
|
||||
TemplateVersionId = templateVersion.Id,
|
||||
TemplateRole = "Service",
|
||||
SortOrder = 10,
|
||||
Alias = template.Name
|
||||
});
|
||||
}
|
||||
|
||||
if (deploymentBatch.TargetAssignments.Count == 0)
|
||||
{
|
||||
var sortOrder = 10;
|
||||
foreach (var targetId in selectedTargetIds)
|
||||
{
|
||||
var targetName = _context.Targets
|
||||
.Where(target => target.Id == targetId)
|
||||
.Select(target => target.Name)
|
||||
.FirstOrDefault();
|
||||
|
||||
deploymentBatch.TargetAssignments.Add(new DeploymentTargetAssignmentModel
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
DeploymentGroupId = deploymentBatch.Id,
|
||||
TargetId = targetId,
|
||||
RoleKey = "Node",
|
||||
SortOrder = sortOrder,
|
||||
NodeDataJson = JsonSerializer.Serialize(new { nodeName = targetName ?? targetId.ToString() })
|
||||
});
|
||||
sortOrder += 10;
|
||||
}
|
||||
}
|
||||
|
||||
if (!SaveChanges())
|
||||
{
|
||||
return false;
|
||||
@@ -112,10 +183,34 @@ namespace Microsoft.SelfService.Portal.Core.API.Repository
|
||||
|
||||
public bool DeleteDeploymentBatchById(DeploymentGroupModel deploymentBatch)
|
||||
{
|
||||
var templateSelections = _context.DeploymentTemplateSelections
|
||||
.Where(existing => existing.DeploymentGroupId == deploymentBatch.Id)
|
||||
.ToList();
|
||||
var parameterValues = _context.DeploymentParameterValues
|
||||
.Where(existing => existing.DeploymentGroupId == deploymentBatch.Id)
|
||||
.ToList();
|
||||
var targetAssignments = _context.DeploymentTargetAssignments
|
||||
.Where(existing => existing.DeploymentGroupId == deploymentBatch.Id)
|
||||
.ToList();
|
||||
var deployments = _context.Deployments
|
||||
.Where(existing => existing.DeploymentGroupId == deploymentBatch.Id)
|
||||
.ToList();
|
||||
|
||||
if (parameterValues.Count > 0)
|
||||
{
|
||||
_context.DeploymentParameterValues.RemoveRange(parameterValues);
|
||||
}
|
||||
|
||||
if (targetAssignments.Count > 0)
|
||||
{
|
||||
_context.DeploymentTargetAssignments.RemoveRange(targetAssignments);
|
||||
}
|
||||
|
||||
if (templateSelections.Count > 0)
|
||||
{
|
||||
_context.DeploymentTemplateSelections.RemoveRange(templateSelections);
|
||||
}
|
||||
|
||||
if (deployments.Count > 0)
|
||||
{
|
||||
_context.Deployments.RemoveRange(deployments);
|
||||
|
||||
Reference in New Issue
Block a user