| | | 1 | | using Kestrun.Languages; |
| | | 2 | | using Kestrun.SharedState; |
| | | 3 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 4 | | using Serilog.Events; |
| | | 5 | | using System.Reflection; |
| | | 6 | | |
| | | 7 | | namespace Kestrun.Scheduling; |
| | | 8 | | |
| | | 9 | | internal static class RoslynJobFactory |
| | | 10 | | { |
| | | 11 | | public static Func<CancellationToken, Task> Build( |
| | | 12 | | string code, |
| | | 13 | | Serilog.ILogger log, |
| | | 14 | | string[]? extraImports, |
| | | 15 | | Assembly[]? extraRefs, |
| | | 16 | | IReadOnlyDictionary<string, object?>? locals, |
| | | 17 | | LanguageVersion languageVersion = LanguageVersion.CSharp12) |
| | | 18 | | { |
| | 6 | 19 | | if (log.IsEnabled(LogEventLevel.Debug)) |
| | | 20 | | { |
| | 6 | 21 | | log.Debug("Building C# job, code length={Length}, imports={ImportsCount}, refs={RefsCount}, lang={Lang}", |
| | 6 | 22 | | code?.Length, extraImports?.Length ?? 0, extraRefs?.Length ?? 0, languageVersion); |
| | | 23 | | } |
| | | 24 | | |
| | 6 | 25 | | var script = CSharpDelegateBuilder.Compile(code: code, log: log, extraImports: extraImports, extraRefs: extraRef |
| | 5 | 26 | | var runner = script.CreateDelegate(); // returns ScriptRunner<object?> |
| | 5 | 27 | | if (log.IsEnabled(LogEventLevel.Debug)) |
| | | 28 | | { |
| | 5 | 29 | | log.Debug("C# job runner created, type={Type}", runner.GetType()); |
| | | 30 | | } |
| | | 31 | | /* 5️⃣ Returned delegate = *execute only* */ |
| | 5 | 32 | | return async ct => |
| | 5 | 33 | | { |
| | 5 | 34 | | if (log.IsEnabled(LogEventLevel.Debug)) |
| | 5 | 35 | | { |
| | 5 | 36 | | log.Debug("Executing C# job at {Now:O}", DateTimeOffset.UtcNow); |
| | 5 | 37 | | } |
| | 5 | 38 | | |
| | 5 | 39 | | var globals = locals is { Count: > 0 } |
| | 5 | 40 | | ? new CsGlobals(SharedStateStore.Snapshot(), locals) |
| | 5 | 41 | | : new CsGlobals(SharedStateStore.Snapshot()); |
| | 5 | 42 | | _ = await runner(globals, ct).ConfigureAwait(false); |
| | 10 | 43 | | }; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | |
| | | 47 | | |
| | | 48 | | public static Func<CancellationToken, Task> Build( |
| | | 49 | | string code, |
| | | 50 | | Serilog.ILogger log, |
| | | 51 | | string[]? extraImports, |
| | | 52 | | Assembly[]? extraRefs, |
| | | 53 | | IReadOnlyDictionary<string, object?>? locals, |
| | | 54 | | Microsoft.CodeAnalysis.VisualBasic.LanguageVersion languageVersion = Microsoft.CodeAnalysis.VisualBasic.LanguageV |
| | | 55 | | { |
| | 1 | 56 | | if (log.IsEnabled(LogEventLevel.Debug)) |
| | | 57 | | { |
| | 1 | 58 | | log.Debug("Building C# job, code length={Length}, imports={ImportsCount}, refs={RefsCount}, lang={Lang}", |
| | 1 | 59 | | code?.Length, extraImports?.Length ?? 0, extraRefs?.Length ?? 0, languageVersion); |
| | | 60 | | } |
| | | 61 | | |
| | 1 | 62 | | var script = VBNetDelegateBuilder.Compile<object>(code: code, log: log, extraImports: extraImports, extraRefs: e |
| | | 63 | | |
| | 1 | 64 | | return async ct => |
| | 1 | 65 | | { |
| | 1 | 66 | | if (log.IsEnabled(LogEventLevel.Debug)) |
| | 1 | 67 | | { |
| | 1 | 68 | | log.Debug("Executing C# job at {Now:O}", DateTimeOffset.UtcNow); |
| | 1 | 69 | | } |
| | 1 | 70 | | |
| | 1 | 71 | | var globals = new CsGlobals(SharedStateStore.Snapshot()); |
| | 1 | 72 | | _ = await script(globals).ConfigureAwait(false); |
| | 2 | 73 | | }; |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | |