Private/Wait-GhostSite.ps1
|
function Wait-GhostSite { <# .SYNOPSIS Waits until the site answers and returns its public information, or nothing when the time is up. .DESCRIPTION A site that was just started answers after it has brought its database up to date, which takes a while the first time. With a timeout of 0 the site is asked once, to find out whether it runs. .EXAMPLE Wait-GhostSite -SiteUrl 'http://localhost:2368' Returns the title, the version and the other public information of the site once it answers. #> [CmdletBinding()] [OutputType([pscustomobject])] param( # Address of the site, such as http://localhost:2368. [Parameter(Mandatory)] [string] $SiteUrl, # How long to wait, in seconds. [int] $Timeout = 180 ) $deadline = [datetime]::UtcNow.AddSeconds($Timeout) do { try { $answer = Invoke-RestMethod -Uri "$SiteUrl/ghost/api/admin/site/" -TimeoutSec 5 -UseBasicParsing return $answer.site } catch { Write-Debug -Message "The site does not answer yet: $($_.Exception.Message)" } if ($Timeout -gt 0) { Start-Sleep -Seconds 2 } } while ([datetime]::UtcNow -lt $deadline) } |