Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headless facade + dockerization #202

Merged
merged 8 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ GolemLib/bin/
GolemLib/obj/
Mock/bin/
Mock/obj/
/FacadeApp/bin/
/FacadeApp/obj/
/FacadeHeadlessApp/bin/
/FacadeHeadlessApp/obj/
/Golem/bin/
/Golem/obj/
/Golem.Tests/bin/
Expand All @@ -34,8 +34,8 @@ Mock/obj/
/Golem.Package/tests/
/Golem.Package/package/

/ExampleRunner/bin/
/ExampleRunner/obj/
/example/ExampleRunner/bin/
/example/ExampleRunner/obj/

/MockGUI/bin/
/MockGUI/obj/
Expand All @@ -48,6 +48,7 @@ Mock/obj/
/example/ai-requestor/.venv/
/example/ai-requestor/dist

example/ai-requestor/outputs/
output.png

/example/DummyAiHttpServer/__pycache__/
Expand Down
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-jammy AS build

# Install python, because Golem.Tools need it to build App and it is dependency
# of `FacadeHeadlessApp` so we have no choice.
RUN apt-get update && apt-get install -y python3 python3-venv python3-pip
RUN ln -s /usr/bin/python3 /usr/bin/python
RUN pip3 install pyinstaller

RUN git clone https://github.com/golemfactory/gamerhash-facade.git
WORKDIR /gamerhash-facade
RUN git checkout headless-facade

# Build necessary application
RUN dotnet build FacadeHeadlessApp
RUN dotnet build Golem.Package

RUN dotnet publish FacadeHeadlessApp --no-restore -o /apps
RUN dotnet publish Golem.Package --no-restore -o /apps


FROM mcr.microsoft.com/dotnet/runtime:7.0-jammy

WORKDIR /apps
COPY --from=build /apps .

RUN ./Golem.Package download --target modules --version v5.1.0
ENTRYPOINT ["./FacadeHeadlessApp", "--golem", "modules"]
CMD ["--wallet", "0x82a630d2447ffd282657978f9f76c02da8be9819"]
127 changes: 0 additions & 127 deletions FacadeApp/Program.cs

This file was deleted.

136 changes: 136 additions & 0 deletions FacadeHeadlessApp/Facade.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System.ComponentModel;
using System.Diagnostics;

using CommandLine;

using Golem;
using GolemLib;
using Golem.Tools.ViewModels;

using Microsoft.Extensions.Logging;
using Golem.Tools;

namespace FacadeHeadlessApp;

public class FacadeAppArguments
{
[Option('g', "golem", Required = true, HelpText = "Path to a folder with golem executables (modules)")]
public string? GolemPath { get; set; }
[Option('d', "use-dll", Required = false, HelpText = "Load Golem object from dll found in binaries directory. (Simulates how GamerHash will use it. Otherwise project dependency will be used.)")]
public bool UseDll { get; set; }
[Option('r', "relay", Default = RelayType.Central, Required = false, HelpText = "Change relay to devnet yacn2a or setup local")]
public required RelayType Relay { get; set; }
[Option('m', "mainnet", Default = false, Required = false, HelpText = "Enables usage of mainnet")]
public bool Mainnet { get; set; }
[Option('w', "wallet", Required = false, HelpText = "Wallet address to receive funds")]
public string? Wallet { get; set; }
[Option('i', "interactive", Default = false, HelpText = "Enable interactive console mode")]
public bool Interactive { get; set; }
}


internal class Facade
{
static async Task Main(string[] argsArray)
{
ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
builder.AddSimpleConsole()
);

var logger = loggerFactory.CreateLogger<Facade>();

var args = Parser.Default.ParseArguments<FacadeAppArguments>(argsArray).Value;
if (args == null)
return;

string golemPath = args.GolemPath ?? "";

logger.LogInformation("Path: " + golemPath);

await using GolemViewModel view = args.UseDll ?
await GolemViewModel.Load(golemPath, args.Relay, args.Mainnet) :
await GolemViewModel.CreateStatic(golemPath, args.Relay, args.Mainnet);

var golem = view.Golem;
if (args.Wallet != null)
golem.WalletAddress = args.Wallet;
golem.PropertyChanged += new PropertyChangedHandler(logger).For(nameof(IGolem.Status));


if (args.Interactive)
{
bool end = false;

do
{
Console.WriteLine("Start/Stop/End?");
var line = Console.ReadLine();

switch (line)
{
case "Start":
await golem.Start();
break;
case "Stop":
await golem.Stop();
break;
case "End":
end = true;
break;
case "Wallet":
var walletAddress = golem.WalletAddress;
golem.WalletAddress = walletAddress;
Console.WriteLine($"Wallet: {walletAddress}");
break;

default: Console.WriteLine($"Didn't understand: {line}"); break;
}
} while (!end);


Console.WriteLine("Done");
}
else
{
await golem.Start();
await ConsoleHelper.WaitForCancellation();
}
}
}

public class PropertyChangedHandler
{

public PropertyChangedHandler(ILogger logger)
{
this._logger = logger;
}

readonly ILogger _logger;
public PropertyChangedEventHandler For(string name)
{
return name switch
{
"Status" => Status_PropertyChangedHandler,
"Activities" => Activities_PropertyChangedHandler,
_ => Empty_PropertyChangedHandler,
};
}

private void Status_PropertyChangedHandler(object? sender, PropertyChangedEventArgs e)
{
if (sender is Golem.Golem golem && e.PropertyName == "Status")
_logger.LogInformation($"Status property has changed: {e.PropertyName} to {golem.Status}");
}

private void Activities_PropertyChangedHandler(object? sender, PropertyChangedEventArgs e)
{
if (sender is Golem.Golem golem && e.PropertyName != "Activities")
_logger.LogInformation($"Activities property has changed: {e.PropertyName}. Current job: {golem.CurrentJob}");
}

private void Empty_PropertyChangedHandler(object? sender, PropertyChangedEventArgs e)
{
_logger.LogInformation($"Property {e} is not supported in this context");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<ProjectReference Include="..\Golem\Golem.csproj" />
<ProjectReference Include="..\Golem.Tools\Golem.Tools.csproj" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions Golem.Tools/Console.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

namespace Golem.Tools;


public class ConsoleHelper
{
public static void WaitForCtrlC()
{
Console.TreatControlCAsInput = true;

ConsoleKeyInfo cki;
do
{
cki = Console.ReadKey();
} while (!(((cki.Modifiers & ConsoleModifiers.Control) != 0) && (cki.Key == ConsoleKey.C)));
}

// Based on: https://www.meziantou.net/handling-cancelkeypress-using-a-cancellationtoken.htm
public static async Task WaitForCancellation()
{
using var cts = new CancellationTokenSource();
Console.CancelKeyPress += (sender, e) =>
{
// We'll stop the process manually by using the CancellationToken
e.Cancel = true;

// Change the state of the CancellationToken to "Canceled"
// - Set the IsCancellationRequested property to true
// - Call the registered callbacks
cts.Cancel();
};


while (true)
{
try
{
// We can't pass TimeSpan.MaxValue because it will throw an exception.
await Task.Delay(TimeSpan.FromHours(1), cts.Token);
}
catch (Exception e) when (e.IsCancelled())
{
Console.WriteLine("Application received cancellation signal. Exiting...");
return;
}
}
}
}
Loading
Loading