Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

C# Refactor For Webhook Queue #1818

Merged
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
7369907
Working on Webhook Queue Changes.
nharper285 Apr 20, 2022
0f3aced
Merge branch 'main' into user/noharper/refactor-webhook-queue
nharper285 Apr 20, 2022
0a4d45e
Initial Push w/ Webhook Work.
nharper285 Apr 21, 2022
e1316e0
Merge branch 'main' into user/noharper/refactor-webhook-queue
nharper285 Apr 21, 2022
9710dae
Initial Push. Working on Testing.
nharper285 Apr 22, 2022
044044f
Merge branch 'main' into user/noharper/refactor-webhook-queue
nharper285 Apr 22, 2022
139005b
Merge branch 'main' into user/noharper/refactor-webhook-queue
nharper285 Apr 22, 2022
2c389ce
Moving InstanceId
nharper285 Apr 22, 2022
73b34a7
Removing primitives.
nharper285 Apr 22, 2022
eadd820
Formatting.
nharper285 Apr 22, 2022
ce083d6
Fixing formatting?
nharper285 Apr 22, 2022
16b16df
Moving GetInstanceId to containers.
nharper285 Apr 22, 2022
2c10381
Moving comments for formatting.
nharper285 Apr 22, 2022
e96c884
Removing unused dependency.
nharper285 Apr 22, 2022
453e945
Comments.
nharper285 Apr 22, 2022
31b4a57
Add WebhookEventGrid test.
nharper285 Apr 22, 2022
19f9a3d
Fixing how tests work.
nharper285 Apr 22, 2022
67c5155
Merge branch 'main' into user/noharper/refactor-webhook-queue
nharper285 Apr 25, 2022
6fd0a58
Working to resolve conflicts.
nharper285 Apr 25, 2022
11ded44
Resolving conflicts.
nharper285 Apr 25, 2022
3cb42b5
Merge branch 'main' into user/noharper/refactor-webhook-queue
nharper285 Apr 25, 2022
3348ff8
Fixing chagnes.
nharper285 Apr 25, 2022
528c15f
Merge branch 'main' into user/noharper/refactor-webhook-queue
nharper285 Apr 26, 2022
f8d8caa
Tested code.
nharper285 Apr 26, 2022
099c77d
Merge branch 'main' into user/noharper/refactor-webhook-queue
nharper285 Apr 26, 2022
ddb3373
Formatting.
nharper285 Apr 26, 2022
8998810
Merge branch 'user/noharper/refactor-webhook-queue' of https://github…
nharper285 Apr 26, 2022
c79c59d
MoreFormatting.
nharper285 Apr 26, 2022
79b05d6
More formatting.
nharper285 Apr 26, 2022
78da0dd
Fixing syntax.
nharper285 Apr 26, 2022
1cc3d8a
Fixing syntax.
nharper285 Apr 26, 2022
72a39c3
Removing test and webhookmessagelogoperation class.
nharper285 Apr 26, 2022
ad9e588
Using config.
nharper285 Apr 26, 2022
021778c
Merge branch 'main' into user/noharper/refactor-webhook-queue
nharper285 Apr 26, 2022
be3bb3c
Fixing ProxyOperations.
nharper285 Apr 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ApiService/ApiService/ApiService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
</Project>
6 changes: 3 additions & 3 deletions src/ApiService/ApiService/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ public async Task<HttpResponseMessage> Delete(Uri url) {
public async Task<HttpResponseMessage> Post(Uri url, String json, IDictionary<string, string>? headers = null) {
using var b = new StringContent(json);
b.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
return await Send(method: HttpMethod.Post, url: url, headers: headers);
return await Send(method: HttpMethod.Post, content: b, url: url, headers: headers);
}

public async Task<HttpResponseMessage> Put(Uri url, String json, IDictionary<string, string>? headers = null) {
using var b = new StringContent(json);
b.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
return await Send(method: HttpMethod.Put, url: url, headers: headers);
return await Send(method: HttpMethod.Put, content: b, url: url, headers: headers);
}

public async Task<HttpResponseMessage> Patch(Uri url, String json, IDictionary<string, string>? headers = null) {
using var b = new StringContent(json);
b.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
return await Send(method: HttpMethod.Patch, url: url, headers: headers);
return await Send(method: HttpMethod.Patch, content: b, url: url, headers: headers);
}
}
2 changes: 1 addition & 1 deletion src/ApiService/ApiService/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public interface ILogTracer {
void Critical(string message);
void Error(string message);
void Event(string evt, IReadOnlyDictionary<string, double>? metrics);
void Exception(Exception ex, IReadOnlyDictionary<string, double>? metrics);
void Exception(Exception ex, IReadOnlyDictionary<string, double>? metrics = null);
void ForceFlush();
void Info(string message);
void Warning(string message);
Expand Down
12 changes: 6 additions & 6 deletions src/ApiService/ApiService/OneFuzzTypes/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public EventType GetEventType() {
this switch {
EventNodeHeartbeat _ => EventType.NodeHeartbeat,
EventTaskHeartbeat _ => EventType.TaskHeartbeat,
EventPing _ => EventType.Ping,
EventInstanceConfigUpdated _ => EventType.InstanceConfigUpdated,
EventProxyCreated _ => EventType.ProxyCreated,
EventProxyDeleted _ => EventType.ProxyDeleted,
Expand All @@ -66,6 +67,7 @@ public static Type GetTypeInfo(EventType eventType) {
EventType.NodeHeartbeat => typeof(EventNodeHeartbeat),
EventType.InstanceConfigUpdated => typeof(EventInstanceConfigUpdated),
EventType.TaskHeartbeat => typeof(EventTaskHeartbeat),
EventType.Ping => typeof(EventPing),
EventType.ProxyCreated => typeof(EventProxyCreated),
EventType.ProxyDeleted => typeof(EventProxyDeleted),
EventType.ProxyFailed => typeof(EventProxyFailed),
Expand Down Expand Up @@ -151,11 +153,9 @@ public record EventTaskHeartbeat(
TaskConfig Config
) : BaseEvent();


//record EventPing(
// PingId: Guid
//): BaseEvent();

public record EventPing(
Guid PingId
) : BaseEvent();

//record EventScalesetCreated(
// Guid ScalesetId,
Expand Down Expand Up @@ -295,7 +295,7 @@ public record EventMessage(
BaseEvent Event,
Guid InstanceId,
String InstanceName
) : EntityBase();
);

public class BaseEventConverter : JsonConverter<BaseEvent> {
public override BaseEvent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
Expand Down
11 changes: 5 additions & 6 deletions src/ApiService/ApiService/OneFuzzTypes/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ NodeTaskState State


public record Task(
// Timestamp: Optional[datetime] = Field(alias="Timestamp")
[PartitionKey] Guid JobId,
[RowKey] Guid TaskId,
TaskState State,
Expand Down Expand Up @@ -307,13 +306,13 @@ public record ApiAccessRule(
Guid[] AllowedGroups
);

//# initial set of admins can only be set during deployment.
//# if admins are set, only admins can update instance configs.
//# if set, only admins can manage pools or scalesets
public record InstanceConfig
(
[PartitionKey, RowKey] string InstanceName,
//# initial set of admins can only be set during deployment.
//# if admins are set, only admins can update instance configs.
Guid[]? Admins,
//# if set, only admins can manage pools or scalesets
bool? AllowPoolManagement,
string[] AllowedAadTenants,
NetworkConfig NetworkConfig,
Expand Down Expand Up @@ -490,20 +489,20 @@ public record Repro(
UserInfo? UserInfo
) : StatefulEntityBase<VmState>(State);

// TODO: Make this >1 and < 7*24 (more than one hour, less than seven days)
public record ReproConfig(
Container Container,
string Path,
// TODO: Make this >1 and < 7*24 (more than one hour, less than seven days)
int Duration
);

// Skipping AutoScaleConfig because it's not used anymore
public record Pool(
DateTimeOffset Timestamp,
PoolName Name,
Guid PoolId,
Os Os,
bool Managed,
// Skipping AutoScaleConfig because it's not used anymore
Architecture Architecture,
PoolState State,
Guid? ClientId,
Expand Down
21 changes: 10 additions & 11 deletions src/ApiService/ApiService/OneFuzzTypes/Webhooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public enum WebhookMessageFormat {
EventGrid
}

public record WebhookMessageQueueObj(
Guid WebhookId,
Guid EventId
);

public record WebhookMessage(Guid EventId,
EventType EventType,
BaseEvent Event,
Expand All @@ -27,24 +32,18 @@ public record WebhookMessageEventGrid(
[property: JsonConverter(typeof(BaseEventConverter))]
BaseEvent Data);


// TODO: This should inherit from Entity Base ? no, since there is
// a table WebhookMessaageLog
public record WebhookMessageLog(
[RowKey] Guid EventId,
EventType EventType,
[property: TypeDiscrimnatorAttribute("EventType", typeof(EventTypeProvider))]
[property: JsonConverter(typeof(BaseEventConverter))]
BaseEvent Event,
Guid InstanceId,
String InstanceName,
[PartitionKey] Guid WebhookId,
WebhookMessageState State = WebhookMessageState.Queued,
int TryCount = 0
) : WebhookMessage(EventId,
EventType,
Event,
InstanceId,
InstanceName,
WebhookId);
long TryCount,
WebhookMessageState State = WebhookMessageState.Queued
) : EntityBase();

public record Webhook(
[PartitionKey] Guid WebhookId,
Expand Down
6 changes: 2 additions & 4 deletions src/ApiService/ApiService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public static List<ILog> GetLoggers(IServiceConfig config) {
return loggers;
}


//Move out expensive resources into separate class, and add those as Singleton
// ArmClient, Table Client(s), Queue Client(s), HttpClient, etc.
public static void Main() {
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(
Expand Down Expand Up @@ -78,9 +79,6 @@ public static void Main() {
.AddScoped<IVmOperations, VmOperations>()
.AddScoped<ISecretsOperations, SecretsOperations>()
.AddScoped<IJobOperations, JobOperations>()

//Move out expensive resources into separate class, and add those as Singleton
// ArmClient, Table Client(s), Queue Client(s), HttpClient, etc.
.AddSingleton<ICreds, Creds>()
.AddSingleton<IServiceConfig, ServiceConfiguration>()
.AddHttpClient()
Expand Down
24 changes: 24 additions & 0 deletions src/ApiService/ApiService/QueueWebhooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Text.Json;
using Microsoft.Azure.Functions.Worker;
using Microsoft.OneFuzz.Service.OneFuzzLib.Orm;

namespace Microsoft.OneFuzz.Service;

public class QueueWebhooks {
private readonly ILogTracer _log;
private readonly IWebhookMessageLogOperations _webhookMessageLog;
public QueueWebhooks(ILogTracer log, IWebhookMessageLogOperations webhookMessageLog) {
_log = log;
_webhookMessageLog = webhookMessageLog;
}

[Function("QueueWebhooks")]
public async Async.Task Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string msg) {

_log.Info($"Webhook Message Queued: {msg}");

var obj = JsonSerializer.Deserialize<WebhookMessageQueueObj>(msg, EntityConverter.GetJsonSerializerOptions()).EnsureNotNull($"wrong data {msg}");

await _webhookMessageLog.ProcessFromQueue(obj);
}
}
Loading