diff --git a/lib/core/thread/New-IcingaThreadHash.psm1 b/lib/core/thread/New-IcingaThreadHash.psm1 new file mode 100644 index 0000000..56e765d --- /dev/null +++ b/lib/core/thread/New-IcingaThreadHash.psm1 @@ -0,0 +1,14 @@ +function New-IcingaThreadHash() +{ + param( + [ScriptBlock]$ShellScript, + [array]$Arguments + ); + + [string]$ScriptString = ''; + [string]$ArgString = ($Arguments | Out-String); + if ($null -ne $ShellScript) { + $ScriptString = $ShellScript.ToString(); + } + return (Get-StringSha1 -Content ($ScriptString + $ArgString + (Get-Date -Format "MM-dd-yyyy-HH-mm-ffff"))); +} diff --git a/lib/core/thread/New-IcingaThreadInstance.psm1 b/lib/core/thread/New-IcingaThreadInstance.psm1 new file mode 100644 index 0000000..6a2fa25 --- /dev/null +++ b/lib/core/thread/New-IcingaThreadInstance.psm1 @@ -0,0 +1,40 @@ +function New-IcingaThreadInstance() +{ + param( + [string]$Name, + $ThreadPool, + [ScriptBlock]$ScriptBlock, + [array]$Arguments, + [Switch]$Start + ); + + if ([string]::IsNullOrEmpty($Name)) { + $Name = New-IcingaThreadHash -ShellScript $ScriptBlock -Arguments $Arguments; + } + + $Shell = [PowerShell]::Create(); + $Shell.RunspacePool = $ThreadPool; + [void]$Shell.AddScript($ScriptBlock); + foreach ($argument in $Arguments) { + [void]$Shell.AddArgument($argument); + } + + $Thread = New-Object PSObject; + Add-Member -InputObject $Thread -MemberType NoteProperty -Name Shell -Value $Shell; + if ($Start) { + Add-Member -InputObject $Thread -MemberType NoteProperty -Name Handle -Value ($Shell.BeginInvoke()); + Add-Member -InputObject $Thread -MemberType NoteProperty -Name Started -Value $TRUE; + } else { + Add-Member -InputObject $Thread -MemberType NoteProperty -Name Handle -Value $null; + Add-Member -InputObject $Thread -MemberType NoteProperty -Name Started -Value $FALSE; + } + + if ($global:IcingaDaemonData.IcingaThreads.ContainsKey($Name) -eq $FALSE) { + $global:IcingaDaemonData.IcingaThreads.Add($Name, $Thread); + } else { + $global:IcingaDaemonData.IcingaThreads.Add( + (New-IcingaThreadHash -ShellScript $ScriptBlock -Arguments $Arguments), + $Thread + ); + } +} diff --git a/lib/core/thread/New-IcingaThreadPool.psm1 b/lib/core/thread/New-IcingaThreadPool.psm1 new file mode 100644 index 0000000..f0a8a2b --- /dev/null +++ b/lib/core/thread/New-IcingaThreadPool.psm1 @@ -0,0 +1,18 @@ +function New-IcingaThreadPool() +{ + param( + [int]$MinInstances = 1, + [int]$MaxInstances = 5 + ); + + $Runspaces = [RunspaceFactory]::CreateRunspacePool( + $MinInstances, + $MaxInstances, + [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault(), + $host + ) + + $Runspaces.Open(); + + return $Runspaces; +} diff --git a/lib/core/thread/Remove-IcingaThread.psm1 b/lib/core/thread/Remove-IcingaThread.psm1 new file mode 100644 index 0000000..60f48c5 --- /dev/null +++ b/lib/core/thread/Remove-IcingaThread.psm1 @@ -0,0 +1,17 @@ +function Remove-IcingaThread() +{ + param( + [string]$Thread + ); + + if ([string]::IsNullOrEmpty($Thread)) { + return; + } + + Stop-IcingaThread -Thread $Thread; + + if ($global:IcingaDaemonData.IcingaThreads.ContainsKey($Thread)) { + $global:IcingaDaemonData.IcingaThreads[$Thread].Shell.Dispose(); + $global:IcingaDaemonData.IcingaThreads.Remove($Thread); + } +} diff --git a/lib/core/thread/Restart-IcingaThread.psm1 b/lib/core/thread/Restart-IcingaThread.psm1 new file mode 100644 index 0000000..885de77 --- /dev/null +++ b/lib/core/thread/Restart-IcingaThread.psm1 @@ -0,0 +1,13 @@ +function Restart-IcingaThread() +{ + param( + [string]$Thread + ); + + if ([string]::IsNullOrEmpty($Thread)) { + return; + } + + Stop-IcingaThread $Thread; + Start-IcingaThread $Thread; +} diff --git a/lib/core/thread/Start-IcingaThread.psm1 b/lib/core/thread/Start-IcingaThread.psm1 new file mode 100644 index 0000000..09eb6ba --- /dev/null +++ b/lib/core/thread/Start-IcingaThread.psm1 @@ -0,0 +1,17 @@ +function Start-IcingaThread() +{ + param( + [string]$Thread + ); + + if ([string]::IsNullOrEmpty($Thread)) { + return; + } + + if ($global:IcingaDaemonData.IcingaThreads.ContainsKey($Thread)) { + if ($global:IcingaDaemonData.IcingaThreads[$Thread].Started -eq $FALSE) { + $global:IcingaDaemonData.IcingaThreads[$Thread].Handle = $global:IcingaDaemonData.IcingaThreads[$Thread].Shell.BeginInvoke(); + $global:IcingaDaemonData.IcingaThreads[$Thread].Started = $TRUE; + } + } +} diff --git a/lib/core/thread/Stop-IcingaThread.psm1 b/lib/core/thread/Stop-IcingaThread.psm1 new file mode 100644 index 0000000..e78f4a0 --- /dev/null +++ b/lib/core/thread/Stop-IcingaThread.psm1 @@ -0,0 +1,18 @@ +function Stop-IcingaThread() +{ + param( + [string]$Thread + ); + + if ([string]::IsNullOrEmpty($Thread)) { + return; + } + + if ($global:IcingaDaemonData.IcingaThreads.ContainsKey($Thread)) { + if ($global:IcingaDaemonData.IcingaThreads[$Thread].Started -eq $TRUE) { + $global:IcingaDaemonData.IcingaThreads[$Thread].Shell.Stop(); + $global:IcingaDaemonData.IcingaThreads[$Thread].Handle = $null; + $global:IcingaDaemonData.IcingaThreads[$Thread].Started = $FALSE; + } + } +} diff --git a/lib/core/thread/Test-IcingaThread.psm1 b/lib/core/thread/Test-IcingaThread.psm1 new file mode 100644 index 0000000..1ff7d23 --- /dev/null +++ b/lib/core/thread/Test-IcingaThread.psm1 @@ -0,0 +1,12 @@ +function Test-IcingaThread() +{ + param( + [string]$Thread + ); + + if ([string]::IsNullOrEmpty($Thread)) { + return $FALSE; + } + + return $global:IcingaDaemonData.IcingaThreads.ContainsKey($Thread); +}