Initial commit
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using System.Collections.Generic;
|
||||
using RudderSdk.Core.Models;
|
||||
|
||||
namespace RudderSdk.Core;
|
||||
|
||||
/// <summary>Snapshot of one running scenario plan.</summary>
|
||||
public sealed class PlanRun
|
||||
{
|
||||
internal PlanRun(
|
||||
string runId,
|
||||
string planId,
|
||||
string scenarioId,
|
||||
string userId,
|
||||
IReadOnlyList<string> activeNodeIds,
|
||||
ExecutionPlan plan)
|
||||
{
|
||||
RunId = runId;
|
||||
PlanId = planId;
|
||||
ScenarioId = scenarioId;
|
||||
UserId = userId;
|
||||
ActiveNodeIds = activeNodeIds;
|
||||
Plan = plan;
|
||||
}
|
||||
|
||||
/// <summary>Server-issued run id.</summary>
|
||||
public string RunId { get; }
|
||||
|
||||
/// <summary>Plan id.</summary>
|
||||
public string PlanId { get; }
|
||||
|
||||
/// <summary>Scenario id.</summary>
|
||||
public string ScenarioId { get; }
|
||||
|
||||
/// <summary>Player the run belongs to.</summary>
|
||||
public string UserId { get; }
|
||||
|
||||
/// <summary>Ids of the currently active nodes.</summary>
|
||||
public IReadOnlyList<string> ActiveNodeIds { get; }
|
||||
|
||||
/// <summary>The execution plan being run.</summary>
|
||||
public ExecutionPlan Plan { get; }
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace RudderSdk.Core;
|
||||
|
||||
/// <summary>Payload of <see cref="ScenarioService.OnScenarioFailed"/>.</summary>
|
||||
public sealed class ScenarioFailedEvent
|
||||
{
|
||||
internal ScenarioFailedEvent(PlanRun run, string nodeId, Exception exception)
|
||||
{
|
||||
Run = run;
|
||||
NodeId = nodeId;
|
||||
Exception = exception;
|
||||
}
|
||||
|
||||
/// <summary>The failed run.</summary>
|
||||
public PlanRun Run { get; }
|
||||
|
||||
/// <summary>Node the failure happened at.</summary>
|
||||
public string NodeId { get; }
|
||||
|
||||
/// <summary>The error that failed the run.</summary>
|
||||
public Exception Exception { get; }
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using RudderSdk.Core.Models;
|
||||
|
||||
namespace RudderSdk.Core;
|
||||
|
||||
/// <summary>Context of the scenario node a session was created for.</summary>
|
||||
public class ScenarioNodeContext
|
||||
{
|
||||
private readonly ScenarioService _service;
|
||||
|
||||
internal ScenarioNodeContext(ScenarioService service, PlanRun run, ExecutionPlanNode node)
|
||||
{
|
||||
_service = service;
|
||||
Run = run;
|
||||
Node = node;
|
||||
Data = node?.Data as JObject ?? new JObject();
|
||||
}
|
||||
|
||||
/// <summary>The run this node belongs to.</summary>
|
||||
public PlanRun Run { get; }
|
||||
|
||||
/// <summary>The plan node.</summary>
|
||||
public ExecutionPlanNode Node { get; }
|
||||
|
||||
/// <summary>Run id.</summary>
|
||||
public string RunId => Run.RunId;
|
||||
|
||||
/// <summary>Plan id.</summary>
|
||||
public string PlanId => Run.PlanId;
|
||||
|
||||
/// <summary>Scenario id.</summary>
|
||||
public string ScenarioId => Run.ScenarioId;
|
||||
|
||||
/// <summary>Node id.</summary>
|
||||
public string NodeId => Node.Id;
|
||||
|
||||
/// <summary>Node type.</summary>
|
||||
public string Type => Node.Type;
|
||||
|
||||
/// <summary>Node data payload.</summary>
|
||||
public JObject Data { get; }
|
||||
|
||||
/// <summary>Reads a typed value from the node data.</summary>
|
||||
public T Get<T>(string key, T defaultValue = default!)
|
||||
{
|
||||
if (Data == null || !Data.TryGetValue(key, out var value))
|
||||
return defaultValue;
|
||||
try { return value.ToObject<T>() ?? defaultValue; }
|
||||
catch { return defaultValue; }
|
||||
}
|
||||
|
||||
/// <summary>Deserializes the whole node data payload.</summary>
|
||||
public T Get<T>()
|
||||
{
|
||||
try { return Data == null ? default! : Data.ToObject<T>() ?? default!; }
|
||||
catch { return default!; }
|
||||
}
|
||||
|
||||
/// <summary>Returns the node data as a plain dictionary.</summary>
|
||||
public Dictionary<string, object> AsObjectDictionary()
|
||||
{
|
||||
return Data?.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
|
||||
}
|
||||
|
||||
internal Task CompleteAsync(string handle, CancellationToken cancellationToken = default)
|
||||
=> _service.CompleteNodeAsync(RunId, NodeId, handle, cancellationToken);
|
||||
|
||||
internal void Complete(string handle)
|
||||
{
|
||||
_ = CompleteAsync(handle);
|
||||
}
|
||||
|
||||
internal Task AddProgressAsync(string counterKey, long amount, CancellationToken cancellationToken = default)
|
||||
=> _service.UpdateProgressAsync(RunId, NodeId, counterKey, amount, cancellationToken);
|
||||
}
|
||||
Reference in New Issue
Block a user