| | 1 | | using System.Management.Automation; |
| | 2 | | using System.Reflection; |
| | 3 | | using Kestrun.Scripting; |
| | 4 | | using Kestrun.Utilities; |
| | 5 | | using Microsoft.CodeAnalysis.CSharp; |
| | 6 | | using Serilog.Events; |
| | 7 | |
|
| | 8 | | namespace Kestrun.Scheduling; |
| | 9 | |
|
| | 10 | | internal static class JobFactory |
| | 11 | | { |
| 12 | 12 | | internal record JobConfig( |
| 17 | 13 | | ScriptLanguage Language, |
| 43 | 14 | | string Code, |
| 91 | 15 | | Serilog.ILogger Log, |
| 39 | 16 | | KestrunRunspacePoolManager? Pool = null, |
| 0 | 17 | | string[]? ExtraImports = null, |
| 0 | 18 | | Assembly[]? ExtraRefs = null, |
| 0 | 19 | | IReadOnlyDictionary<string, object?>? Locals = null, |
| 0 | 20 | | LanguageVersion LanguageVersion = LanguageVersion.CSharp12 |
| 12 | 21 | | ); |
| | 22 | |
|
| | 23 | | internal static Func<CancellationToken, Task> Create(JobConfig config) |
| | 24 | | { |
| 12 | 25 | | return config.Language switch |
| 12 | 26 | | { |
| 12 | 27 | | ScriptLanguage.PowerShell => |
| 12 | 28 | | config.Pool is null |
| 12 | 29 | | ? throw new InvalidOperationException("PowerShell runspace pool must be provided for PowerShell jobs |
| 12 | 30 | | : PowerShellJob(config), |
| 0 | 31 | | ScriptLanguage.CSharp => RoslynJob(config), |
| 0 | 32 | | ScriptLanguage.VBNet => RoslynJob(config), |
| 0 | 33 | | _ => throw new NotSupportedException($"Language {config.Language} not supported.") |
| 12 | 34 | | }; |
| | 35 | | } |
| | 36 | |
|
| | 37 | | public static async Task<Func<CancellationToken, Task>> CreateAsync( |
| | 38 | | JobConfig config, FileInfo fileInfo, CancellationToken ct = default) |
| | 39 | | { |
| 5 | 40 | | ArgumentNullException.ThrowIfNull(fileInfo); |
| 5 | 41 | | if (!fileInfo.Exists) |
| | 42 | | { |
| 0 | 43 | | throw new FileNotFoundException(fileInfo.FullName); |
| | 44 | | } |
| | 45 | |
|
| 5 | 46 | | var updatedConfig = config with { Code = await File.ReadAllTextAsync(fileInfo.FullName, ct) }; |
| 5 | 47 | | if (updatedConfig.Log.IsEnabled(LogEventLevel.Debug)) |
| | 48 | | { |
| 5 | 49 | | updatedConfig.Log.Debug("Creating job for {File} with language {Lang}", fileInfo.FullName, updatedConfig.Lan |
| | 50 | | } |
| | 51 | |
|
| 5 | 52 | | return Create(updatedConfig); |
| 5 | 53 | | } |
| | 54 | |
|
| | 55 | | public static Func<CancellationToken, Task> Create( |
| 2 | 56 | | JobConfig config, FileInfo fileInfo) => CreateAsync(config, fileInfo).GetAwaiter().GetResult(); |
| | 57 | |
|
| | 58 | |
|
| | 59 | | /* ---------------- PowerShell ---------------- */ |
| | 60 | | private static Func<CancellationToken, Task> PowerShellJob( |
| | 61 | | JobConfig config) |
| | 62 | | { |
| 11 | 63 | | return async ct => |
| 11 | 64 | | { |
| 9 | 65 | | if (config.Log.IsEnabled(LogEventLevel.Debug)) |
| 11 | 66 | | { |
| 9 | 67 | | config.Log.Debug("Building PowerShell delegate, script length={Length}", config.Code?.Length); |
| 11 | 68 | | } |
| 11 | 69 | |
|
| 9 | 70 | | if (config.Pool is null) |
| 11 | 71 | | { |
| 0 | 72 | | throw new InvalidOperationException("PowerShell runspace pool must be provided for PowerShell jobs."); |
| 11 | 73 | | } |
| 11 | 74 | |
|
| 9 | 75 | | var runspace = config.Pool.Acquire(); |
| 11 | 76 | | try |
| 11 | 77 | | { |
| 9 | 78 | | using var ps = PowerShell.Create(); |
| 9 | 79 | | ps.Runspace = runspace; |
| 9 | 80 | | _ = ps.AddScript(config.Code); |
| 9 | 81 | | if (config.Log.IsEnabled(LogEventLevel.Debug)) |
| 11 | 82 | | { |
| 9 | 83 | | config.Log.Debug("Executing PowerShell script with {RunspaceId} - {Preview}", runspace.Id, config.Co |
| 11 | 84 | | } |
| 11 | 85 | |
|
| 11 | 86 | | // Register cancellation |
| 9 | 87 | | using var reg = ct.Register(() => ps.Stop()); |
| 11 | 88 | |
|
| 11 | 89 | | // Wait for the PowerShell script to complete |
| 9 | 90 | | var psResults = await ps.InvokeAsync().WaitAsync(ct).ConfigureAwait(false); |
| 11 | 91 | |
|
| 8 | 92 | | config.Log.Verbose($"PowerShell script executed with {psResults.Count} results."); |
| 8 | 93 | | if (config.Log.IsEnabled(LogEventLevel.Debug)) |
| 11 | 94 | | { |
| 8 | 95 | | config.Log.Debug("PowerShell script output:"); |
| 20 | 96 | | foreach (var r in psResults.Take(10)) // first 10 only |
| 11 | 97 | | { |
| 2 | 98 | | config.Log.Debug(" • {Result}", r); |
| 11 | 99 | | } |
| 11 | 100 | |
|
| 8 | 101 | | if (psResults.Count > 10) |
| 11 | 102 | | { |
| 0 | 103 | | config.Log.Debug(" … {Count} more", psResults.Count - 10); |
| 11 | 104 | | } |
| 11 | 105 | | } |
| 11 | 106 | |
|
| 8 | 107 | | if (ps.HadErrors || ps.Streams.Error.Count != 0 || ps.Streams.Verbose.Count > 0 || ps.Streams.Debug.Coun |
| 11 | 108 | | { |
| 0 | 109 | | config.Log.Verbose("PowerShell script completed with verbose/debug/warning/info messages."); |
| 0 | 110 | | config.Log.Verbose(BuildError.Text(ps)); |
| 11 | 111 | | } |
| 8 | 112 | | } |
| 1 | 113 | | catch (Exception ex) |
| 11 | 114 | | { |
| 1 | 115 | | config.Log.Error(ex, "PowerShell job failed - {Preview}", config.Code?[..Math.Min(40, config.Code.Length |
| 1 | 116 | | throw; |
| 11 | 117 | | } |
| 11 | 118 | | finally |
| 11 | 119 | | { |
| 9 | 120 | | if (config.Log.IsEnabled(LogEventLevel.Debug)) |
| 11 | 121 | | { |
| 9 | 122 | | config.Log.Debug("PowerShell job completed, releasing runspace back to pool."); |
| 11 | 123 | | } |
| 11 | 124 | | // Ensure we release the runspace back to the pool |
| 9 | 125 | | config.Pool.Release(runspace); |
| 11 | 126 | | } |
| 19 | 127 | | }; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// Creates a C# or VB.NET job using Roslyn compilation. |
| | 132 | | /// </summary> |
| | 133 | | /// <param name="config">The job configuration containing code, logger, and other parameters.</param> |
| | 134 | | /// <returns>A function that executes the job.</returns> |
| | 135 | | /// <remarks> |
| | 136 | | /// This method uses Roslyn to compile and execute C# or VB.NET code. |
| | 137 | | /// </remarks> |
| 0 | 138 | | private static Func<CancellationToken, Task> RoslynJob(JobConfig config) => RoslynJobFactory.Build(config.Code, conf |
| | 139 | | } |