Skip to content

Commit f88ace0

Browse files
authored
Move EF trigger to its own file (#4430)
Follows better what we do for other configuration and cleans up the startup file where we go quite often. <!-- #4430 -->
1 parent 5e4e719 commit f88ace0

File tree

2 files changed

+63
-55
lines changed

2 files changed

+63
-55
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using EntityFrameworkCore.Triggers;
5+
using Maestro.Data;
6+
using Maestro.Data.Models;
7+
using Microsoft.EntityFrameworkCore;
8+
using Microsoft.EntityFrameworkCore.Infrastructure;
9+
using ProductConstructionService.DependencyFlow.WorkItems;
10+
using ProductConstructionService.WorkItems;
11+
12+
namespace ProductConstructionService.Api.Configuration;
13+
14+
internal static class SubscriptionTriggerConfiguration
15+
{
16+
public static void TriggerSubscriptionOnNewBuild(IInsertedEntry<BuildChannel, DbContext> entry)
17+
{
18+
var context = (BuildAssetRegistryContext)entry.Context;
19+
ILogger<BuildAssetRegistryContext> logger = context.GetService<ILogger<BuildAssetRegistryContext>>();
20+
var workItemProducerFactory = context.GetService<IWorkItemProducerFactory>();
21+
BuildChannel entity = entry.Entity;
22+
23+
Build? build = context.Builds
24+
.Include(b => b.Assets)
25+
.ThenInclude(a => a.Locations)
26+
.FirstOrDefault(b => b.Id == entity.BuildId);
27+
28+
if (build == null)
29+
{
30+
logger.LogError("Could not find build with id {buildId} in BAR. Skipping dependency update.", entity.BuildId);
31+
}
32+
else
33+
{
34+
var hasAssetsWithPublishedLocations = build.Assets
35+
.Any(a => a.Locations.Any(al => al.Type != LocationType.None && !al.Location.EndsWith("/artifacts")));
36+
37+
if (!hasAssetsWithPublishedLocations)
38+
{
39+
logger.LogInformation("Skipping Dependency update for Build {buildId} because it contains no assets in valid locations", entity.BuildId);
40+
return;
41+
}
42+
43+
var subscriptionsToUpdate = context.Subscriptions
44+
.Where(sub =>
45+
sub.Enabled &&
46+
sub.ChannelId == entity.ChannelId &&
47+
(sub.SourceRepository == entity.Build.GitHubRepository || sub.SourceDirectory == entity.Build.AzureDevOpsRepository) &&
48+
JsonExtensions.JsonValue(sub.PolicyString, "lax $.UpdateFrequency") == ((int)UpdateFrequency.EveryBuild).ToString())
49+
.ToList();
50+
51+
foreach (Subscription subscription in subscriptionsToUpdate)
52+
{
53+
var workItemProducer = workItemProducerFactory.CreateProducer<SubscriptionTriggerWorkItem>(subscription.SourceEnabled);
54+
workItemProducer.ProduceWorkItemAsync(new()
55+
{
56+
BuildId = entity.BuildId,
57+
SubscriptionId = subscription.Id
58+
}).GetAwaiter().GetResult();
59+
}
60+
}
61+
}
62+
}

src/ProductConstructionService/ProductConstructionService.Api/PcsStartup.cs

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Globalization;
55
using System.Net;
6-
using System.Reflection;
76
using System.Text;
87
using Azure.Identity;
98
using EntityFrameworkCore.Triggers;
@@ -21,8 +20,6 @@
2120
using Microsoft.DotNet.GitHub.Authentication;
2221
using Microsoft.DotNet.Internal.Logging;
2322
using Microsoft.DotNet.Services.Utility;
24-
using Microsoft.EntityFrameworkCore;
25-
using Microsoft.EntityFrameworkCore.Infrastructure;
2623
using Microsoft.Extensions.Options;
2724
using Newtonsoft.Json;
2825
using Newtonsoft.Json.Converters;
@@ -33,7 +30,6 @@
3330
using ProductConstructionService.Api.Telemetry;
3431
using ProductConstructionService.Api.VirtualMonoRepo;
3532
using ProductConstructionService.Common;
36-
using ProductConstructionService.DependencyFlow.WorkItems;
3733
using ProductConstructionService.WorkItems;
3834
using ProductConstructionService.DependencyFlow;
3935
using ProductConstructionService.ServiceDefaults;
@@ -76,57 +72,7 @@ private static class ConfigurationKeys
7672

7773
static PcsStartup()
7874
{
79-
var metadata = typeof(Program).Assembly
80-
.GetCustomAttributes()
81-
.OfType<AssemblyMetadataAttribute>()
82-
.ToDictionary(m => m.Key, m => m.Value);
83-
84-
Triggers<BuildChannel>.Inserted += entry =>
85-
{
86-
var context = (BuildAssetRegistryContext)entry.Context;
87-
ILogger<BuildAssetRegistryContext> logger = context.GetService<ILogger<BuildAssetRegistryContext>>();
88-
var workItemProducerFactory = context.GetService<IWorkItemProducerFactory>();
89-
BuildChannel entity = entry.Entity;
90-
91-
Build? build = context.Builds
92-
.Include(b => b.Assets)
93-
.ThenInclude(a => a.Locations)
94-
.FirstOrDefault(b => b.Id == entity.BuildId);
95-
96-
if (build == null)
97-
{
98-
logger.LogError("Could not find build with id {buildId} in BAR. Skipping dependency update.", entity.BuildId);
99-
}
100-
else
101-
{
102-
bool hasAssetsWithPublishedLocations = build.Assets
103-
.Any(a => a.Locations.Any(al => al.Type != LocationType.None && !al.Location.EndsWith("/artifacts")));
104-
105-
if (!hasAssetsWithPublishedLocations)
106-
{
107-
logger.LogInformation("Skipping Dependency update for Build {buildId} because it contains no assets in valid locations", entity.BuildId);
108-
return;
109-
}
110-
111-
List<Subscription> subscriptionsToUpdate = context.Subscriptions
112-
.Where(sub =>
113-
sub.Enabled &&
114-
sub.ChannelId == entity.ChannelId &&
115-
(sub.SourceRepository == entity.Build.GitHubRepository || sub.SourceDirectory == entity.Build.AzureDevOpsRepository) &&
116-
JsonExtensions.JsonValue(sub.PolicyString, "lax $.UpdateFrequency") == ((int)UpdateFrequency.EveryBuild).ToString())
117-
.ToList();
118-
119-
foreach (Subscription subscription in subscriptionsToUpdate)
120-
{
121-
var workItemProducer = workItemProducerFactory.CreateProducer<SubscriptionTriggerWorkItem>(subscription.SourceEnabled);
122-
workItemProducer.ProduceWorkItemAsync(new()
123-
{
124-
BuildId = entity.BuildId,
125-
SubscriptionId = subscription.Id
126-
}).GetAwaiter().GetResult();
127-
}
128-
}
129-
};
75+
Triggers<BuildChannel>.Inserted += SubscriptionTriggerConfiguration.TriggerSubscriptionOnNewBuild;
13076
}
13177

13278
/// <summary>

0 commit comments

Comments
 (0)