Skip to content

Commit

Permalink
Fix issue #82
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Aug 31, 2024
1 parent 827521f commit ebca6e3
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 39 deletions.
9 changes: 9 additions & 0 deletions src/net/Wexflow.Core.Service.Client/WexflowServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public Guid StartWorkflow(int id, string username, string password)
return Guid.Parse(instanceId.Replace("\"", string.Empty));
}

public Guid StartWorkflowWithVariables(string payload, string username, string password)
{
var uri = $"{Uri}/start-with-variables";
var webClient = new WebClient();
webClient.Headers.Add("Authorization", $"Basic {Base64Encode($"{username}:{GetMd5(password)}")}");
var instanceId = webClient.UploadString(uri, payload);
return Guid.Parse(instanceId.Replace("\"", string.Empty));
}

public void StopWorkflow(int id, Guid instanceId, string username, string password)
{
var uri = $"{Uri}/stop?w={id}&i={instanceId}";
Expand Down
5 changes: 3 additions & 2 deletions src/net/Wexflow.Core/WexflowEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,9 @@ public Workflow GetWorkflow(int workflowId)
/// </summary>
/// <param name="startedBy">Username of the user that started the workflow.</param>
/// <param name="workflowId">Workflow Id.</param>
/// <param name="restVariables">Rest variables</param>
/// <returns>Instance id.</returns>
public Guid StartWorkflow(string startedBy, int workflowId)
public Guid StartWorkflow(string startedBy, int workflowId, List<Variable> restVariables = null)
{
var wf = GetWorkflow(workflowId);

Expand All @@ -903,7 +904,7 @@ public Guid StartWorkflow(string startedBy, int workflowId)
{
if (wf.IsEnabled)
{
var instanceId = wf.StartAsync(startedBy);
var instanceId = wf.StartAsync(startedBy, restVariables);
return instanceId;
}
}
Expand Down
24 changes: 18 additions & 6 deletions src/net/Wexflow.Core/Workflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Wexflow.Core
/// </summary>
public class Workflow
{
private readonly object padLock = new object();
private readonly object padlock = new object();

/// <summary>
/// This constant is used to determine the key size of the encryption algorithm in bits.
Expand Down Expand Up @@ -899,8 +899,9 @@ private string GetWorkflowSetting(XDocument xdoc, string name, bool throwExcepti
/// Starts this workflow asynchronously.
/// </summary>
/// <param name="startedBy">Username of the user that started the workflow.</param>
/// <param name="restVariables">Rest variables</param>
/// <returns>Instance Id.</returns>
public Guid StartAsync(string startedBy)
public Guid StartAsync(string startedBy, List<Variable> restVariables = null)
{
if (IsRunning && !EnableParallelJobs)
{
Expand Down Expand Up @@ -928,14 +929,14 @@ public Guid StartAsync(string startedBy)
RestVariables = RestVariables,
StartedBy = startedBy
};
return workflow.StartAsync(startedBy);
return workflow.StartAsync(startedBy, restVariables);
}

StartedOn = DateTime.Now;
StartedBy = startedBy;
var instanceId = Guid.NewGuid();
var warning = false;
var thread = new Thread(() => StartSync(startedBy, instanceId, ref warning));
var thread = new Thread(() => StartSync(startedBy, instanceId, ref warning, restVariables));
_thread = thread;
thread.Start();

Expand All @@ -948,19 +949,30 @@ public Guid StartAsync(string startedBy)
/// <param name="startedBy">Username of the user that started the workflow.</param>
/// <param name="instanceId">Instance id.</param>
/// <param name="resultWarning">Indicates whether the final result is warning or not.</param>
public bool StartSync(string startedBy, Guid instanceId, ref bool resultWarning)
/// <param name="restVariables">Rest variables</param>
/// <returns>Result.</returns>
public bool StartSync(string startedBy, Guid instanceId, ref bool resultWarning, List<Variable> restVariables = null)
{
var resultSuccess = true;

try
{
lock (padLock)
lock (padlock)
{
StartedOn = DateTime.Now;
StartedBy = startedBy;
InstanceId = instanceId;
Jobs.Add(InstanceId, this);

//
// Add rest variables
//
if (restVariables != null)
{
RestVariables.Clear();
RestVariables.AddRange(restVariables);
}

//
// Parse the workflow definition (Global variables and local variables.)
//
Expand Down
21 changes: 20 additions & 1 deletion src/net/Wexflow.Server.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,26 @@ private static void Main()
void startWorkflow()
{
Thread.CurrentThread.IsBackground = true;
var jobId = client.StartWorkflow(41, username, password);
//var jobId = client.StartWorkflow(41, username, password);
//Console.WriteLine(jobId);

var payload = $@"
{{
""WorkflowId"":131,
""Variables"":[
{{
""Name"":""restVar1"",
""Value"":""C:\\WexflowTesting\\file1.txt""
}},
{{
""Name"":""restVar2"",
""Value"":""C:\\WexflowTesting\\file2.txt""
}}
]
}}
";

var jobId = client.StartWorkflowWithVariables(payload, username, password);
Console.WriteLine(jobId);
}

Expand Down
12 changes: 4 additions & 8 deletions src/net/Wexflow.Server/WexflowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,10 @@ private void StartWorkflowWithVariables()
var workflowId = o.Value<int>("WorkflowId");
var variables = o.Value<JArray>("Variables");
var vars = new List<Core.Variable>();
var restVariables = new List<Core.Variable>();
foreach (var variable in variables)
{
vars.Add(new Core.Variable { Key = variable.Value<string>("Name"), Value = variable.Value<string>("Value") });
restVariables.Add(new Core.Variable { Key = variable.Value<string>("Name"), Value = variable.Value<string>("Value") });
}
var workflow = WexflowServer.WexflowEngine.Workflows.First(w => w.Id == workflowId);
Expand All @@ -587,9 +587,7 @@ private void StartWorkflowWithVariables()
{
if (user.UserProfile == Core.Db.UserProfile.SuperAdministrator)
{
workflow.RestVariables.Clear();
workflow.RestVariables.AddRange(vars);
var instanceId = WexflowServer.WexflowEngine.StartWorkflow(username, workflowId);
var instanceId = WexflowServer.WexflowEngine.StartWorkflow(username, workflowId, restVariables);
var resStr = JsonConvert.SerializeObject(instanceId.ToString());
var resBytes = Encoding.UTF8.GetBytes(resStr);
Expand All @@ -606,9 +604,7 @@ private void StartWorkflowWithVariables()
var check = WexflowServer.WexflowEngine.CheckUserWorkflow(user.GetDbId(), workflowDbId);
if (check)
{
workflow.RestVariables.Clear();
workflow.RestVariables.AddRange(vars);
var instanceId = WexflowServer.WexflowEngine.StartWorkflow(username, workflowId);
var instanceId = WexflowServer.WexflowEngine.StartWorkflow(username, workflowId, restVariables);
var resStr = JsonConvert.SerializeObject(instanceId.ToString());
var resBytes = Encoding.UTF8.GetBytes(resStr);
Expand Down
14 changes: 13 additions & 1 deletion src/netcore/Wexflow.Core.Service.Client/WexflowServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ private static async Task<string> DownloadStringAsync(HttpClient client, string
return responseString;
}

private static async Task<string> UploadStringAsync(HttpClient client, string url, string username, string password)
private static async Task<string> UploadStringAsync(HttpClient client, string url, string username, string password, string body = "")
{
HttpRequestMessage request = new(HttpMethod.Post, url);
request.Headers.Add("Authorization", $"Basic {Base64Encode($"{username}:{GetMd5(password)}")}");
if (!string.IsNullOrEmpty(body))
{
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
}
var response = await client.SendAsync(request);
var byteArray = await response.Content.ReadAsByteArrayAsync();
var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
Expand Down Expand Up @@ -72,6 +76,14 @@ public async Task<Guid> StartWorkflow(int id, string username, string password)
return Guid.Parse(instanceId.Replace("\"", string.Empty));
}

public async Task<Guid> StartWorkflowWithVariables(string payload, string username, string password)
{
var uri = $"{Uri}/start-with-variables";
using HttpClient webClient = new();
var instanceId = await UploadStringAsync(webClient, uri, username, password, payload);
return Guid.Parse(instanceId.Replace("\"", string.Empty));
}

public async Task StopWorkflow(int id, Guid instanceId, string username, string password)
{
var uri = $"{Uri}/stop?w={id}&i={instanceId}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private readonly void Sort()
Array.Sort(_changes, 0, _count, Comparer.ColumnDefault);
}

public readonly override string ToString()
public override readonly string ToString()
{
return _count.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FileState(string directory, string path) : this()
Path = path;
}

public readonly override string ToString()
public override readonly string ToString()
{
return Path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public Bucket(string directory, string file, int valueIndex)
}
public readonly bool IsEmpty => ValuesIndex == 0;

public readonly override string ToString()
public override readonly string ToString()
{
return IsEmpty ? "empty" : Key.ToString();
}
Expand All @@ -245,7 +245,7 @@ private struct FullPath
public string Directory;
public string File;

public readonly override string ToString()
public override readonly string ToString()
{
return File;
}
Expand Down
5 changes: 3 additions & 2 deletions src/netcore/Wexflow.Core/WexflowEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,9 @@ public Workflow GetWorkflow(int workflowId)
/// </summary>
/// <param name="startedBy">Username of the user that started the workflow.</param>
/// <param name="workflowId">Workflow Id.</param>
/// <param name="restVariables">Rest variables</param>
/// <returns>Instance id.</returns>
public Guid StartWorkflow(string startedBy, int workflowId)
public Guid StartWorkflow(string startedBy, int workflowId, List<Variable> restVariables = null)
{
var wf = GetWorkflow(workflowId);

Expand All @@ -896,7 +897,7 @@ public Guid StartWorkflow(string startedBy, int workflowId)
{
if (wf.IsEnabled)
{
var instanceId = wf.StartAsync(startedBy);
var instanceId = wf.StartAsync(startedBy, restVariables);
return instanceId;
}
}
Expand Down
24 changes: 18 additions & 6 deletions src/netcore/Wexflow.Core/Workflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Wexflow.Core
/// </summary>
public class Workflow
{
private readonly object padLock = new();
private readonly object padlock = new();

/// <summary>
/// This constant is used to determine the key size of the encryption algorithm in bits.
Expand Down Expand Up @@ -951,8 +951,9 @@ private string GetWorkflowSetting(XDocument xdoc, string name, bool throwExcepti
/// Starts this workflow asynchronously.
/// </summary>
/// <param name="startedBy">Username of the user that started the workflow.</param>
/// <param name="restVariables">Rest variables</param>
/// <returns>Instance Id.</returns>
public Guid StartAsync(string startedBy)
public Guid StartAsync(string startedBy, List<Variable> restVariables = null)
{
if (IsRunning && !EnableParallelJobs)
{
Expand Down Expand Up @@ -981,14 +982,14 @@ public Guid StartAsync(string startedBy)
RestVariables = RestVariables,
StartedBy = startedBy
};
return workflow.StartAsync(startedBy);
return workflow.StartAsync(startedBy, restVariables);
}

StartedOn = DateTime.Now;
StartedBy = startedBy;
var instanceId = Guid.NewGuid();
var warning = false;
Thread thread = new(() => StartSync(startedBy, instanceId, ref warning));
Thread thread = new(() => StartSync(startedBy, instanceId, ref warning, restVariables));
_thread = thread;
thread.Start();

Expand All @@ -1001,19 +1002,30 @@ public Guid StartAsync(string startedBy)
/// <param name="startedBy">Username of the user that started the workflow.</param>
/// <param name="instanceId">Instance id.</param>
/// <param name="resultWarning">Indicates whether the final result is warning or not.</param>
public bool StartSync(string startedBy, Guid instanceId, ref bool resultWarning)
/// <param name="restVariables">Rest variables</param>
/// <returns>Result.</returns>
public bool StartSync(string startedBy, Guid instanceId, ref bool resultWarning, List<Variable> restVariables = null)
{
var resultSuccess = true;

try
{
lock (padLock)
lock (padlock)
{
StartedOn = DateTime.Now;
StartedBy = startedBy;
InstanceId = instanceId;
Jobs.Add(InstanceId, this);

//
// Add rest variables
//
if (restVariables != null)
{
RestVariables.Clear();
RestVariables.AddRange(restVariables);
}

//
// Parse the workflow definition (Global variables and local variables.)
//
Expand Down
20 changes: 19 additions & 1 deletion src/netcore/Wexflow.Server.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,25 @@
async void startWorkflow()
{
Thread.CurrentThread.IsBackground = true;
var jobId = await client.StartWorkflow(41, username, password);
//var jobId = await client.StartWorkflow(41, username, password);
//Console.WriteLine(jobId);
var payload = $@"
{{
""WorkflowId"":138,
""Variables"":[
{{
""Name"":""restVar1"",
""Value"":""C:\\WexflowTesting\\file1.txt""
}},
{{
""Name"":""restVar2"",
""Value"":""C:\\WexflowTesting\\file2.txt""
}}
]
}}
";

var jobId = await client.StartWorkflowWithVariables(payload, username, password);
Console.WriteLine(jobId);
}

Expand Down
12 changes: 4 additions & 8 deletions src/netcore/Wexflow.Server/WexflowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,10 @@ private void StartWorkflowWithVariables()
var workflowId = o.Value<int>("WorkflowId");
var variables = o.Value<JArray>("Variables");
List<Core.Variable> vars = [];
List<Core.Variable> restVariables = [];
foreach (var variable in variables)
{
vars.Add(new Core.Variable { Key = variable.Value<string>("Name"), Value = variable.Value<string>("Value") });
restVariables.Add(new Core.Variable { Key = variable.Value<string>("Name"), Value = variable.Value<string>("Value") });
}
var workflow = WexflowServer.WexflowEngine.Workflows.First(w => w.Id == workflowId);
Expand All @@ -545,9 +545,7 @@ private void StartWorkflowWithVariables()
{
if (user.UserProfile == Core.Db.UserProfile.SuperAdministrator)
{
workflow.RestVariables.Clear();
workflow.RestVariables.AddRange(vars);
var instanceId = WexflowServer.WexflowEngine.StartWorkflow(username, workflowId);
var instanceId = WexflowServer.WexflowEngine.StartWorkflow(username, workflowId, restVariables);
await context.Response.WriteAsync(JsonConvert.SerializeObject(instanceId.ToString()));
}
Expand All @@ -557,9 +555,7 @@ private void StartWorkflowWithVariables()
var check = WexflowServer.WexflowEngine.CheckUserWorkflow(user.GetDbId(), workflowDbId);
if (check)
{
workflow.RestVariables.Clear();
workflow.RestVariables.AddRange(vars);
var instanceId = WexflowServer.WexflowEngine.StartWorkflow(username, workflowId);
var instanceId = WexflowServer.WexflowEngine.StartWorkflow(username, workflowId, restVariables);
await context.Response.WriteAsync(JsonConvert.SerializeObject(instanceId.ToString()));
}
Expand Down

0 comments on commit ebca6e3

Please sign in to comment.