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

Part 2 in the move to IOCing the world. #199

Merged
merged 5 commits into from
Sep 13, 2020
Merged

Part 2 in the move to IOCing the world. #199

merged 5 commits into from
Sep 13, 2020

Conversation

asherw
Copy link
Contributor

@asherw asherw commented Aug 27, 2020

Breaking change

Appears to load the 2 hello world apps...

Proposed change

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

The goal is to move away from Activator.CreateInstance and to pull these from IOC.

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the [development checklist][dev-checklist]
  • The code compiles without warnings (code quality check)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:


private static void RegisterNetDaemonAssembly(IServiceCollection services)
{
if (BypassLocalAssemblyLoading())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do I have this arse about?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure what you asking ?

Copy link
Contributor

@BeeHiveJava BeeHiveJava Aug 27, 2020

Choose a reason for hiding this comment

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

I'm wondering if it would be better to inject both compilers and pick the right one at runtime (depending on the ENV var) in a factory. But this is something that could be easily added in the future, it was just a thought I had.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ohh right, interesting idea @BeeHiveJava

private static void RegisterNetDaemonAssembly(IServiceCollection services)
{
if (BypassLocalAssemblyLoading())
services.AddSingleton<IDaemonAppCompiler, LocalDaemonAppCompiler>();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Split up local app assembly loading vs compiling .cs files from apps folder.

_netDaemonSettings = netDaemonSettings;
}

public IEnumerable<Type> GetApps()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I created this to help with migration. Allows me to change less code.
I would like to instantiate apps using DI.

A POC I created worked but I didn't get round to how the apps would react when a re-connection would occur, so see what happens.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I test this manually during development. And yea would be nice to create apps with DI but... still needs to be dynamic. I do not want the user to specify explicit what apps should be compiled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm angling for the app to register all the apps in the DI container. POC didn't require this, it just did reflection over app assembly and registered them in the DI container, but as I'm implementing changes things are turning out slightly different than I expected.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Got it

return (assembly.GetTypesWhereSubclassOf<NetDaemonAppBase>(), string.Empty);
}

public static Assembly GetCompiledAppAssembly(out CollectibleAssemblyLoadContext alc, string codeFolder, ILogger logger)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I expect all this to be eventually moved into NetDaemon.Service.App.DaemonAppCompiler.

Future feature, recompile app folder and reload without having to restart docker container?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This once worked but was moved to be compiled by container cause I wanted to support custom csproj file. We need to be able to support this in that case. Would be great if we manage this.

{
loadedDaemonApps = null;
var alcWeakRef = new WeakReference(alc, trackResurrection: true);
alc.Unload();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

alc was already being unloaded right after apps were being loaded in GetCompiledAppAssembly(out CollectibleAssemblyLoadContext alc, string codeFolder, ILogger logger)
This caused an exception on app shutdown.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ohh good find


private static bool BypassLocalAssemblyLoading()
{
var value = Environment.GetEnvironmentVariable("HASS_DISABLE_LOCAL_ASM");
Copy link
Collaborator

Choose a reason for hiding this comment

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

So this should always be added to the docker file then? When using default container/add-on? Maybe this should be added to normal config?

Copy link
Contributor

@BeeHiveJava BeeHiveJava left a comment

Choose a reason for hiding this comment

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

Great changes!

@helto4real
Copy link
Collaborator

Great changes!

Agree nice

@helto4real
Copy link
Collaborator

@asherw is your plan to update the PR? Or do you want to merge it as is?

@helto4real helto4real merged commit 2d69e35 into dev Sep 13, 2020
@helto4real helto4real deleted the feature/IocPartTwo branch September 13, 2020 13:11
Ikcelaks pushed a commit to Ikcelaks/netdaemon that referenced this pull request Dec 23, 2022
* Part 2 in the move to IOCing the world.

* Remove unused code and reduce if statement nesting.

* fix: ensure types are instantiable

* refactor: use bool parse as return value

Co-authored-by: Lesley Vente <BeeHiveJava@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants