Skip to content

Commit 6f7233f

Browse files
refactor and small deserializer fix
1 parent 71b848a commit 6f7233f

40 files changed

Lines changed: 376 additions & 300 deletions

CloudBan.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace GithubActionsOrchestrator;
2+
3+
internal class CloudBan
4+
{
5+
public string Cloud { get; set; }
6+
public string Size { get; set; }
7+
public DateTime UnbanTime { get; set; }
8+
}

CloudControllers/CspServer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace GithubActionsOrchestrator.CloudControllers;
2+
3+
public class CspServer
4+
{
5+
public long Id { get; set; }
6+
public string Name { get; set; }
7+
public DateTime CreatedAt { get; set; }
8+
}

CloudControllers/HetznerCloudController.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,4 @@ public async Task<int> GetServerCountFromCsp()
187187
}
188188

189189
public string CloudIdentifier => "htz";
190-
}
191-
192-
public class UnsupportedMachineTypeException : Exception
193-
{
194-
public UnsupportedMachineTypeException()
195-
{
196-
}
197-
198-
public UnsupportedMachineTypeException(string message) : base(message)
199-
{
200-
}
201-
202-
public UnsupportedMachineTypeException(string message, Exception inner) : base(message, inner)
203-
{
204-
}
205-
}
190+
}

CloudControllers/ICloudController.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,4 @@ public interface ICloudController
1111
Task<int> GetServerCountFromCsp();
1212

1313
string CloudIdentifier { get; }
14-
}
15-
16-
public class CspServer
17-
{
18-
public long Id { get; set; }
19-
public string Name { get; set; }
20-
public DateTime CreatedAt { get; set; }
2114
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace GithubActionsOrchestrator.CloudControllers;
2+
3+
public class UnsupportedMachineTypeException : Exception
4+
{
5+
public UnsupportedMachineTypeException()
6+
{
7+
}
8+
9+
public UnsupportedMachineTypeException(string message) : base(message)
10+
{
11+
}
12+
13+
public UnsupportedMachineTypeException(string message, Exception inner) : base(message, inner)
14+
{
15+
}
16+
}

Database/ActionsRunnerContext.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.ComponentModel.DataAnnotations.Schema;
2+
3+
namespace GithubActionsOrchestrator.Database;
4+
using Microsoft.EntityFrameworkCore;
5+
using System;
6+
7+
public class ActionsRunnerContext()
8+
: DbContext
9+
{
10+
public DbSet<Runner> Runners { get; set; }
11+
public DbSet<Job> Jobs { get; set; }
12+
public DbSet<RunnerLifecycle> RunnerLifecycles { get; set; }
13+
14+
15+
16+
protected override void OnConfiguring(DbContextOptionsBuilder options)
17+
=> options.UseNpgsql(Program.Config.DbConnectionString);
18+
19+
protected override void OnModelCreating(ModelBuilder modelBuilder)
20+
{
21+
modelBuilder.Entity<Runner>()
22+
.HasOne(r => r.Job)
23+
.WithOne()
24+
.HasForeignKey<Runner>(r => r.JobId)
25+
.IsRequired(false);
26+
27+
modelBuilder.Entity<Job>()
28+
.HasOne(j => j.Runner)
29+
.WithOne()
30+
.HasForeignKey<Job>(j => j.RunnerId)
31+
.IsRequired(false);
32+
}
33+
34+
public async Task<Runner> LinkJobToRunner(long jobId, string runnerName)
35+
{
36+
try
37+
{
38+
var job = await Jobs.Include(x => x.Runner).FirstOrDefaultAsync(x => x.GithubJobId == jobId);
39+
var runner = await Runners.Include(x => x.Job).Include(x => x.Lifecycle).FirstOrDefaultAsync(x => x.Hostname == runnerName);
40+
runner.Job = job;
41+
job.Runner = runner;
42+
job.InProgressTime = DateTime.UtcNow;
43+
runner.Lifecycle.Add(new()
44+
{
45+
Event = $"Runner got picked by job {jobId}",
46+
Status = RunnerStatus.Processing,
47+
EventTimeUtc = DateTime.UtcNow
48+
});
49+
await SaveChangesAsync();
50+
return runner;
51+
}
52+
catch
53+
{
54+
// unable to link
55+
return null;
56+
}
57+
58+
}
59+
60+
}
61+
62+
// Runners provisioned over time

Database/DbContext.cs

Lines changed: 0 additions & 166 deletions
This file was deleted.

Database/Job.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace GithubActionsOrchestrator.Database;
4+
5+
public class Job
6+
{
7+
public int JobId { get; set; }
8+
public long GithubJobId { get; set; }
9+
public string Repository { get; set; }
10+
public string Owner { get; set; }
11+
public JobState State { get; set; }
12+
public DateTime InProgressTime { get; set; }
13+
public DateTime QueueTime { get; set; }
14+
public DateTime CompleteTime { get; set; }
15+
public string JobUrl { get; set; }
16+
17+
//Relations
18+
public int? RunnerId { get; set; }
19+
20+
[JsonIgnore]
21+
public Runner Runner { get; set; }
22+
public bool Orphan { get; set; }
23+
public string RequestedProfile { get; set; }
24+
public string RequestedSize { get; set; }
25+
}

Database/JobState.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace GithubActionsOrchestrator.Database;
2+
3+
public enum JobState
4+
{
5+
Unknown = 0,
6+
Queued = 1,
7+
InProgress = 2,
8+
Completed = 3,
9+
Vanished = 4
10+
}

Database/Runner.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
namespace GithubActionsOrchestrator.Database;
2+
3+
public class Runner
4+
{
5+
public string IPv4 { get; set; }
6+
public int RunnerId { get; set; }
7+
public string Cloud { get; set; }
8+
public string Hostname { get; set; }
9+
public string Size { get; set; }
10+
public string Profile { get; set; }
11+
12+
// Relations
13+
14+
public int? JobId { get; set; }
15+
public Job Job { get; set; }
16+
public ICollection<RunnerLifecycle> Lifecycle { get; set; }
17+
public long CloudServerId { get; set; }
18+
19+
public DateTime CreateTime
20+
{
21+
get
22+
{
23+
return Lifecycle.FirstOrDefault(x => x.Status == RunnerStatus.CreationQueued)!.EventTimeUtc;
24+
}
25+
}
26+
27+
public RunnerStatus LastState
28+
{
29+
get
30+
{
31+
return Lifecycle.MaxBy(x => x.EventTimeUtc).Status;
32+
}
33+
}
34+
35+
public bool IsOnline { get; set; }
36+
public string Arch { get; set; }
37+
public string Owner { get; set; }
38+
public bool IsCustom { get; set; }
39+
40+
public DateTime LastStateTime
41+
{
42+
get
43+
{
44+
return Lifecycle.MaxBy(x => x.EventTimeUtc).EventTimeUtc;
45+
}
46+
}
47+
48+
public bool StuckJobReplacement { get; set; } = false;
49+
public bool UsePrivateNetwork { get; set; }
50+
public string ProvisionId { get; set; }
51+
public string ProvisionPayload { get; set; }
52+
}

0 commit comments

Comments
 (0)