-
Notifications
You must be signed in to change notification settings - Fork 50
/
Start-MicroServices.ps1
172 lines (134 loc) · 4.45 KB
/
Start-MicroServices.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#requires -version 3
[CmdletBinding()]
param ([bool]$monitorServices = $true)
[console]::TreatControlCAsInput = $true
function Invoke-MicroService {
param (
$SourcePath,
$DeployPath,
$ProcessFilePath
)
Copy-Item -Path $SourcePath -Destination $DeployPath -Recurse -ErrorAction Stop
$Process = Start-Process -FilePath $ProcessFilePath -PassThru -ErrorAction Stop
return $Process
}
function Stop-MicroService {
param (
$Process
)
if (-not $Process) { return }
$Process.Refresh()
if (-not $Process.HasExited) {
$Process.CloseMainWindow() | Out-Null # todo consider PostMessage to window with Ctrl+C
Start-Sleep -Seconds 1
$Process.Refresh()
}
if (-not $Process.HasExited) {
$Process.Kill() | Out-Null
Start-Sleep -Seconds 2
$Process.Refresh()
}
if (-not $Process.HasExited) {
throw "could not stop process"
}
}
function Find-MicroService {
param (
$ProcessFilePath
)
$ProcessBaseName = [System.IO.Path]::GetFileNameWithoutExtension($ProcessFilePath)
$Process = Get-Process -Name $ProcessBaseName -ErrorAction SilentlyContinue |
Where-Object { $_.Path -eq $ProcessFilePath }
if (@($Process).Length -gt 1) { throw "More than one instance of the same microservice running" }
return $Process
}
function Test-MicroService {
param (
$Process
)
if ($Process) {
$Process.Refresh()
return (-not $Process.HasExited)
}
return $false
}
function Get-LastWriteTime {
param (
[Parameter(Mandatory)]
$Path
)
Get-ChildItem -Path $Path -Recurse |
Measure-Object -Property LastWriteTime -Maximum |
Select-Object -ExpandProperty Maximum
}
function New-MicroServiceObject {
param (
$ProjectName,
$RootPath
)
$ServiceHostProcessFileName = $ProjectName + '.exe'
$o = [pscustomobject]@{
Name = $ProjectName
SourcePath = Join-Path -Path $PSScriptRoot -ChildPath $RootPath\$ProjectName\bin\Debug
ProcessFileName = $ServiceHostProcessFileName
Process = [System.Diagnostics.Process]$null
DeployedLastWriteTime = [DateTime]::MinValue
DeployPath = ''
ProcessFilePath = ''
}
$o.DeployPath = Join-Path -Path (Split-Path -Path $o.SourcePath) -ChildPath Deployed
$o.ProcessFilePath = Join-Path -Path $o.DeployPath -ChildPath $o.ProcessFileName
$o.Process = Find-MicroService -ProcessFilePath $o.ProcessFilePath
return $o
}
function Poll {
param (
$MicroServiceData
)
$ShouldDeploy = $false
$LastWriteTime = Get-LastWriteTime -Path $MicroServiceData.SourcePath
if (-not (Test-MicroService -Process $MicroServiceData.Process)) {
$ShouldDeploy = $true
} else {
if ($LastWriteTime -gt $MicroServiceData.DeployedLastWriteTime) {
$ShouldDeploy = $true
}
}
if (-not $ShouldDeploy) {
return
}
"$(Get-Date): Redeploying '$($MicroServiceData.Name)'"
Stop-MicroService -Process $MicroServiceData.Process
if (Test-Path -Path $MicroServiceData.DeployPath) {
Remove-Item -Path $MicroServiceData.DeployPath -Recurse
if (-not $?) {
return
}
}
$MicroServiceData.Process = Invoke-MicroService -SourcePath $MicroServiceData.SourcePath -DeployPath $MicroServiceData.DeployPath -ProcessFilePath $MicroServiceData.ProcessFilePath
$MicroServiceData.DeployedLastWriteTime = $LastWriteTime
}
$MicroServices = @(
New-MicroServiceObject -RootPath Admin -ProjectName Admin.Service
New-MicroServiceObject -RootPath Admin -ProjectName Admin.ReadModels.Service
New-MicroServiceObject -RootPath Cashier -ProjectName Cashier.ReadModels.Service
New-MicroServiceObject -RootPath Cashier -ProjectName Cashier.Service
New-MicroServiceObject -RootPath Barista -ProjectName Barista.Service
)
while ($true) {
if ($Host.UI.RawUI.KeyAvailable -and (3 -eq [int]$Host.UI.RawUI.ReadKey("AllowCtrlC,IncludeKeyUp,NoEcho").Character)) {
"$(Get-Date): Shutdown requested"
break
}
foreach ($MicroService in $MicroServices) {
Poll -MicroServiceData $MicroService
}
if (-not $monitorServices) {
return
}
Start-Sleep -Seconds 1
}
foreach ($MicroService in $MicroServices) {
"$(Get-Date): Stopping '$($MicroService.Name)'"
Stop-MicroService -Process $MicroService.Process
}