[1.2.5] Better linux handling

This commit is contained in:
BlubbFish 2022-01-20 19:20:04 +01:00
parent 628db8db10
commit 1862aa7da2
8 changed files with 205 additions and 276 deletions

View File

@ -4,68 +4,39 @@ using System.Threading;
namespace BlubbFish.Utils.IoT.Bots { namespace BlubbFish.Utils.IoT.Bots {
public abstract class ABot { public abstract class ABot {
#if !NETCOREAPP
private Thread sig_thread;
#endif
private Boolean RunningProcess = true; private Boolean RunningProcess = true;
protected ProgramLogger logger = new ProgramLogger(); protected ProgramLogger logger = new ProgramLogger();
private void SetupShutdown(Object sender, ConsoleCancelEventArgs e) { private void ConsoleCancelEvent(Object sender, ConsoleCancelEventArgs e) {
e.Cancel = true; e.Cancel = true;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.SetupShutdown: Signalhandler Windows INT recieved."); Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ConsoleCancelEvent()");
this.RunningProcess = false; this.RunningProcess = false;
} }
#if NETCOREAPP #if NETCOREAPP
private void Default_Unloading(AssemblyLoadContext obj) { private void Unloading(AssemblyLoadContext obj) => this.RunningProcess = false;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.SetupShutdown: Signalhandler Windows NETCORE recieved.");
this.RunningProcess = false; private void ProcessExit(Object sender, EventArgs e) => this.RunningProcess = false;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Shutdown.");
this.Dispose();
}
#endif #endif
protected void WaitForShutdown() { protected void WaitForShutdown() {
if(Type.GetType("Mono.Runtime") != null) {
#if !NETCOREAPP
this.sig_thread = new Thread(delegate () {
Mono.Unix.UnixSignal[] signals = new Mono.Unix.UnixSignal[] {
new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGTERM),
new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGINT)
};
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono attached.");
while(true) {
Int32 i = Mono.Unix.UnixSignal.WaitAny(signals, -1);
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Mono INT recieved " + i + ".");
this.RunningProcess = false;
break;
}
});
this.sig_thread.Start();
#endif
} else {
#if NETCOREAPP #if NETCOREAPP
AssemblyLoadContext.Default.Unloading += this.Default_Unloading; AssemblyLoadContext.Default.Unloading += this.Unloading;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Netcore attached."); AppDomain.CurrentDomain.ProcessExit += this.ProcessExit;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Attach Unloading and ProcessExit.");
#endif #endif
Console.CancelKeyPress += new ConsoleCancelEventHandler(this.SetupShutdown); Console.CancelKeyPress += new ConsoleCancelEventHandler(this.ConsoleCancelEvent);
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Windows attached."); Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Attach ConsoleCancelEvent.");
}
while(this.RunningProcess) { while(this.RunningProcess) {
Thread.Sleep(100); Thread.Sleep(100);
} }
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Shutdown."); Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Shutdown.");
} }
public virtual void Dispose() { public virtual void Dispose() {
#if !NETCOREAPP Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.Dispose: Shutdown.");
if(this.sig_thread != null && this.sig_thread.IsAlive) { this.RunningProcess = false;
this.sig_thread.Abort();
}
#endif
} }
} }
} }

View File

@ -5,17 +5,18 @@
<RootNamespace>BlubbFish.Utils.IoT.Bots</RootNamespace> <RootNamespace>BlubbFish.Utils.IoT.Bots</RootNamespace>
<AssemblyName>Bot-Utils</AssemblyName> <AssemblyName>Bot-Utils</AssemblyName>
<PackageId>Bots.IoT.Utils.BlubbFish</PackageId> <PackageId>Bots.IoT.Utils.BlubbFish</PackageId>
<Version>1.2.4</Version> <Version>1.2.5</Version>
<NeutralLanguage>de-DE</NeutralLanguage> <NeutralLanguage>de-DE</NeutralLanguage>
<Description>Bot-Utils are helpers for programming a bot</Description> <Description>Bot-Utils are helpers for programming a bot</Description>
<Authors>BlubbFish</Authors> <Authors>BlubbFish</Authors>
<Company>BlubbFish</Company> <Company>BlubbFish</Company>
<Copyright>Copyright © BlubbFish 2018 - 18.01.2022</Copyright> <Copyright>Copyright © BlubbFish 2018 - 20.01.2022</Copyright>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>http://git.blubbfish.net/vs_utils/Bot-Utils</PackageProjectUrl> <PackageProjectUrl>http://git.blubbfish.net/vs_utils/Bot-Utils</PackageProjectUrl>
<RepositoryUrl>http://git.blubbfish.net/vs_utils/Bot-Utils.git</RepositoryUrl> <RepositoryUrl>http://git.blubbfish.net/vs_utils/Bot-Utils.git</RepositoryUrl>
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
<PackageReleaseNotes> <PackageReleaseNotes>
1.2.5 - 2022-01-20 - Better linux handling
1.2.4 - 2022-01-18 - Config enabled module loading 1.2.4 - 2022-01-18 - Config enabled module loading
1.2.3 - 2022-01-09 - Tiny Refactoring 1.2.3 - 2022-01-09 - Tiny Refactoring
1.2.2 - 2021-08-22 - Going to netcore 1.2.2 - 2021-08-22 - Going to netcore

