-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild__10__wait_for_query.ps1
More file actions
183 lines (134 loc) · 4.93 KB
/
build__10__wait_for_query.ps1
File metadata and controls
183 lines (134 loc) · 4.93 KB
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
173
174
175
176
177
178
179
180
181
182
183
################################################
#
# SCRIPT ROOT
#
################################################
# script root path
if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript")
{ $scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition }
else
{ $scriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0]) }
Set-Location $scriptPath
################################################
#
# DEBUG
#
################################################
$debug = $false
if ( $debug ) {
$inputMssqlConnectionString = "Data Source=<mssqlserver>;Initial Catalog=<mssqldatabase>;Trusted_Connection=True;"
$inputDb2ConnectionString = "Server=<db2server>:<port>;Database=<db2database>;Uid=<db2user>;Pwd=<db2password>;"
$inputSuccessFile = "D:\Apteco\Build\Holidays\temp\build.debug"
} else {
$inputMssqlConnectionString = $Env:SOURCEDB
$inputDb2ConnectionString = $Env:DBCONNECTION
$inputSuccessFile = $Env:WAITFILE
}
################################################
#
# SETTINGS
#
################################################
# Load settings
$settings = Get-Content -Path "$( $scriptPath )\settings.json" -Encoding UTF8 -Raw | ConvertFrom-Json
$functionsSubfolder = "functions"
# set db connections
$mssqlConnectionString = $inputMssqlConnectionString
$db2ConnectionString = $inputDb2ConnectionString
# Assign variables
$successFile = $inputSuccessFile
$sleepTime = $settings.sleepTime
$maxWaitTimeTotal = $settings.maxSecondsWaiting
$startTime = Get-Date
################################################
#
# FUNCTIONS
#
################################################
Get-ChildItem -Path ".\$( $functionsSubfolder )" | ForEach {
. $_.FullName
}
################################################
#
# REMOVE SUCCESS FILE
#
################################################
If (Test-Path $successFile ) {
Remove-Item -Path $successFile -Force
}
################################################
#
# CHECK MSSQL FOR SUCCESS
#
################################################
$mssqlConnection = New-Object System.Data.SqlClient.SqlConnection
$mssqlConnection.ConnectionString = $mssqlConnectionString
$mssqlConnection.Open()
Do {
"Trying to load the data from MSSQL"
# define query -> currently the age of the date in the query has to be less than 12 hours
$mssqlQuery = "Select * from dbo.StatusTable where datediff(hh, [LastUpdateDate], GETDATE()) <= $( $settings.maxAgeForData ) "
# execute command
$mssqlCommand = $mssqlConnection.CreateCommand()
$mssqlCommand.CommandText = $mssqlQuery
$mssqlResult = $mssqlCommand.ExecuteReader()
# load data
$mssqlTable = new-object "System.Data.DataTable"
$mssqlTable.Load($mssqlResult)
# check for result, count the number of rows for success
$mssqlSuccess = $mssqlTable.rows.Count -gt 0
# pause
if ( $mssqlSuccess ) {
"MSSQL successful"
} else {
"MSSQL not successful"
Start-Sleep -Seconds $sleepTime
}
} until ( $mssqlSuccess -or (New-TimeSpan -Start $startTime).TotalSeconds -ge $maxWaitTimeTotal )
$mssqlConnection.Close()
################################################
#
# CHECK DB2 FOR SUCCESS
#
################################################
# Load IBM DB2 assembly
$db2AssemblyFile = "C:\Program Files\Apteco\FastStats Designer\IBM.Data.DB2.dll"
[Reflection.Assembly]::LoadFile($db2AssemblyFile)
# the enviroment variable fills from the designer user defined variables
$db2Connection = New-Object "IBM.Data.DB2.DB2Connection"
$db2Connection.ConnectionString = $db2ConnectionString
$db2Connection.Open()
Do {
"Trying to load the data from DB2"
# define query
$db2Query = "select * from schema.statustable where TIMESTAMP >= (CURRENT TIMESTAMP - $( $settings.maxAgeForData ) HOURS)"
# execute command
$db2Command = $db2Connection.CreateCommand()
$db2Command.CommandText = $db2Query
$db2Result = $db2Command.ExecuteReader()
# load data
$db2Table = new-object "System.Data.DataTable"
$db2Table.Load($db2Result)
# check for result, count the number of rows for success
$db2Success = $db2Table.rows.Count -gt 0
# pause
if ( $db2Success ) {
"DB2 successful"
} else {
"DB2 not successful"
$db2Table
Start-Sleep -Seconds $sleepTime
}
} until ( $db2Success -or (New-TimeSpan -Start $startTime).TotalSeconds -ge $maxWaitTimeTotal )
$db2Connection.Close()
################################################
#
# CREATE SUCCESS FILE
#
################################################
if ( ($mssqlSuccess -and $db2Success) -or ( (New-TimeSpan -Start $startTime).TotalSeconds -ge $maxWaitTimeTotal ) ) {
"MSSQL success: $( $mssqlSuccess )"
"DB2 success: $( $db2Success )"
"Timeout reached: $( ( (New-TimeSpan -Start $startTime).TotalSeconds -ge $maxWaitTimeTotal ) )"
[datetime]::Now.ToString("yyyyMMddHHmmss") | Out-File -FilePath $successFile -Encoding utf8 -Force
}