Skip to content

Commit

Permalink
Init now creates default .env file; run command now checks for .env f…
Browse files Browse the repository at this point in the history
…ile and sends its contents along to worker; added to existing engine tests to check that environment variables are actually sent to worker and are usable by JS
  • Loading branch information
doug-jacob committed Feb 5, 2025
1 parent cdd9dbe commit 621d253
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
25 changes: 22 additions & 3 deletions Trell.Test/Engine/EngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public EngineFixture() {
throw new Error(`Expected: ${expected}, Actual: ${actual}`);
}
expected = JSON.stringify({ 'red': 'blue' });
actual = JSON.stringify(context.env);
if (actual !== expected) {
throw new Error(`Expected: ${expected}, Actual: ${actual}`);
}
return true;
"""
);
Expand All @@ -81,6 +87,13 @@ public EngineFixture() {
throw new Error(`Expected: ${expected}, Actual: ${actual}`);
}
// JS doesn't do value equality for objects, so this is a workaround
expected = JSON.stringify({ 'A': '1', 'B': '2' });
actual = JSON.stringify(context.env);
if (actual !== expected) {
throw new Error(`Expected: ${expected}, Actual: ${actual}`);
}
return true;
"""
);
Expand All @@ -101,6 +114,12 @@ public EngineFixture() {
throw new Error(`Expected: ${expected}, Actual: ${context.file.type}`);
}
expected = JSON.stringify({ 'XYZ': 'abcd', '37': '12', '9number': 'TEST' });
actual = JSON.stringify(context.env);
if (actual !== expected) {
throw new Error(`Expected: ${expected}, Actual: ${actual}`);
}
return true;
"""
);
Expand Down Expand Up @@ -318,7 +337,7 @@ public async Task TestCronReceivesExpectedContext() {
}
};

var work = new Work(new(), "{}", this.fixture.EngineDir, "onCronTrigger") {
var work = new Work(new(), "{\"A\": \"1\", \"B\": \"2\"}", this.fixture.EngineDir, "onCronTrigger") {
WorkerJs = workerPath!,
Arg = fn.ToFunctionArg(eng),
};
Expand Down Expand Up @@ -347,7 +366,7 @@ public async Task TestRequestReceivesExpectedContext() {
}
};

var work = new Work(new(), "{}", this.fixture.EngineDir, "onRequest") {
var work = new Work(new(), "{\"red\": \"blue\"}", this.fixture.EngineDir, "onRequest") {
WorkerJs = workerPath!,
Arg = fn.ToFunctionArg(eng),
};
Expand All @@ -372,7 +391,7 @@ public async Task TestUploadReceivesExpectedContext() {
}
};

var work = new Work(new(), "{}", this.fixture.EngineDir, "onUpload") {
var work = new Work(new(), "{\"XYZ\": \"abcd\", \"37\": \"12\", \"9number\": \"TEST\"}", this.fixture.EngineDir, "onUpload") {
WorkerJs = workerPath!,
Arg = fn.ToFunctionArg(eng),
};
Expand Down
14 changes: 13 additions & 1 deletion Trell/CliCommands/InitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public async override Task<int> ExecuteAsync(CommandContext context, InitCommand
var gitignoreFilePath = Path.GetFullPath(".gitignore", currentDir);
var gitignoreAlreadyExists = File.Exists(gitignoreFilePath);

if (configAlreadyExists || workerAlreadyExists || gitignoreAlreadyExists) {
var envFilePath = Path.GetFullPath(".env", currentDir);
var envAlreadyExists = File.Exists(envFilePath);

if (configAlreadyExists || workerAlreadyExists || gitignoreAlreadyExists || envAlreadyExists) {
var shouldClobber = AnsiConsole.Prompt(
new TextPrompt<bool>("Existing Trell files found. Continuing will overwrite the existing files. Are you sure you want to continue?")
.AddChoice(true)
Expand Down Expand Up @@ -98,12 +101,21 @@ async function onUpload(context) {

await File.WriteAllTextAsync(gitignoreFilePath, """
*.sock
*.env
data/
"""
);
AnsiConsole.WriteLine(gitignoreAlreadyExists ? $"Overwrote {gitignoreFilePath}" : $"Created {gitignoreFilePath}");

await File.WriteAllTextAsync(envFilePath, """
# Place any environment variables/secrets in this file for worker development.
# KEY="..."
"""
);
AnsiConsole.WriteLine(envAlreadyExists ? $"Overwrote {envFilePath}" : $"Created {envFilePath}");

AnsiConsole.WriteLine($"""
Trell worker created in {currentDir}.
Run this worker's handlers with:
Expand Down
21 changes: 19 additions & 2 deletions Trell/CliCommands/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class RunCommand : AsyncCommand<RunCommandSettings> {
int id = 0;
string GetNextExecutionId() => $"id-{Interlocked.Increment(ref this.id)}";

Rpc.ServerWorkOrder GetServerWorkOrder(RunCommandSettings settings, TrellConfig config) {
Rpc.ServerWorkOrder GetServerWorkOrder(RunCommandSettings settings, TrellConfig config, Dictionary<string, string> env) {
// Makes all paths relative to worker root and sanitizes paths for TrellPath's use.
var rootDir = Path.GetFullPath(config.Storage.Path);

Expand Down Expand Up @@ -129,6 +129,9 @@ Rpc.ServerWorkOrder GetServerWorkOrder(RunCommandSettings settings, TrellConfig
Data = new() {
Text = JsonSerializer.Serialize(new { timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString() }),
},
Env = new() {
Text = JsonSerializer.Serialize(env),
},
CodePath = codePath,
DataPath = dataPath,
WorkerFilename = fileName,
Expand Down Expand Up @@ -205,10 +208,24 @@ Rpc.Function GetFunction(RunCommandSettings settings) {
};
}

static Dictionary<string, string> LoadEnv(string path) {
path = Path.GetFullPath(path);
if (!File.Exists(path)) {
return [];
}
var content = File.ReadAllText(path);
try {
return Tomlyn.Toml.ToModel<Dictionary<string, string>>(content);
} catch (Exception) {
throw new Exception("Unable to parse .env file. Please make sure file contains only entries formatted: KEY = \"value\"");
}
}

public override async Task<int> ExecuteAsync(CommandContext context, RunCommandSettings settings) {
settings.Validate();
App.BootstrapLogger(null);
var config = TrellConfig.LoadToml("Trell.toml");
var env = LoadEnv(".env");

var extensionContainer = TrellSetup.ExtensionContainer(config);
var runtimeWrapper = new RuntimeWrapper(extensionContainer, config.ToRuntimeConfig());
Expand All @@ -217,7 +234,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, RunCommandS
Log.Information("Run: Executing worker in-process.");

async Task<Rpc.WorkResult> Exec() {
var workOrder = GetServerWorkOrder(settings, config);
var workOrder = GetServerWorkOrder(settings, config, env);
workOrder.WorkOrder.ExecutionId = GetNextExecutionId();
var result = await worker.ExecuteAsync(workOrder.WorkOrder);
Log.Information("Run: Execution Id: {Id} Result: {Result}",
Expand Down

0 comments on commit 621d253

Please sign in to comment.