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