-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathScriptEngine.Nested.cs
More file actions
294 lines (238 loc) · 10.7 KB
/
ScriptEngine.Nested.cs
File metadata and controls
294 lines (238 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System.Reflection;
using BlocklyNet.Core.Model;
using BlocklyNet.Scripting.Debugger;
using BlocklyNet.Scripting.Generic;
using Microsoft.Extensions.Logging;
namespace BlocklyNet.Scripting.Engine;
partial class ScriptEngine<TLogType>
{
/// <summary>
/// Helper class to manage nested script calls.
/// </summary>
protected class ScriptSite : IScriptSite<TLogType>, IGroupManagerSite
{
/// <summary>
///
/// </summary>
/// <param name="engine">The main script engine.</param>
/// <param name="parent">Parent script.</param>
/// <param name="depth">Nestring depth of the script, at least 1.</param>
/// <param name="groupManager">Group management for this nested script only.</param>
public ScriptSite(ScriptEngine<TLogType> engine, IScript<TLogType>? parent, int depth, ISiteGroupManager groupManager)
{
_depth = depth;
_engine = engine;
_groupManager = groupManager;
Parent = parent;
_groupManager.AttachSite(this);
}
private readonly ISiteGroupManager _groupManager;
private readonly int _depth;
/// <inheritdoc/>
public IScriptEngine Engine => _engine;
/// <summary>
/// Last progress information of this child script.
/// </summary>
public ProgressDetails? LastProgress => _progress.Latest;
/// <summary>
/// Progress management.
/// </summary>
private readonly ProgressManager _progress = new();
/// <summary>
/// The script starting this script.
/// </summary>
protected readonly IScript<TLogType>? Parent;
/// <inheritdoc/>
public IScriptSite? ParentSite => Parent?.Engine;
/// <summary>
/// Synchronize access to the result.
/// </summary>
private readonly object _resultLock = new();
/// <summary>
/// Set as soon as the script is finished.
/// </summary>
private bool _done = false;
/// <summary>
/// Set if the script execution failed.
/// </summary>
private Exception? _error;
/// <summary>
/// The result of the script execution - this will be the value
/// of the result variable.
/// </summary>
private object? _result;
/// <summary>
/// The main script engine.
/// </summary>
private readonly ScriptEngine<TLogType> _engine;
/// <inheritdoc/>
public IServiceProvider ServiceProvider => _engine.ServiceProvider;
/// <inheritdoc/>
public ILogger Logger => _engine.Logger;
/// <inheritdoc/>
public CancellationToken Cancellation => _engine.Cancellation;
/// <inheritdoc/>
public IScript<TLogType>? CurrentScript { get; private set; }
/// <inheritdoc/>
public IScript<TLogType>? MainScript => _engine.MainScript;
/// <inheritdoc/>
IScript? IScriptSite.CurrentScript => CurrentScript;
/// <inheritdoc/>
IScript? IScriptSite.MainScript => MainScript;
/// <inheritdoc/>
public Task<object?> EvaluateAsync(string scriptAsXml, Dictionary<string, object?> presets)
=> _engine.Parser.Parse(scriptAsXml).EvaluateAsync(presets, this);
/// <inheritdoc/>
public Task<GroupStatus?> BeginGroupAsync(string key, string? name, string? details) => _groupManager.StartAsync(key, name, details);
/// <inheritdoc/>
public Task<GroupStatus> EndGroupAsync(GroupResult result) => _groupManager.FinishAsync(result);
/// <inheritdoc/>
public GroupStatus GetGroupStatus(int index) => _groupManager[index];
/// <inheritdoc/>
public Task<TResult> RunAsync<TResult>(StartScript request, StartScriptOptions? options = null)
=> _engine.StartChildAsync<TResult>(request, CurrentScript, options, _depth, _groupManager);
/// <inheritdoc/>
public Task<T?> GetUserInputAsync<T>(string key, string? type = null, double? delay = null, bool? required = null)
=> _engine.GetUserInputAsync<T>(key, type, delay, required);
/// <inheritdoc/>
public void ReportProgress(object info, double? progress, string? name, bool? addEstimation, bool? noVisualisation)
{
/* Remember and propagate. */
_progress.Update(info, progress, name, addEstimation, noVisualisation);
_engine.ReportProgress(info, _depth);
}
/// <summary>
/// Wait for the script to finish and report either the result
/// or the exception observed.
/// </summary>
/// <returns>Result of the script.</returns>
public Task<object?> WaitForResultAsync()
{
return Task.Run(() =>
{
/* Wait for script to finish. */
while (!_done)
lock (_resultLock)
Monitor.Wait(_resultLock);
/* Report execution error. */
if (_error != null) throw _error;
/* Report result. */
return _result;
});
}
/// <summary>
/// Start a nested script.
/// </summary>
/// <param name="request">Script startup information.</param>
/// <param name="options">Optional options for the script.</param>
public void Start(StartScript request, StartScriptOptions? options = null)
{
Logger.LogTrace("Nested script '{Name}' should be started.", request.Name);
try
{
/* Create the script instance from the configuration model. */
if (Activator.CreateInstance(request.GetScriptType(), request, this, options) is not IScriptInstance<TLogType> script)
throw new ArgumentException("bad script for '{Name}' request.", request.Name);
/* Start the background execution of the script. */
Task.Factory.StartNew(() => RunScriptAsync(script), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Current).Touch();
}
catch (Exception e)
{
Logger.LogError("Unable to start nested script '{Name}': {Exception}", request.Name, e.Message);
throw;
}
}
/// <summary>
/// Process the script.
/// </summary>
private async Task RunScriptAsync(IScriptInstance<TLogType> script)
{
try
{
CurrentScript = script;
/* Run the script and remember the result. */
await script.ExecuteAsync();
_result = script.Result;
}
catch (Exception e)
{
/* Remember the error. */
if (e is AggregateException aggregation && aggregation.InnerExceptions.Count == 1)
_error = aggregation.InnerExceptions.Single();
else
_error = e;
if (_error is TargetInvocationException target)
_error = target.InnerException ?? target;
Logger.LogError("Failed to execute nested script {JobId}: {Exception}", script.JobId, _error.Message);
}
finally
{
/* Inform debugger if still active. */
_engine.Debugger?.ScriptFinished(_error);
/* Customize. */
await _engine.OnScriptDoneAsync(script, Parent);
/* Mark as done and wake up pending requests for result. */
lock (_resultLock)
{
_done = true;
Monitor.PulseAll(_resultLock);
}
}
}
/// <inheritdoc/>
public Task SingleStepAsync(Block block, Context context, ScriptDebuggerStopReason reason)
=> _engine.Debugger?.InterceptAsync(block, context, reason) ?? Task.CompletedTask;
/// <inheritdoc/>
public Task<Exception?> CatchExceptionAsync(Block block, Context context, Exception original)
=> _engine.Debugger?.InterceptExceptionAsync(block, context, original) ?? Task.FromResult<Exception?>(original);
/// <inheritdoc/>
public virtual Task BeginExecuteGroupAsync(GroupStatus status, bool recover) => Task.CompletedTask;
/// <inheritdoc/>
public virtual Task DoneExecuteGroupAsync(GroupStatus status) => Task.CompletedTask;
/// <inheritdoc/>
public Task UpdateLogAsync() => CurrentScript == null ? Task.CompletedTask : _engine.UpdateResultLogEntryAsync(CurrentScript, Parent, false);
}
/// <summary>
/// Create a new script site to allow proper customization of the engine.
/// </summary>
/// <param name="parent">Parent script.</param>
/// <param name="depth">Nesting depth.</param>
/// <param name="groupManager">Execution group management helper.</param>
/// <returns>The new site.</returns>
protected virtual ScriptSite CreateSite(IScript<TLogType>? parent, int depth, IGroupManager groupManager)
=> new(this, parent, depth, groupManager);
/// <summary>
/// Start a child script.
/// </summary>
/// <typeparam name="TResult">Type of the result data.</typeparam>
/// <param name="request">Script configuration.</param>
/// <param name="parent">Parent script.</param>
/// <param name="options">Detailed configuration of the new script.</param>
/// <param name="depth">Nestring depth of the child.</param>
/// <param name="groupManager">Parent group manager to allow for any-depth nesting.</param>
/// <returns>Task on the result.</returns>
protected virtual async Task<TResult> StartChildAsync<TResult>(StartScript request, IScript<TLogType>? parent, StartScriptOptions? options, int depth, ISiteGroupManager groupManager)
{
/* Create execution context. */
var site = CreateSite(parent, depth + 1, await groupManager.CreateNestedAsync((request as IStartGenericScript)?.ScriptId ?? string.Empty, request.Name));
using (Lock.Wait())
{
/* Create a new progress entry for this child. */
while (depth >= _allProgress.Count) _allProgress.Add([]);
_allProgress[depth].Add(site);
}
try
{
/* Start the script. */
site.Start(request, options);
/* Execute the script and report the result - or exception. */
return (TResult)(await site.WaitForResultAsync())!;
}
finally
{
using (Lock.Wait())
if (depth < _allProgress.Count)
_allProgress[depth].Remove(site);
}
}
}