Skip to content

Commit

Permalink
General cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Jul 5, 2023
1 parent 0bde627 commit f0876a7
Show file tree
Hide file tree
Showing 18 changed files with 186 additions and 148 deletions.
5 changes: 5 additions & 0 deletions src/net/Wexflow.Clients.CommandLine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@ private enum Operation
Reject
}

// ReSharper disable once ClassNeverInstantiated.Local
private class Options
{
[Option('o', "operation", Required = true, HelpText = "start|suspend|resume|stop|approve|reject")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public Operation Operation { get; set; }

[Option('i', "workflowId", Required = true, HelpText = "Workflow Id")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public int WorkflowId { get; set; }

[Option('j', "jobId", Required = false, HelpText = "Job instance id (Guid)")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public string JobId { get; set; }

[Option('w', "wait", Required = false, HelpText = "Wait until workflow finishes", Default = false)]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public bool Wait { get; set; }
}

Expand Down
2 changes: 2 additions & 0 deletions src/net/Wexflow.Server/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<!-- ReSharper disable once ResxNotResolved -->
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<!-- ReSharper disable once ResxNotResolved -->
<data name="app" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\app.manifest;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions src/net/Wexflow.Server/Wexflow.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="WexflowServer.resx">
<DependentUpon>WexflowServer.cs</DependentUpon>
Expand Down
2 changes: 1 addition & 1 deletion src/net/Wexflow.Tasks.CsvToSql/CsvToSql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private bool ConvertCsvToSql(string csvPath, string sqlPath, string tableName, s
using (var sr = new StreamReader(csvPath))
using (var sw = new StreamWriter(sqlPath))
{
var columnsLine = sr.ReadLine(); // First line contains columns
var columnsLine = sr.ReadLine() ?? throw new InvalidOperationException(); // First line contains columns
string line;
while (!string.IsNullOrEmpty(line = sr.ReadLine()))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ private void InitFileSystemWatcher()
{
Thread.Sleep(1);
}
// ReSharper disable once FunctionNeverReturns
}

private void OnCreated(object source, FileSystemEventArgs e)
Expand Down
3 changes: 2 additions & 1 deletion src/net/Wexflow.Tasks.FilesLoaderEx/MiscExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public static class MiscExtensions
{
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int n)
{
return source.Skip(Math.Max(0, source.Count() - n));
var enumerable = source as T[] ?? source.ToArray();
return enumerable.Skip(Math.Max(0, enumerable.Length - n));
}
}
}
8 changes: 4 additions & 4 deletions src/net/Wexflow.Tasks.MailsSender/Mail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public void Send(string host, int port, bool enableSsl, string user, string pass

public static Mail Parse(MailsSender mailsSender, XElement xe, FileInf[] attachments)
{
var from = mailsSender.ParseVariables(xe.XPathSelectElement("From").Value);
var to = mailsSender.ParseVariables(xe.XPathSelectElement("To").Value).Split(',');
var from = mailsSender.ParseVariables(xe.XPathSelectElement("From")?.Value);
var to = mailsSender.ParseVariables(xe.XPathSelectElement("To")?.Value).Split(',');

string[] cc = { };
var ccElement = xe.XPathSelectElement("Cc");
Expand All @@ -99,8 +99,8 @@ public static Mail Parse(MailsSender mailsSender, XElement xe, FileInf[] attachm
bcc = mailsSender.ParseVariables(bccElement.Value).Split(',');
}

var subject = mailsSender.ParseVariables(xe.XPathSelectElement("Subject").Value);
var body = mailsSender.ParseVariables(xe.XPathSelectElement("Body").Value);
var subject = mailsSender.ParseVariables(xe.XPathSelectElement("Subject")?.Value);
var body = mailsSender.ParseVariables(xe.XPathSelectElement("Body")?.Value);

return new Mail(from, to, cc, bcc, subject, body, attachments);
}
Expand Down
2 changes: 1 addition & 1 deletion src/net/Wexflow.Tasks.ProcessInfo/ProcessInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override TaskStatus Run()
var xprocess = new XElement("Process"
, new XAttribute("id", process.Id)
, new XAttribute("processName", process.ProcessName)
, new XAttribute("fileName", process.MainModule.FileName)
, new XAttribute("fileName", process.MainModule != null ? process.MainModule.FileName : string.Empty)
, new XAttribute("startTime", $"{process.StartTime:yyyy-MM-dd HH:mm:ss.fff}")
, new XAttribute("machineName", process.MachineName)
, new XAttribute("sessionId", process.SessionId)
Expand Down
4 changes: 3 additions & 1 deletion src/net/Wexflow.Tasks.Torrent/Torrent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Wexflow.Tasks.Torrent
{
public class Torrent : Task
{
private const double Tolerance = 0.001;

public string SaveFolder { get; set; }

public Torrent(XElement xe, Workflow wf) : base(xe, wf)
Expand Down Expand Up @@ -97,7 +99,7 @@ private bool DownloadTorrent(string path)
{
Thread.Sleep(1000);

if (manager.Progress == 100.0)
if (Math.Abs(manager.Progress - 100.0) < Tolerance)
{
// If we want to stop a torrent, or the engine for whatever reason, we call engine.StopAll()
_ = engine.StopAllAsync();
Expand Down
185 changes: 99 additions & 86 deletions src/netcore/Wexflow.Clients.CommandLine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@ private enum Operation
Reject
}

// ReSharper disable once ClassNeverInstantiated.Local
private class Options
{
[Option('o', "operation", Required = true, HelpText = "start|suspend|resume|stop|approve|reject")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public Operation Operation { get; set; }

[Option('i', "workflowId", Required = true, HelpText = "Workflow Id")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public int WorkflowId { get; set; }

[Option('j', "jobId", Required = false, HelpText = "Job instance id (Guid)")]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public string JobId { get; set; }

[Option('w', "wait", Required = false, HelpText = "Wait until workflow finishes", Default = false)]
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public bool Wait { get; set; }
}

Expand All @@ -45,92 +50,100 @@ private static void Main(string[] args)
.Build();

Parser parser = new(cfg => cfg.CaseInsensitiveEnumValues = true);
var res = parser.ParseArguments<Options>(args)
.WithParsed(async o =>
{
WexflowServiceClient client = new(config["WexflowWebServiceUri"]);
var username = config["Username"];
var password = config["Password"];
var workflows = await client.Search(string.Empty, username, password);
if (workflows.All(w => w.Id != o.WorkflowId))
{
Console.WriteLine("Workflow id {0} is incorrect.", o.WorkflowId);
return;
}
WorkflowInfo workflow;
switch (o.Operation)
{
case Operation.Start:
var instanceId = await client.StartWorkflow(o.WorkflowId, username, password);
Console.WriteLine("JobId: {0}", instanceId);
if (o.Wait)
{
Thread.Sleep(1000);
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
var isRunning = workflow.IsRunning;
while (isRunning)
{
Thread.Sleep(100);
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
isRunning = workflow.IsRunning;
}
}
break;
case Operation.Suspend:
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
if (!workflow.IsRunning)
{
Console.WriteLine("Workflow {0} is not running to be suspended.", o.WorkflowId);
return;
}
await client.SuspendWorkflow(o.WorkflowId, Guid.Parse(o.JobId), username, password);
break;
case Operation.Stop:
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
if (!workflow.IsRunning)
{
Console.WriteLine("Workflow {0} is not running to be stopped.", o.WorkflowId);
return;
}
await client.StopWorkflow(o.WorkflowId, Guid.Parse(o.JobId), username, password);
break;
case Operation.Resume:
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
if (!workflow.IsPaused)
{
Console.WriteLine("Workflow {0} is not suspended to be resumed.", o.WorkflowId);
return;
}
await client.ResumeWorkflow(o.WorkflowId, Guid.Parse(o.JobId), username, password);
break;
case Operation.Approve:
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
if (!workflow.IsWaitingForApproval)
{
Console.WriteLine("Workflow {0} is not waiting for approval to be approved.", o.WorkflowId);
return;
}
await client.ApproveWorkflow(o.WorkflowId, Guid.Parse(o.JobId), username, password);
break;
case Operation.Reject:
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
if (!workflow.IsWaitingForApproval)
{
Console.WriteLine("Workflow {0} is not waiting for approval to be rejected.", o.WorkflowId);
return;
}
await client.RejectWorkflow(o.WorkflowId, Guid.Parse(o.JobId), username, password);
break;
}
});

async void Action(Options o)
{
WexflowServiceClient client = new(config["WexflowWebServiceUri"]);
var username = config["Username"];
var password = config["Password"];

var workflows = await client.Search(string.Empty, username, password);
if (workflows.All(w => w.Id != o.WorkflowId))
{
Console.WriteLine("Workflow id {0} is incorrect.", o.WorkflowId);
return;
}

WorkflowInfo workflow;
switch (o.Operation)
{
case Operation.Start:
var instanceId = await client.StartWorkflow(o.WorkflowId, username, password);
Console.WriteLine("JobId: {0}", instanceId);

if (o.Wait)
{
Thread.Sleep(1000);
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
var isRunning = workflow.IsRunning;
while (isRunning)
{
Thread.Sleep(100);
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
isRunning = workflow.IsRunning;
}
}

break;

case Operation.Suspend:
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
if (!workflow.IsRunning)
{
Console.WriteLine("Workflow {0} is not running to be suspended.", o.WorkflowId);
return;
}

await client.SuspendWorkflow(o.WorkflowId, Guid.Parse(o.JobId), username, password);
break;

case Operation.Stop:
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
if (!workflow.IsRunning)
{
Console.WriteLine("Workflow {0} is not running to be stopped.", o.WorkflowId);
return;
}

await client.StopWorkflow(o.WorkflowId, Guid.Parse(o.JobId), username, password);
break;

case Operation.Resume:
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
if (!workflow.IsPaused)
{
Console.WriteLine("Workflow {0} is not suspended to be resumed.", o.WorkflowId);
return;
}

await client.ResumeWorkflow(o.WorkflowId, Guid.Parse(o.JobId), username, password);
break;

case Operation.Approve:
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
if (!workflow.IsWaitingForApproval)
{
Console.WriteLine("Workflow {0} is not waiting for approval to be approved.", o.WorkflowId);
return;
}

await client.ApproveWorkflow(o.WorkflowId, Guid.Parse(o.JobId), username, password);
break;

case Operation.Reject:
workflow = await client.GetWorkflow(username, password, o.WorkflowId);
if (!workflow.IsWaitingForApproval)
{
Console.WriteLine("Workflow {0} is not waiting for approval to be rejected.", o.WorkflowId);
return;
}

await client.RejectWorkflow(o.WorkflowId, Guid.Parse(o.JobId), username, password);
break;
}
}

var res = parser.ParseArguments<Options>(args).WithParsed(Action);

_ = res.WithNotParsed(_ =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/netcore/Wexflow.Core.Db.PostgreSQL/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void CreateDatabaseIfNotExists(string server, string userId, strin

using NpgsqlCommand command1 = new("SELECT COUNT(*) FROM pg_database WHERE datname = '" + databaseName + "'", conn);

var count = (long)command1.ExecuteScalar();
var count = (long)command1.ExecuteScalar()!;

if (count == 0)
{
Expand Down
Loading

0 comments on commit f0876a7

Please sign in to comment.