feat: support inline event scripts
This commit is contained in:
@@ -2,6 +2,7 @@ using System.Diagnostics;
|
||||
using LiveRecorder.Application.Abstractions.Logging;
|
||||
using LiveRecorder.Application.Abstractions.Scripting;
|
||||
using LiveRecorder.Application.Abstractions.Settings;
|
||||
using LiveRecorder.Application.Models.Settings;
|
||||
using LiveRecorder.Domain.Entities;
|
||||
using LiveRecorder.Domain.Enums;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -31,7 +32,9 @@ public sealed class EventScriptService : IEventScriptService
|
||||
environment["LIVE_RECORDER_EVENT"] = "live_started";
|
||||
await RunAsync(
|
||||
settings.EnableEventScripts,
|
||||
settings.LiveStartedScriptMode,
|
||||
settings.LiveStartedScriptPath,
|
||||
settings.LiveStartedScriptContent,
|
||||
settings.EventScriptTimeoutSeconds,
|
||||
"live_started",
|
||||
environment,
|
||||
@@ -48,7 +51,9 @@ public sealed class EventScriptService : IEventScriptService
|
||||
environment["LIVE_RECORDER_EVENT"] = "live_ended";
|
||||
await RunAsync(
|
||||
settings.EnableEventScripts,
|
||||
settings.LiveEndedScriptMode,
|
||||
settings.LiveEndedScriptPath,
|
||||
settings.LiveEndedScriptContent,
|
||||
settings.EventScriptTimeoutSeconds,
|
||||
"live_ended",
|
||||
environment,
|
||||
@@ -82,7 +87,9 @@ public sealed class EventScriptService : IEventScriptService
|
||||
|
||||
await RunAsync(
|
||||
settings.EnableEventScripts,
|
||||
settings.SegmentCompletedScriptMode,
|
||||
settings.SegmentCompletedScriptPath,
|
||||
settings.SegmentCompletedScriptContent,
|
||||
settings.EventScriptTimeoutSeconds,
|
||||
"segment_completed",
|
||||
environment,
|
||||
@@ -94,7 +101,9 @@ public sealed class EventScriptService : IEventScriptService
|
||||
|
||||
private async Task RunAsync(
|
||||
bool enabled,
|
||||
string scriptMode,
|
||||
string scriptPath,
|
||||
string scriptContent,
|
||||
int timeoutSeconds,
|
||||
string eventName,
|
||||
IReadOnlyDictionary<string, string> environment,
|
||||
@@ -103,19 +112,24 @@ public sealed class EventScriptService : IEventScriptService
|
||||
Guid? recordTaskId,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (!enabled || string.IsNullOrWhiteSpace(scriptPath))
|
||||
if (!enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var normalizedScriptPath = NormalizePath(scriptPath);
|
||||
if (!File.Exists(normalizedScriptPath))
|
||||
var execution = CreateExecution(scriptMode, scriptPath, scriptContent);
|
||||
if (execution is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (execution.IsMissing)
|
||||
{
|
||||
await _systemLogService.WriteAsync(
|
||||
SystemLogLevel.Warning,
|
||||
"Script",
|
||||
$"Event script was not found for {eventName}.",
|
||||
normalizedScriptPath,
|
||||
execution.Detail,
|
||||
liveRoomId,
|
||||
recordSessionId,
|
||||
recordTaskId,
|
||||
@@ -123,7 +137,7 @@ public sealed class EventScriptService : IEventScriptService
|
||||
return;
|
||||
}
|
||||
|
||||
var startInfo = CreateStartInfo(normalizedScriptPath);
|
||||
var startInfo = execution.StartInfo!;
|
||||
foreach (var pair in environment)
|
||||
{
|
||||
startInfo.Environment[pair.Key] = pair.Value;
|
||||
@@ -148,7 +162,7 @@ public sealed class EventScriptService : IEventScriptService
|
||||
process.ExitCode == 0
|
||||
? $"Event script completed for {eventName}."
|
||||
: $"Event script exited with code {process.ExitCode} for {eventName}.",
|
||||
normalizedScriptPath,
|
||||
execution.Detail,
|
||||
liveRoomId,
|
||||
recordSessionId,
|
||||
recordTaskId,
|
||||
@@ -161,7 +175,7 @@ public sealed class EventScriptService : IEventScriptService
|
||||
SystemLogLevel.Warning,
|
||||
"Script",
|
||||
$"Event script timed out for {eventName}.",
|
||||
normalizedScriptPath,
|
||||
execution.Detail,
|
||||
liveRoomId,
|
||||
recordSessionId,
|
||||
recordTaskId,
|
||||
@@ -182,6 +196,32 @@ public sealed class EventScriptService : IEventScriptService
|
||||
}
|
||||
}
|
||||
|
||||
private static EventScriptExecution? CreateExecution(string scriptMode, string scriptPath, string scriptContent)
|
||||
{
|
||||
if (string.Equals(scriptMode, EventScriptSourceModes.Inline, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(scriptContent))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new EventScriptExecution(CreateInlineStartInfo(scriptContent), "[inline script]");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(scriptPath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var normalizedScriptPath = NormalizePath(scriptPath);
|
||||
if (!File.Exists(normalizedScriptPath))
|
||||
{
|
||||
return new EventScriptExecution(null, normalizedScriptPath, IsMissing: true);
|
||||
}
|
||||
|
||||
return new EventScriptExecution(CreateStartInfo(normalizedScriptPath), normalizedScriptPath);
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> BuildLiveRoomEnvironment(LiveRoom? liveRoom, DateTimeOffset occurredAt)
|
||||
{
|
||||
return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
||||
@@ -239,6 +279,34 @@ public sealed class EventScriptService : IEventScriptService
|
||||
return startInfo;
|
||||
}
|
||||
|
||||
private static ProcessStartInfo CreateInlineStartInfo(string scriptContent)
|
||||
{
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
UseShellExecute = false,
|
||||
RedirectStandardError = false,
|
||||
RedirectStandardOutput = false,
|
||||
CreateNoWindow = true,
|
||||
WorkingDirectory = AppContext.BaseDirectory
|
||||
};
|
||||
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
startInfo.FileName = "powershell";
|
||||
startInfo.ArgumentList.Add("-NoProfile");
|
||||
startInfo.ArgumentList.Add("-ExecutionPolicy");
|
||||
startInfo.ArgumentList.Add("Bypass");
|
||||
startInfo.ArgumentList.Add("-Command");
|
||||
startInfo.ArgumentList.Add(scriptContent);
|
||||
return startInfo;
|
||||
}
|
||||
|
||||
startInfo.FileName = "/bin/sh";
|
||||
startInfo.ArgumentList.Add("-c");
|
||||
startInfo.ArgumentList.Add(scriptContent);
|
||||
return startInfo;
|
||||
}
|
||||
|
||||
private static string NormalizePath(string? path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
@@ -275,4 +343,6 @@ public sealed class EventScriptService : IEventScriptService
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record EventScriptExecution(ProcessStartInfo? StartInfo, string Detail, bool IsMissing = false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user