-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerSession.cs
More file actions
46 lines (38 loc) · 1.33 KB
/
Copy pathWorkerSession.cs
File metadata and controls
46 lines (38 loc) · 1.33 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
using DotPython.Protocol;
namespace DotPython.Worker;
public sealed class WorkerSession : IAsyncDisposable
{
private readonly WorkerProcessClient _client;
private readonly WorkerGenerationScope _scope;
private int _disposed;
internal WorkerSession(WorkerProcessClient client, WorkerGenerationScope scope, Guid sessionId)
{
_client = client;
_scope = scope;
SessionId = sessionId;
}
public Guid SessionId { get; }
public WorkerIdentity WorkerIdentity => _scope.Identity;
public Task<WorkerExecuteResponse> ExecuteAsync(
string code,
string fileName = "<worker>",
long instructionLimit = 1_000_000,
CancellationToken cancellationToken = default
)
{
ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) != 0, this);
return _client.ExecuteAsync(SessionId, code, fileName, instructionLimit, cancellationToken);
}
public void ValidateHandle(WorkerObjectHandle handle)
{
ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) != 0, this);
_client.ValidateHandle(handle, SessionId);
}
public async ValueTask DisposeAsync()
{
if (Interlocked.Exchange(ref _disposed, 1) == 0)
{
await _client.CloseSessionAsync(SessionId).ConfigureAwait(false);
}
}
}