Skip to content

Commit

Permalink
Added support for derived workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Mar 22, 2023
1 parent c459cbd commit 3e502ab
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
11 changes: 8 additions & 3 deletions NeuroSpeech.Eternity/EternityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,15 @@ private async Task<int> InternalProcessMessagesOnceAsync(

private static TimeSpan MaxLock = TimeSpan.FromMinutes(1);

private IWorkflow GetWorkflowInstance(EternityEntity entity, Type type, string id, DateTimeOffset eta)
private IWorkflow GetWorkflowInstance(EternityEntity entity, Type originalType, Type type, string id, DateTimeOffset eta)
{
var w = (Activator.CreateInstance(type) as IWorkflow)!;
w.Init(id, this, eta, type.GetCustomAttribute<GeneratedWorkflowAttribute>() != null);
w.Init(
id,
this,
eta,
type.GetCustomAttribute<GeneratedWorkflowAttribute>() != null,
originalType);
w.Entity = entity;
return w;
}
Expand All @@ -259,7 +264,7 @@ private async Task RunWorkflowAsync(EternityEntity entity, CancellationToken arg
var originalType = Type.GetType(entity.Name);
var workflowType = this.GetDerived(originalType);
// we need to begin...
var instance = GetWorkflowInstance(entity, workflowType, entity.ID, entity.UtcCreated);
var instance = GetWorkflowInstance(entity, originalType, workflowType, entity.ID, entity.UtcCreated);

if (entity.State == EternityEntityState.Completed
|| entity.State == EternityEntityState.Failed)
Expand Down
4 changes: 1 addition & 3 deletions NeuroSpeech.Eternity/IWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal interface IWorkflow

EternityEntity Entity { get; set; }

void Init(string id, EternityContext context, DateTimeOffset start, bool generated);
void Init(string id, EternityContext context, DateTimeOffset start, bool generated, Type originalType);
void SetCurrentTime(DateTimeOffset time);

Type InputType { get; }
Expand All @@ -21,8 +21,6 @@ internal interface IWorkflow

Task<object> RunAsync(object input);

IList<string> QueueItemList { get; }

bool IsActivityRunning { get; set; }

bool IsGenerated { get; }
Expand Down
18 changes: 9 additions & 9 deletions NeuroSpeech.Eternity/Workflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,8 @@ public static Task<string> CreateUniqueAsync(

bool IWorkflow.IsActivityRunning { get => IsActivityRunning; set => IsActivityRunning = value; }

IList<string> IWorkflow.QueueItemList { get; } = new List<string>();

private EternityEntity _entity;
private Type originalType;
EternityEntity IWorkflow.Entity { get => _entity; set => _entity = value; }

/// <summary>
Expand Down Expand Up @@ -244,9 +243,10 @@ public Task SaveAsync()

public abstract Task<TOutput> RunAsync(TInput input);

void IWorkflow.Init(string id, EternityContext context, DateTimeOffset start, bool generated)
void IWorkflow.Init(string id, EternityContext context, DateTimeOffset start, bool generated, Type originalType)
{
this.ID = id;
this.originalType = originalType;
this.Context = context;
this.CurrentUtc = start;
this.generated = generated;
Expand Down Expand Up @@ -292,15 +292,15 @@ public Task Delay(TimeSpan timeout)
[EditorBrowsable(EditorBrowsableState.Never)]
public Task<T> InternalScheduleResultAsync<T>(string method, params object?[] items)
{
var fx = typeof(TWorkflow).GetVirtualMethod(method);
var fx = originalType.GetVirtualMethod(method);
var unique = fx.GetCustomAttribute<ActivityAttribute>();
return Context.ScheduleAsync<T>(this, unique.UniqueParameters, ID, CurrentUtc, fx, items);
}

[EditorBrowsable(EditorBrowsableState.Never)]
public async Task InternalScheduleAsync(string method, params object?[] items)
{
var fx = typeof(TWorkflow).GetVirtualMethod(method);
var fx = originalType.GetVirtualMethod(method);
var unique = fx.GetCustomAttribute<ActivityAttribute>();
await Context.ScheduleAsync<object>(this, unique.UniqueParameters, ID, CurrentUtc, fx, items);
}
Expand All @@ -312,7 +312,7 @@ public Task<T> InternalScheduleAtResultAsync<T>(DateTimeOffset at, string method
{
throw new ArgumentException($"{nameof(at)} cannot be in the past");
}
var fx = typeof(TWorkflow).GetVirtualMethod(method);
var fx = originalType.GetVirtualMethod(method);
var unique = fx.GetCustomAttribute<ActivityAttribute>();
return Context.ScheduleAsync<T>(this, unique.UniqueParameters, ID, at, fx, items);
}
Expand All @@ -324,7 +324,7 @@ public async Task InternalScheduleAtAsync(DateTimeOffset at, string method, para
{
throw new ArgumentException($"{nameof(at)} cannot be in the past");
}
var fx = typeof(TWorkflow).GetVirtualMethod(method);
var fx = originalType.GetVirtualMethod(method);
var unique = fx.GetCustomAttribute<ActivityAttribute>();
await Context.ScheduleAsync<object>(this, unique.UniqueParameters, ID, at, fx, items);
}
Expand All @@ -336,7 +336,7 @@ public Task<T> InternalScheduleAfterResultAsync<T>(TimeSpan at, string method, p
{
throw new ArgumentException($"{nameof(at)} cannot be in the past");
}
var fx = typeof(TWorkflow).GetVirtualMethod(method);
var fx = originalType.GetVirtualMethod(method);
var unique = fx.GetCustomAttribute<ActivityAttribute>();
return Context.ScheduleAsync<T>(this, unique.UniqueParameters, ID, CurrentUtc.Add(at), fx, items);
}
Expand All @@ -348,7 +348,7 @@ public async Task InternalScheduleAfterAsync(TimeSpan at, string method, params
{
throw new ArgumentException($"{nameof(at)} cannot be in the past");
}
var fx = typeof(TWorkflow).GetVirtualMethod(method);
var fx = originalType.GetVirtualMethod(method);
var unique = fx.GetCustomAttribute<ActivityAttribute>();
await Context.ScheduleAsync<object>(this, unique.UniqueParameters, ID, CurrentUtc.Add(at), fx, items);
}
Expand Down

0 comments on commit 3e502ab

Please sign in to comment.