Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update WPFCLI to add keep alive startup param handling #12156

Merged
merged 14 commits into from
Apr 11, 2022
Merged
1 change: 0 additions & 1 deletion src/DynamoCLI/CommandLineRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ private static XmlDocument RunCommandLineArgs(DynamoModel model, StartupUtils.Co
{
Console.WriteLine("geometryFilePath option is only available when running DynamoWPFCLI, not DynamoCLI");
}
model.HostAnalyticsInfo = cmdLineArgs.AnalyticsInfo;

cmdLineArgs.ImportedPaths.ToList().ForEach(path =>
{
Expand Down
4 changes: 2 additions & 2 deletions src/DynamoCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ static internal void Main(string[] args)
DynamoModel model;
if (!String.IsNullOrEmpty(cmdLineArgs.ASMPath))
{
model = Dynamo.Applications.StartupUtils.MakeModel(true, cmdLineArgs.ASMPath);
model = Dynamo.Applications.StartupUtils.MakeModel(true, cmdLineArgs.ASMPath, cmdLineArgs.AnalyticsInfo);
}
else
{
model = Dynamo.Applications.StartupUtils.MakeModel(true);
model = Dynamo.Applications.StartupUtils.MakeModel(true, string.Empty, cmdLineArgs.AnalyticsInfo);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QilongTang Hi Aaron. Is this the correct way to update the MakeModel usage from the obsoleted methods

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

}
var runner = new CommandLineRunner(model);
runner.Run(cmdLineArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,14 @@ internal WorkspaceViewModel CurrentSpaceViewModel
{
get
{
return viewModel.Workspaces.FirstOrDefault(vm => vm.Model == dynamoModel.CurrentWorkspace);
if (viewModel == null)
{
return null;
}
else
{
return viewModel.Workspaces.FirstOrDefault(vm => vm.Model == dynamoModel.CurrentWorkspace);
}
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/DynamoWPFCLI/CommandLineRunnerWPF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ private static XmlDocument RunCommandLineArgs(DynamoViewModel viewModel, Startup
Console.WriteLine("commandFilePath option is only available when running DynamoSandbox, not DynamoWPFCLI");
}

viewModel.Model.HostAnalyticsInfo= cmdLineArgs.AnalyticsInfo;

cmdLineArgs.ImportedPaths.ToList().ForEach(path =>
{
ImportAssembly(viewModel.Model, path);
});

viewModel.OpenCommand.Execute(new Tuple<string, bool>(cmdLineArgs.OpenFilePath, true));
Console.WriteLine("loaded file");
viewModel.Model.EvaluationCompleted += (o, args) => { evalComplete = true; };
Expand Down
1 change: 1 addition & 0 deletions src/DynamoWPFCLI/DynamoWPFCLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Reference Include="Microsoft.Practices.Prism">
<HintPath>..\..\extern\prism\Microsoft.Practices.Prism.dll</HintPath>
</Reference>
<Reference Include="WindowsBase" />
Copy link
Contributor

@QilongTang QilongTang Apr 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why we need to add this? Would this prevent running WPF CLI on linux based system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was necessary with the threading model required to load the ViewModel which makes sence since that also will not load on linux

<PackageReference Include="Newtonsoft.Json" Version="8.0.3" />
</ItemGroup>
<ItemGroup>
Expand Down
126 changes: 107 additions & 19 deletions src/DynamoWPFCLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
using System;
using System.Linq;
using System.Threading;
using Dynamo.Applications;
using Dynamo.Models;
using Dynamo.ViewModels;
using Dynamo.Wpf.ViewModels.Watch3D;
using static System.Windows.Threading.Dispatcher;

namespace DynamoWPFCLI
{
internal class Program
{
[STAThread]
static internal void Main(string[] args)
internal static void Main(string[] args)
{

try
{
var cmdLineArgs = StartupUtils.CommandLineArguments.Parse(args);
var locale = StartupUtils.SetLocale(cmdLineArgs);

if (cmdLineArgs.DisableAnalytics)
{
Dynamo.Logging.Analytics.DisableAnalytics = true;
}

DynamoModel model;
if (!String.IsNullOrEmpty(cmdLineArgs.ASMPath))
if (cmdLineArgs.KeepAlive)
{
model = Dynamo.Applications.StartupUtils.MakeModel(true, cmdLineArgs.ASMPath);
var thread = new Thread(() => RunKeepAlive(cmdLineArgs));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a name for this thread so that is easily visible when debugging like the Scheduler thread?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name added


thread.SetApartmentState(ApartmentState.STA);
thread.Start();

Console.WriteLine("Starting DynamoWPFCLI in keepalive mode");
Console.ReadLine();

ShutDown();
}
else
{
model = Dynamo.Applications.StartupUtils.MakeModel(true);
var viewModel = StartupDaynamo(cmdLineArgs);

var runner = new CommandLineRunnerWPF(viewModel);
runner.Run(cmdLineArgs);
}
var viewModel = DynamoViewModel.Start(
new DynamoViewModel.StartConfiguration()
{
DynamoModel = model,
Watch3DViewModel = new DefaultWatch3DViewModel(null, new Watch3DViewModelStartupParams(model))
{
Active = false,
CanBeActivated = false
}
});

var runner = new CommandLineRunnerWPF(viewModel);
runner.Run(cmdLineArgs);

}
catch (Exception e)
{
Expand All @@ -61,5 +61,93 @@ static internal void Main(string[] args)
}
}

/// <summary>
/// Start Dynamo Model and ViewModel per cmdLineArgs parameters.
/// </summary>
/// <param name="cmdLineArgs"></param>
/// <returns></returns>
private static DynamoViewModel StartupDaynamo(StartupUtils.CommandLineArguments cmdLineArgs)
{
DynamoModel model;
if (!String.IsNullOrEmpty(cmdLineArgs.ASMPath))
{
model = Dynamo.Applications.StartupUtils.MakeModel(true, cmdLineArgs.ASMPath, cmdLineArgs.AnalyticsInfo);
}
else
{
model = Dynamo.Applications.StartupUtils.MakeModel(true, string.Empty, cmdLineArgs.AnalyticsInfo);
}

model.ShutdownCompleted += (m) => { ShutDown(); };

var viewModel = DynamoViewModel.Start(
new DynamoViewModel.StartConfiguration()
{
DynamoModel = model,
Watch3DViewModel = new DefaultWatch3DViewModel(null, new Watch3DViewModelStartupParams(model))
{
Active = false,
CanBeActivated = false
}
});

cmdLineArgs.ImportedPaths.ToList().ForEach(path =>
{
ImportAssembly(model, path);
});

return viewModel;
}

private static void RunKeepAlive(StartupUtils.CommandLineArguments cmdLineArgs)
{
try
{
StartupDaynamo(cmdLineArgs);

Console.WriteLine("-----------------------------------------");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I realize that we probably want to show the console only in debug mode ? But we can deal with this later in other PR.

Console.WriteLine("DynamoWPFCLI is running in keepalive mode");
Console.WriteLine("Press Enter to shutdown...");

Run();
}
catch
{
Console.WriteLine("Server is shutting down due to an error");
}
}

/// <summary>
/// Attempts to import an assembly as a node library from a given file path.
/// </summary>
/// <param name="model"></param>
/// <param name="path"></param>
private static void ImportAssembly(DynamoModel model, string path)
{
try
{
var filePath = new System.IO.FileInfo(path);
if (!filePath.Exists)
{
Console.WriteLine($"could not find requested import library at path{path}");
}
else
{
Console.WriteLine($"attempting to import assembly {path}");
var assembly = System.Reflection.Assembly.LoadFile(path);
model.LoadNodeLibrary(assembly, true);
}
}
catch (Exception e)
{
Console.WriteLine($"exception while trying to load assembly {path}: {e}");
}
}

private static void ShutDown()
{
Environment.Exit(0);
}

}
}