mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
Added support and management of threads
This commit is contained in:
parent
7150fd1236
commit
16f5c5fe13
8 changed files with 149 additions and 0 deletions
14
lib/core/thread/New-IcingaThreadHash.psm1
Normal file
14
lib/core/thread/New-IcingaThreadHash.psm1
Normal file
|
|
@ -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")));
|
||||||
|
}
|
||||||
40
lib/core/thread/New-IcingaThreadInstance.psm1
Normal file
40
lib/core/thread/New-IcingaThreadInstance.psm1
Normal file
|
|
@ -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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
18
lib/core/thread/New-IcingaThreadPool.psm1
Normal file
18
lib/core/thread/New-IcingaThreadPool.psm1
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
17
lib/core/thread/Remove-IcingaThread.psm1
Normal file
17
lib/core/thread/Remove-IcingaThread.psm1
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
lib/core/thread/Restart-IcingaThread.psm1
Normal file
13
lib/core/thread/Restart-IcingaThread.psm1
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
function Restart-IcingaThread()
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
[string]$Thread
|
||||||
|
);
|
||||||
|
|
||||||
|
if ([string]::IsNullOrEmpty($Thread)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Stop-IcingaThread $Thread;
|
||||||
|
Start-IcingaThread $Thread;
|
||||||
|
}
|
||||||
17
lib/core/thread/Start-IcingaThread.psm1
Normal file
17
lib/core/thread/Start-IcingaThread.psm1
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
lib/core/thread/Stop-IcingaThread.psm1
Normal file
18
lib/core/thread/Stop-IcingaThread.psm1
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
lib/core/thread/Test-IcingaThread.psm1
Normal file
12
lib/core/thread/Test-IcingaThread.psm1
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
function Test-IcingaThread()
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
[string]$Thread
|
||||||
|
);
|
||||||
|
|
||||||
|
if ([string]::IsNullOrEmpty($Thread)) {
|
||||||
|
return $FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $global:IcingaDaemonData.IcingaThreads.ContainsKey($Thread);
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue