Last active
January 4, 2019 20:12
-
-
Save itfranck/6341d276f94804d463794d178c538231 to your computer and use it in GitHub Desktop.
Modified Start-UDHotReloader from Adam Driscoll that includes an app pool restart when a file in the Endpoints subdirectory is modified so the changes are applied nearly instantly. This was intended to works with IIS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Remove Import if not found and not using IIS | |
Import-Module WebAdministration | |
Add-Type -TypeDefinition @" | |
[System.Flags] | |
public enum DashboardAction | |
{ | |
Undefined = 0, | |
Update = 1, | |
Restart = 2 | |
} | |
"@ | |
function Start-UDHotReloader { | |
param( | |
[Parameter(Mandatory = $true)] | |
$Root, | |
[Parameter(Mandatory)] | |
$UpdateToken, | |
[Parameter(Mandatory)] | |
$Url, | |
$AppPool, | |
[int]$UpdateDelay = 750, | |
[String]$EndpointsPath = '*\Endpoints\*' | |
) | |
Process { | |
$fileInfo = [System.IO.FileInfo]::new($Root) | |
$fileSystemWatcher = [System.IO.FileSystemWatcher]::new($fileInfo.DirectoryName, "*.ps1") | |
$fileSystemWatcher.NotifyFilter = [IO.NotifyFilters]::LastWrite | |
$fileSystemWatcher.EnableRaisingEvents = $true | |
$fileSystemWatcher.IncludeSubdirectories = $true | |
$Global:DashboardAction = [DashboardAction]::Undefined | |
$Global:DashboardActionDelay = New-Object -TypeName 'System.Timers.Timer' | |
$Global:DashboardActionDelay.AutoReset = $true | |
$Global:DashboardActionDelay.Interval = $UpdateDelay | |
Register-ObjectEvent $Global:DashboardActionDelay elapsed -SourceIdentifier 'ActionDelay' -Action { | |
try { | |
$Operation = "" | |
$Token = $event.MessageData.UpdateToken | |
$Url = $event.MessageData.Url | |
switch ($Global:DashboardAction) { | |
{$_ -band [DashboardAction]::Restart } { | |
$Operation = 'Apppool recycled' | |
Restart-WebAppPool -Name $event.MessageData.AppPool | |
break | |
} | |
{$_ -band [DashboardAction]::Update} { | |
$Operation = 'Dashboard updated' | |
Update-UDDashboard -Url $Url -UpdateToken $Token -FilePath $event.MessageData.Root | |
} | |
} | |
if ($Global:DashboardAction -ne [DashboardAction]::Undefined) { | |
Write-Host "$Operation - $Url " -ForegroundColor Cyan | |
} | |
} | |
catch { | |
Write-Host $_.Exception -ForegroundColor Red | |
} | |
Finally { | |
$Global:DashboardAction = [DashboardAction]::Undefined | |
$Global:DashboardActionDelay.Stop() | |
} | |
} -MessageData @{ | |
Url = $Url | |
UpdateToken = $UpdateToken | |
Root = $Root | |
AppPool = $AppPool | |
} | |
Register-ObjectEvent $fileSystemWatcher Changed -SourceIdentifier FileChanged -Action { | |
try { | |
$Global:DashboardActionDelay.Stop() | |
$Global:DashboardActionDelay.Start() | |
$EndpointsPath = $event.MessageData.EndpointsPath | |
$AppPool = $event.MessageData.AppPool | |
$PerformRestart = $event.SourceEventArgs.FullPath -like $EndpointsPath -and -not [String]::IsNullOrWhiteSpace($event.MessageData.AppPool) | |
if ($PerformRestart) { | |
$Global:DashboardAction = $Global:DashboardAction -bor [DashboardAction]::Restart | |
} else { | |
$Global:DashboardAction = $Global:DashboardAction -bor [DashboardAction]::Update | |
} | |
} | |
catch { | |
Write-Host $_.Exception -ForegroundColor Red | |
} | |
} -MessageData @{ | |
EndpointsPath = $EndpointsPath | |
AppPool = $AppPool | |
} | |
} | |
} | |
$UDHotParams = @{ | |
Root = 'C:\Users\m\PoshUD-IIS\rest\src\root.ps1' | |
UpdateToken = 'test' | |
Url = 'https://Contoso.com:4444/' | |
AppPool = 'PoshUD-IIS' # If omitted (because not using IIS), it will simply update dashboard | |
UpdateDelay = 500 # Default is 750 ms | |
EndpointsPath = '*\Endpoints\*' | |
} | |
Start-UDHotReloader @UDHotParams | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment