initial copy

This commit is contained in:
2025-12-24 21:26:35 +03:00
parent ff1f62f341
commit f3b5260f7e
88 changed files with 3308 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
namespace s8n_runtime;
using System.Text.Json;
using System.Text.Json.Serialization;
public class WorkflowRuntimeRunner
{
private static readonly JsonSerializerOptions serializerOptions = new(JsonSerializerDefaults.Web)
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
public static async Task<int> Main<TWorkflow>(string[] args, bool silent = false) where TWorkflow : WorkflowRuntime
{
if (!silent) Console.WriteLine("Starting workflow...");
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
{
if (!silent) Console.WriteLine("Canceling...");
cts.Cancel();
e.Cancel = true;
};
async Task<string?> FindWorkflowJsonFile()
{
if (args.FirstOrDefault(a => a.EndsWith("workflow.json")) is string jsonArg) return jsonArg;
if (Environment.GetEnvironmentVariable("WORKFLOW_FILE") is string workflowFile) return workflowFile;
var testPath = Path.GetFullPath("workflow.json");
if (File.Exists(testPath))
{
return testPath;
}
return null;
}
var filePath = await FindWorkflowJsonFile();
TWorkflow runtime;
if (filePath == null)
{
runtime = Activator.CreateInstance<TWorkflow>();
}
else
{
runtime = JsonSerializer.Deserialize<TWorkflow>(File.OpenRead(filePath), serializerOptions)!;
}
runtime.Prepare();
try
{
await runtime.RunAsync(cts.Token);
}
catch (TaskCanceledException)
{
if (!silent) Console.WriteLine("Canceled.");
}
finally
{
await runtime.OnStop();
}
if (runtime.StoreState == RuntimeStateStoreStrategy.WhenExit)
{
if (string.IsNullOrEmpty(filePath) is false)
{
JsonSerializer.Serialize(File.OpenWrite(filePath), runtime, serializerOptions);
}
}
if (!silent) Console.WriteLine("Workflow stopped.");
return 0;
}
}