Skip to content

Commit

Permalink
Delete queued tasks if their parent job is cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
drakon64 committed Dec 23, 2024
1 parent 89ebe15 commit 6816d8c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
21 changes: 21 additions & 0 deletions Ktisis.Common/Clients/GoogleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ public static async Task CreateTask(CreateCloudTask task)
await Console.Out.WriteLineAsync(await request.Content.ReadAsStringAsync());
}

public static async Task DeleteTask(string task)
{
var accessToken = await GetAccessToken();

var request = await Ktisis.HttpClient.SendAsync(
new HttpRequestMessage
{
Method = HttpMethod.Delete,
Headers =
{
{ "Authorization", $"{accessToken.TokenType} {accessToken.AccessToken}" },
},
RequestUri = new Uri(
$"https://cloudtasks.googleapis.com/v2/projects/{Project}/locations/{Region}/queues/{Queue}/tasks/{task}"
),
}
);

await Console.Out.WriteLineAsync(await request.Content.ReadAsStringAsync());
}

public static async Task<ZoneOperation> CreateInstance(CreateInstance instance, string zone)
{
var accessToken = await GetAccessToken();
Expand Down
31 changes: 18 additions & 13 deletions Ktisis.Receiver/EventProcessors/GitHubWebhookEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Octokit.Webhooks;
using Octokit.Webhooks.Events;
using Octokit.Webhooks.Events.WorkflowJob;
using Octokit.Webhooks.Models.WorkflowJobEvent;

namespace Ktisis.Receiver.EventProcessors;

Expand Down Expand Up @@ -42,6 +43,9 @@ await Console.Out.WriteLineAsync(
return;
}

var instanceName =
$"{workflowJobEvent.Repository!.FullName.Replace('/', '-')}-{workflowJobEvent.WorkflowJob.RunId}-{workflowJobEvent.WorkflowJob.Id}";

switch (workflowJobEvent.Action)
{
case "queued":
Expand Down Expand Up @@ -96,16 +100,13 @@ await Console.Out.WriteLineAsync(
}
}

var name =
$"{workflowJobEvent.Repository!.FullName.Replace('/', '-')}-{workflowJobEvent.WorkflowJob.RunId}-{workflowJobEvent.WorkflowJob.Id}";

await GoogleClient.CreateTask(
new CreateCloudTask
{
Task = new CloudTask
{
Name =
$"projects/{Program.Project}/locations/{Program.Region}/queues/{Program.Queue}/tasks/{Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(name)))}",
$"projects/{Program.Project}/locations/{Program.Region}/queues/{Program.Queue}/tasks/{Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(instanceName)))}",
HttpRequest = new CloudTaskHttpRequest
{
Url = Program.TaskServiceUrl,
Expand All @@ -117,7 +118,7 @@ await GoogleClient.CreateTask(
Zone = zone,
Instance = new CreateInstance
{
Name = name,
Name = instanceName,
MachineType =
$"projects/{GoogleClient.Project}/zones/{zone}/machineTypes/{machineType}",
NetworkInterfaces =
Expand Down Expand Up @@ -223,14 +224,18 @@ rm actions-runner-linux-{runnerArchitecture}-2.321.0.tar.gz
}
case "completed":
{
// TODO: Get the instance zone rather than brute-forcing
foreach (var zone in Program.Zones)
{
await GoogleClient.DeleteInstance(
workflowJobEvent.WorkflowJob.RunnerName!,
zone
);
}
if (
workflowJobEvent.WorkflowJob.Conclusion!.Value
== WorkflowJobConclusion.Cancelled
)
await GoogleClient.DeleteTask(instanceName);
else
// TODO: Get the instance zone rather than brute-forcing
foreach (var zone in Program.Zones)
await GoogleClient.DeleteInstance(
workflowJobEvent.WorkflowJob.RunnerName!,
zone
);

break;
}
Expand Down
7 changes: 6 additions & 1 deletion tofu/ktisis/cloud_tasks.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ resource "google_cloud_tasks_queue" "cloud_tasks" {
}

resource "google_cloud_tasks_queue_iam_member" "ktisis" {
for_each = toset([
"enqueuer",
"taskDeleter",
])

member = google_service_account.ktisis_receiver.member
name = google_cloud_tasks_queue.cloud_tasks.name
role = "roles/cloudtasks.enqueuer"
role = "roles/cloudtasks.${each.value}"
}

0 comments on commit 6816d8c

Please sign in to comment.