View File

@ -11,6 +11,7 @@ namespace BlubbFish.Utils.IoT.Bots {
protected void ModulDispose() { protected void ModulDispose() {
foreach (KeyValuePair<String, AModul<T>> item in this.moduls) { foreach (KeyValuePair<String, AModul<T>> item in this.moduls) {
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulDispose: Entlade Modul: " + item.Key);
item.Value.Dispose(); item.Value.Dispose();
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulDispose: Modul entladen: " + item.Key); Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.ModulDispose: Modul entladen: " + item.Key);
} }

View File

@ -3,12 +3,14 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using BlubbFish.Utils.IoT.Bots.Interfaces; using BlubbFish.Utils.IoT.Bots.Interfaces;
namespace BlubbFish.Utils.IoT.Bots.Moduls { namespace BlubbFish.Utils.IoT.Bots.Moduls {
public abstract class CronJob<T> : AModul<T>, IDisposable, IForceLoad { public abstract class CronJob<T> : AModul<T>, IForceLoad {
protected readonly List<Tuple<String, Action<Object>, Object>> internalCron = new List<Tuple<String, Action<Object>, Object>>(); protected readonly List<Tuple<String, Action<Object>, Object>> internalCron = new List<Tuple<String, Action<Object>, Object>>();
protected Thread thread; protected Thread thread;
protected Boolean threadRunning = false;
protected DateTime crontime; protected DateTime crontime;
protected readonly Dictionary<String, String> cron_named = new Dictionary<String, String> { protected readonly Dictionary<String, String> cron_named = new Dictionary<String, String> {
@ -24,14 +26,18 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
public CronJob(T lib, InIReader settings) : base(lib, settings) { public CronJob(T lib, InIReader settings) : base(lib, settings) {
this.crontime = DateTime.Now; this.crontime = DateTime.Now;
this.thread = new Thread(this.Runner); this.thread = new Thread(this.Runner);
this.threadRunning = true;
this.thread.Start(); this.thread.Start();
} }
#endregion #endregion
#region Cronjobrunner #region Cronjobrunner
protected void Runner() { protected void Runner() {
Thread.Sleep(DateTime.Now.AddMinutes(1).AddSeconds(DateTime.Now.Second * -1).AddMilliseconds(DateTime.Now.Millisecond * -1) - DateTime.Now); DateTime nextminute = DateTime.Now.AddMinutes(1).AddSeconds(DateTime.Now.Second * -1).AddMilliseconds(DateTime.Now.Millisecond * -1);
while (true) { while(nextminute > DateTime.Now && this.threadRunning) {
Thread.Sleep(100);
}
while (this.threadRunning) {
if (this.crontime.Minute != DateTime.Now.Minute) { if (this.crontime.Minute != DateTime.Now.Minute) {
this.crontime = DateTime.Now; this.crontime = DateTime.Now;
if (this.config.Count != 0) { if (this.config.Count != 0) {
@ -143,27 +149,13 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
public override void SetInterconnection(String cron, Action<Object> hook, Object data) => this.internalCron.Add(new Tuple<String, Action<Object>, Object>(cron, hook, data)); public override void SetInterconnection(String cron, Action<Object> hook, Object data) => this.internalCron.Add(new Tuple<String, Action<Object>, Object>(cron, hook, data));
protected override void UpdateConfig() { } protected override void UpdateConfig() { }
#endregion
#region IDisposable Support
private Boolean disposedValue = false;
protected virtual void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
if (this.thread != null) {
this.thread.Abort();
while (this.thread.ThreadState == ThreadState.Running) { Thread.Sleep(100); }
}
}
this.thread = null;
this.disposedValue = true;
}
}
public override void Dispose() { public override void Dispose() {
this.Dispose(true); this.threadRunning = false;
GC.SuppressFinalize(this); while(this.thread != null && this.thread.IsAlive) {
Thread.Sleep(10);
}
this.thread = null;
} }
#endregion #endregion
} }

View File

@ -7,7 +7,7 @@ using BlubbFish.Utils.IoT.Events;
using LitJson; using LitJson;
namespace BlubbFish.Utils.IoT.Bots.Moduls { namespace BlubbFish.Utils.IoT.Bots.Moduls {
public abstract class Mqtt<T> : AModul<T>, IDisposable { public abstract class Mqtt<T> : AModul<T> {
protected ABackend mqtt; protected ABackend mqtt;
protected Dictionary<String, AModul<T>> modules; protected Dictionary<String, AModul<T>> modules;
@ -34,6 +34,8 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
public override void Interconnect(Dictionary<String, AModul<T>> moduls) => this.modules = moduls; public override void Interconnect(Dictionary<String, AModul<T>> moduls) => this.modules = moduls;
protected override void UpdateConfig() => this.Reconnect(); protected override void UpdateConfig() => this.Reconnect();
public override void Dispose() => this.Disconnect();
#endregion #endregion
protected Tuple<Boolean, MqttEvent> ChangeConfig(BackendEvent e, String topic) { protected Tuple<Boolean, MqttEvent> ChangeConfig(BackendEvent e, String topic) {
@ -74,23 +76,5 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
} }
return new Tuple<Boolean, MqttEvent>(false, null); return new Tuple<Boolean, MqttEvent>(false, null);
} }
#region IDisposable Support
private Boolean disposedValue = false;
protected void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
this.Disconnect();
}
this.disposedValue = true;
}
}
public override void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
} }
} }

View File

@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace BlubbFish.Utils.IoT.Bots.Moduls { namespace BlubbFish.Utils.IoT.Bots.Moduls {
public abstract class Overtaker<T> : AModul<T>, IDisposable { public abstract class Overtaker<T> : AModul<T> {
protected readonly Dictionary<String, Dictionary<String, String>> events = new Dictionary<String, Dictionary<String, String>>(); protected readonly Dictionary<String, Dictionary<String, String>> events = new Dictionary<String, Dictionary<String, String>>();
#region Constructor #region Constructor
@ -62,22 +62,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
#region AModul #region AModul
public override void Interconnect(Dictionary<String, AModul<T>> moduls) { } public override void Interconnect(Dictionary<String, AModul<T>> moduls) { }
protected override void UpdateConfig() => this.ParseIni(); protected override void UpdateConfig() => this.ParseIni();
#endregion
#region IDisposable Support
private Boolean disposedValue = false;
protected virtual void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
}
this.disposedValue = true;
}
}
public override void Dispose() { public override void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
} }
#endregion #endregion
} }

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using BlubbFish.Utils.IoT.Bots.Interfaces; using BlubbFish.Utils.IoT.Bots.Interfaces;
namespace BlubbFish.Utils.IoT.Bots.Moduls { namespace BlubbFish.Utils.IoT.Bots.Moduls {
public abstract class Statuspolling<T> : AModul<T>, IDisposable, IForceLoad { public abstract class Statuspolling<T> : AModul<T>, IForceLoad {
#region Constructor #region Constructor
public Statuspolling(T lib, InIReader settings) : base(lib, settings) { } public Statuspolling(T lib, InIReader settings) : base(lib, settings) { }
@ -31,22 +31,7 @@ namespace BlubbFish.Utils.IoT.Bots.Moduls {
} }
} }
protected override void UpdateConfig() { } protected override void UpdateConfig() { }
#endregion
#region IDisposable Support
private Boolean disposedValue = false;
protected virtual void Dispose(Boolean disposing) {
if (!this.disposedValue) {
if (disposing) {
}
this.disposedValue = true;
}
}
public override void Dispose() { public override void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
} }
#endregion #endregion
} }

View File

@ -1,5 +1,15 @@
# Changelog # Changelog
## 1.2.5 - 2022-01-20 - Better linux handling
### New Features
* Add ProcessExit Handler in ABot
* Add Output in ModulDispose
### Bugfixes
* Eleminate Hangs in Cronjob, when in startup phase it not blocks shutdown
### Changes
* Reweite ABot, remove Mono Code
* Codingstyle
## 1.2.4 - 2022-01-18 - Config enabled module loading ## 1.2.4 - 2022-01-18 - Config enabled module loading
### New Features ### New Features
* Modules can have an enabled=true|false in config, so that also enables or disables moduleloading. * Modules can have an enabled=true|false in config, so that also enables or disables moduleloading.