Skip to content

Commit 6b0a16b

Browse files
committed
Merge branch 'feature/better-autoclient'
2 parents e7d9e83 + eac06e8 commit 6b0a16b

18 files changed

+524
-6
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Docker - AutoClientCenter
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
tags:
8+
- 'v*.*.*'
9+
paths:
10+
- 'Tools/AutoClientCenter/**'
11+
- '!Tools/AutoClientCenter/Dockerfile'
12+
- .github/workflows/docker-autoclientcenter.yml
13+
- .github/workflows/docker-reusable.yml
14+
workflow_dispatch:
15+
16+
jobs:
17+
build-and-push:
18+
name: Build and Push
19+
uses: ./.github/workflows/docker-reusable.yml
20+
with:
21+
docker_file: Tools/AutoClientCenter/Dockerfile
22+
docker_repo: codexstorage/codex-autoclientcenter
23+
secrets: inherit
24+

ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace CodexPlugin
77
{
88
public class CodexContainerRecipe : ContainerRecipeFactory
99
{
10-
private const string DefaultDockerImage = "codexstorage/nim-codex:sha-64b82de-dist-tests";
10+
private const string DefaultDockerImage = "codexstorage/nim-codex:sha-1e2ad95-dist-tests";
1111

1212
public const string ApiPortTag = "codex_api_port";
1313
public const string ListenPortTag = "codex_listen_port";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<UserSecretsId>80f55fc5-9f8f-4bb4-89bc-314ed7379c5f</UserSecretsId>
8+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
13+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ActiveDebugProfile>IIS Express</ActiveDebugProfile>
5+
</PropertyGroup>
6+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
7+
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
8+
</PropertyGroup>
9+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@AutoClientCenter_HostAddress = http://localhost:5185
2+
3+
GET {{AutoClientCenter_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###

Tools/AutoClientCenter/CidRepo.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
namespace AutoClientCenter
2+
{
3+
public class CidRepo
4+
{
5+
private readonly Random random = new Random();
6+
private readonly object _lock = new object();
7+
private readonly List<CidEntry> entries = new List<CidEntry>();
8+
9+
public void Add(string cid, long knownSize)
10+
{
11+
lock (_lock)
12+
{
13+
entries.Add(new CidEntry(cid, knownSize));
14+
}
15+
}
16+
17+
public void AddEncoded(string originalCid, string encodedCid)
18+
{
19+
lock (_lock)
20+
{
21+
var entry = entries.SingleOrDefault(e => e.Cid == originalCid);
22+
if (entry == null) return;
23+
24+
entry.Encoded = encodedCid;
25+
}
26+
}
27+
28+
public void Assign(AcDownloadStep downloadStep)
29+
{
30+
lock (_lock)
31+
{
32+
while (true)
33+
{
34+
if (!entries.Any()) return;
35+
36+
var i = random.Next(0, entries.Count);
37+
var entry = entries[i];
38+
39+
if (entry.CreatedUtc < (DateTime.UtcNow + TimeSpan.FromHours(18)))
40+
{
41+
entries.RemoveAt(i);
42+
}
43+
else
44+
{
45+
downloadStep.Cid = entry.Cid;
46+
return;
47+
}
48+
}
49+
}
50+
}
51+
52+
public long? GetSizeKbsForCid(string cid)
53+
{
54+
lock (_lock)
55+
{
56+
var entry = entries.SingleOrDefault(e => e.Cid == cid);
57+
if (entry == null) return null;
58+
return entry.KnownSize;
59+
}
60+
}
61+
}
62+
63+
public class CidEntry
64+
{
65+
public CidEntry(string cid, long knownSize)
66+
{
67+
Cid = cid;
68+
KnownSize = knownSize;
69+
}
70+
71+
public string Cid { get; }
72+
public string Encoded { get; set; } = string.Empty;
73+
public long KnownSize { get; }
74+
public DateTime CreatedUtc { get; } = DateTime.UtcNow;
75+
}
76+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace AutoClientCenter.Controllers
4+
{
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class ConfigController : ControllerBase
8+
{
9+
private readonly ITaskService taskService;
10+
11+
public ConfigController(ITaskService taskService)
12+
{
13+
this.taskService = taskService;
14+
}
15+
16+
[HttpGet("Stats")]
17+
public AcStats Get()
18+
{
19+
return taskService.GetStats();
20+
}
21+
22+
[HttpPost("Set")]
23+
public void Post([FromBody] AcTasks tasks)
24+
{
25+
taskService.SetConfig(tasks);
26+
}
27+
}
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace AutoClientCenter.Controllers
4+
{
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class TasksController : ControllerBase
8+
{
9+
private readonly ITaskService taskService;
10+
private static readonly object processLock = new object();
11+
12+
public TasksController(ITaskService taskService)
13+
{
14+
this.taskService = taskService;
15+
}
16+
17+
[HttpGet]
18+
public AcTasks Get()
19+
{
20+
return taskService.GetTasks();
21+
}
22+
23+
[HttpPost("Results")]
24+
public void Post([FromBody] AcTaskStep[] taskSteps)
25+
{
26+
Task.Run(() =>
27+
{
28+
lock (processLock)
29+
{
30+
taskService.ProcessResults(taskSteps);
31+
}
32+
});
33+
}
34+
}
35+
}

Tools/AutoClientCenter/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
2+
3+
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
4+
USER app
5+
WORKDIR /app
6+
7+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
8+
ARG BUILD_CONFIGURATION=Release
9+
WORKDIR /src
10+
COPY ["AutoClientCenter/AutoClientCenter.csproj", "AutoClientCenter/"]
11+
RUN dotnet restore "./AutoClientCenter/AutoClientCenter.csproj"
12+
COPY . .
13+
WORKDIR "/src/AutoClientCenter"
14+
RUN dotnet build "./AutoClientCenter.csproj" -c $BUILD_CONFIGURATION -o /app/build
15+
16+
FROM build AS publish
17+
ARG BUILD_CONFIGURATION=Release
18+
RUN dotnet publish "./AutoClientCenter.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
19+
20+
FROM base AS final
21+
WORKDIR /app
22+
COPY --from=publish /app/publish .
23+
ENTRYPOINT ["dotnet", "AutoClientCenter.dll"]

Tools/AutoClientCenter/Model.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
namespace AutoClientCenter
2+
{
3+
public class AcTasks
4+
{
5+
public int StartTaskEverySeconds { get; set; }
6+
public AcTask[] Tasks { get; set; } = Array.Empty<AcTask>();
7+
}
8+
9+
public class AcTask
10+
{
11+
public int ChanceWeight { get; set; }
12+
public AcTaskStep[] Steps { get; set; } = Array.Empty<AcTaskStep>();
13+
}
14+
15+
public class AcTaskStep
16+
{
17+
public string Id { get; set; } = string.Empty;
18+
public AcUploadStep? UploadStep { get; set; }
19+
public AcStoreStep? StoreStep { get; set; }
20+
public AcDownloadStep? DownloadStep { get; set; }
21+
public string? ResultErrorMsg { get; set; }
22+
}
23+
24+
public class AcUploadStep
25+
{
26+
public long SizeInBytes { get; set; }
27+
public string? ResultCid { get; set; }
28+
}
29+
30+
public class AcStoreStep
31+
{
32+
public int ContractDurationMinutes { get; set; }
33+
public int ContractExpiryMinutes { get; set; }
34+
public int NumHosts { get; set; }
35+
public int HostTolerance { get; set; }
36+
public int Price { get; set; }
37+
public int RequiredCollateral { get; set; }
38+
public string? ResultPurchaseId { get; set; }
39+
public string? ResultOriginalCid { get; set; }
40+
public string? ResultEncodedCid { get; set; }
41+
}
42+
43+
public class AcDownloadStep
44+
{
45+
public string Cid { get; set; } = string.Empty;
46+
public long ResultDownloadTimeMilliseconds { get; set; }
47+
}
48+
49+
public class AcStats
50+
{
51+
public DateTime ServiceStartUtc { get; set; } = DateTime.MinValue;
52+
public int TotalUploads { get; set; }
53+
public int TotalUploadsFailed { get; set; }
54+
public int TotalDownloads { get; set; }
55+
public long[] DownloadTimesMillisecondsPerKb { get; set; } = Array.Empty<long>();
56+
public int TotalDownloadsFailed { get; set; }
57+
public int TotalContractsStarted { get; set; }
58+
public int TotalContractStartsFailed { get; set; }
59+
}
60+
}

Tools/AutoClientCenter/Program.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
namespace AutoClientCenter
3+
{
4+
public class Program
5+
{
6+
public static void Main(string[] args)
7+
{
8+
var builder = WebApplication.CreateBuilder(args);
9+
10+
var listenPort = Environment.GetEnvironmentVariable("APIPORT");
11+
if (string.IsNullOrEmpty(listenPort)) listenPort = "31090";
12+
13+
builder.WebHost.ConfigureKestrel((context, options) =>
14+
{
15+
options.ListenAnyIP(Convert.ToInt32(listenPort));
16+
});
17+
18+
builder.Services.AddSingleton<ITaskService>(new TaskService());
19+
builder.Services.AddControllers();
20+
builder.Services.AddEndpointsApiExplorer();
21+
builder.Services.AddSwaggerGen();
22+
23+
var app = builder.Build();
24+
25+
if (app.Environment.IsDevelopment())
26+
{
27+
app.UseSwagger();
28+
app.UseSwaggerUI();
29+
}
30+
31+
app.UseHttpsRedirection();
32+
33+
app.UseAuthorization();
34+
35+
36+
app.MapControllers();
37+
38+
Console.WriteLine("AutoClientCenter listening on port " + listenPort);
39+
40+
app.Run();
41+
}
42+
}
43+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"profiles": {
3+
"http": {
4+
"commandName": "Project",
5+
"launchBrowser": true,
6+
"launchUrl": "swagger",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"dotnetRunMessages": true,
11+
"applicationUrl": "http://localhost:5185"
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
},
20+
"dotnetRunMessages": true,
21+
"applicationUrl": "https://localhost:7077;http://localhost:5185"
22+
},
23+
"IIS Express": {
24+
"commandName": "IISExpress",
25+
"launchBrowser": true,
26+
"launchUrl": "swagger",
27+
"environmentVariables": {
28+
"ASPNETCORE_ENVIRONMENT": "Development"
29+
}
30+
},
31+
"Container (Dockerfile)": {
32+
"commandName": "Docker",
33+
"launchBrowser": true,
34+
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
35+
"environmentVariables": {
36+
"ASPNETCORE_HTTPS_PORTS": "8081",
37+
"ASPNETCORE_HTTP_PORTS": "8080"
38+
},
39+
"publishAllPorts": true,
40+
"useSSL": true
41+
}
42+
},
43+
"$schema": "http://json.schemastore.org/launchsettings.json",
44+
"iisSettings": {
45+
"windowsAuthentication": false,
46+
"anonymousAuthentication": true,
47+
"iisExpress": {
48+
"applicationUrl": "http://localhost:50475",
49+
"sslPort": 44395
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)