-
Notifications
You must be signed in to change notification settings - Fork 1
/
VistATaskmanUtil.py
288 lines (263 loc) · 11.2 KB
/
VistATaskmanUtil.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#---------------------------------------------------------------------------
# Copyright 2013 The Open Source Electronic Health Record Agent
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#---------------------------------------------------------------------------
from __future__ import with_statement
import sys
import os
import argparse
import time
from VistAMenuUtil import VistAMenuUtil
from LoggerManager import logger, initConsoleLogging, getTempLogFile
from VistATestClient import VistATestClientFactory, createTestClientArgParser
from VistAPackageInfoFetcher import findChoiceNumber
def getBoxVolPair(vistAClient):
connection = vistAClient.getConnection()
vistAClient.waitForPrompt()
connection.send("D GETENV^%ZOSV W Y\r")
vistAClient.waitForPrompt()
retValue = connection.before.split('^')[-1].rstrip(' \r\n')
connection.send('\r')
return retValue
class VistATaskmanUtil(object):
""" Enum for taskman Status """
TASKMAN_STATUS_UNKNOWN = -1
TASKMAN_STATUS_RUNNING_CURRENT = 0
TASKMAN_STATUS_RUNNING_BEHIND = 1
TASKMAN_STATUS_WAIT = 2
TASKMAN_STATUS_SHUTDOWN = 3
TASKMAN_STATUS_ERROR_STATE = 4
TASKMAN_STATUS_LAST = 5
def __init__(self):
pass
def verifyTaskmanSiteParameter(self, vistAClient, autoFix=True):
retValue = True
connection = vistAClient.getConnection()
menuUtil = VistAMenuUtil(duz=1)
boxVolPair = getBoxVolPair(vistAClient)
logger.debug("Box:Vol Pair is [%s] " % boxVolPair)
menuUtil.gotoTaskmanEditParamMenu(vistAClient)
connection.send("Site Parameters Edit\r")
connection.expect("Select TASKMAN SITE PARAMETERS BOX-VOLUME PAIR: ")
connection.send("?\r")
connection.expect("Answer with TASKMAN SITE PARAMETERS BOX-VOLUME PAIR.*?:")
connection.expect("You may enter a new TASKMAN SITE PARAMETERS")
curBoxVol = connection.before.strip(' \r\n')
curBoxVol = [x.strip(' ') for x in curBoxVol.split('\r\n')]
logger.debug("Box:Vol Pair is [%s] " % curBoxVol)
if boxVolPair not in curBoxVol :
logger.error("taskman site parameter mismatch, current:[%s], correct:[%s]" %
(curBoxVol, boxVolPair))
if autoFix:
self.__fixTaskmanSiteParameter__(connection, curBoxVol[0], boxVolPair)
else:
retValue = False
connection.expect("Select TASKMAN SITE PARAMETERS BOX-VOLUME PAIR: ")
connection.send('\r')
menuUtil.exitTaskmanEditParamMenu(vistAClient)
return retValue
def getTaskmanStatus(self, vistAClient):
connection = vistAClient.getConnection()
menuUtil = VistAMenuUtil(duz=1)
menuUtil.gotoTaskmanMgrUtilMenu(vistAClient)
connection.send("MTM\r") # Monitor Taskman
curStatus = self.__getTaskmanStatus__(connection)
connection.send("^\r")
menuUtil.exitTaskmanMgrUtilMenu(vistAClient)
return curStatus
@staticmethod
def isTaskmanRunningCurrent(status):
return status == VistATaskmanUtil.TASKMAN_STATUS_RUNNING_CURRENT
@staticmethod
def isTaskmanInWaitState(status):
return status == VistATaskmanUtil.TASKMAN_STATUS_WAIT
def waitTaskmanToCurrent(self, vistAClient, timeOut=120):
DEFAULT_POLL_INTERVAL = 1 # 1 seconds
MaxRetry = timeOut/DEFAULT_POLL_INTERVAL
startRetry = 0
connection = vistAClient.getConnection()
menuUtil = VistAMenuUtil(duz=1)
menuUtil.gotoTaskmanMgrUtilMenu(vistAClient)
connection.send("MTM\r") # Monitor Taskman
while startRetry < MaxRetry:
curStatus = self.__getTaskmanStatus__(connection)
if self.isTaskmanRunningCurrent(curStatus):
break;
else:
startRetry += 1
time.sleep(DEFAULT_POLL_INTERVAL)
connection.send("\r")
if startRetry >= MaxRetry:
logger.error("Time out while waiting Taskman to Current")
connection.send("^\r")
menuUtil.exitTaskmanMgrUtilMenu(vistAClient)
def stopTaskman(self, vistAClient, shutdownSubMgrs=True,
shutdownActJobs=True):
connection = vistAClient.getConnection()
menuUtil = VistAMenuUtil(duz=1)
menuUtil.gotoTaskmanMgrUtilMenu(vistAClient)
connection.send("Stop Task Manager\r")
connection.expect("Are you sure you want to stop TaskMan\? ")
connection.send("YES\r")
connection.expect("Should active submanagers shut down after finishing their current tasks\? ")
if shutdownSubMgrs:
connection.send("YES\r")
else:
connection.send("NO\r")
connection.expect("Should active jobs be signaled to stop\? ")
if shutdownActJobs:
connection.send("YES\r")
else:
connection.send("NO\r")
menuUtil.exitTaskmanMgrUtilMenu(vistAClient)
logger.info("Wait 30 seconds for Taskman to stop")
time.sleep(30)
def placeTaskmanToWait(self, vistAClient, shutdownSubMgrs=True):
connection = vistAClient.getConnection()
menuUtil = VistAMenuUtil(duz=1)
menuUtil.gotoTaskmanMgrUtilMenu(vistAClient)
connection.send("Place Taskman in a WAIT State\r")
connection.expect("Should active submanagers shut down after finishing their current tasks\? ")
if shutdownSubMgrs:
connection.send("YES\r")
else:
connection.send("NO\r")
menuUtil.exitTaskmanMgrUtilMenu(vistAClient)
logger.info("Wait 10 seconds for Taskman to wait state")
time.sleep(10)
def removeTaskmanFromWait(self, vistAClient):
connection = vistAClient.getConnection()
menuUtil = VistAMenuUtil(duz=1)
menuUtil.gotoTaskmanMgrUtilMenu(vistAClient)
connection.send("Remove Taskman from WAIT State\r")
menuUtil.exitTaskmanMgrUtilMenu(vistAClient)
logger.info("Wait 10 seconds for Taskman to start")
time.sleep(10)
def shutdownAllTasks(self, vistAClient):
self.stopMailManBackgroundFiler(vistAClient)
self.stopHL7BackgroundFiler(vistAClient)
self.stopTaskman(vistAClient)
def stopMailManBackgroundFiler(self, vistAClient):
connection = vistAClient.getConnection()
menuUtil = VistAMenuUtil(duz=1)
menuUtil.gotoMailmanLocalDeliveryMgrMenu(vistAClient)
connection.send("STOP background filer\r")
connection.expect("Are you sure you want the Background Filers to stop delivering mail\? ")
connection.send("YES\r")
menuUtil.exitMailmanLocalDeliveryMgrMenu(vistAClient)
logger.info("Wait 30 seconds for Mailman backgroud filer to stop")
time.sleep(30)
def stopHL7BackgroundFiler(self, vistAClient):
connection = vistAClient.getConnection()
menuUtil = VistAMenuUtil(duz=1)
menuUtil.gotoHL7FilerLinkMgrMenu(vistAClient)
connection.send("Stop All Messaging Background Processes\r")
connection.expect("Okay to shut down all Links and Filers\? ")
connection.send("Yes\r")
menuUtil.exitHL7FilerLinkMgrMenu(vistAClient)
logger.info("Wait 30 seconds for HL7 backgroud filer to stop")
time.sleep(30)
""" Start taskman, it will not restart taskman if it is already started """
def startTaskman(self, vistAClient, waitToCurrent=True):
self.verifyTaskmanSiteParameter(vistAClient)
connection = vistAClient.getConnection()
menuUtil = VistAMenuUtil(duz=1)
menuUtil.gotoTaskmanMgrUtilMenu(vistAClient)
connection.send("Restart Task Manager\r")
index = connection.expect(["ARE YOU SURE YOU WANT TO RESTART ANOTHER TASKMAN\?",
"ARE YOU SURE YOU WANT TO RESTART TASKMAN\?"])
if index == 0:
connection.send("NO\r")
elif index == 1:
connection.send("YES\r")
connection.expect("Restarting...TaskMan restarted\!")
menuUtil.exitTaskmanMgrUtilMenu(vistAClient)
curStatus = self.getTaskmanStatus(vistAClient)
if self.isTaskmanInWaitState(curStatus):
self.removeTaskmanFromWait(vistAClient)
if waitToCurrent:
if not self.isTaskmanRunningCurrent(curStatus):
self.waitTaskmanToCurrent(vistAClient)
"""
Internal Implementation
"""
""" Fixed the BOX-VOLUME Pair """
def __fixTaskmanSiteParameter__(self, connection, curBoxVol, boxVol):
connection.expect("Select TASKMAN SITE PARAMETERS BOX-VOLUME PAIR: ")
connection.send("%s\r" % curBoxVol)
while True:
index = connection.expect(["BOX-VOLUME PAIR: %s//" % curBoxVol,
"CHOOSE [0-9]+-[0-9]+"])
if index == 0:
break
else:
choice = findChoiceNumber(connection.before, curBoxVol)
if choice:
connection.send('%s\r' % choice)
else:
connection.send('\r') # no match continue
connection.send("%s\r" % boxVol)
connection.expect(["//", ': '])
connection.send("^\r")
def __getTaskmanStatus__(self, connection):
connection.expect("Checking Taskman. ")
connection.expect("Taskman is ")
connection.expect("Checking the Status List:")
statusString = connection.before.strip(' \r\n')
logger.debug("Status String is %s" % statusString)
connection.expect("Node weight status time \$J")
connection.expect("Checking the Schedule List:")
detailedStatus = connection.before.strip(' \r\n')
logger.debug("Detailed Status String is %s" % detailedStatus)
connection.expect("Enter monitor action: UPDATE//")
return self.__taskmanStatusStringToEnum__(statusString, detailedStatus)
""" map taskman status to enum """
def __taskmanStatusStringToEnum__(self, statusString, detailedStatus):
if ( (statusString == "shutting down not running.." or
statusString == "not running..") and
detailedStatus == "The Status List is empty."):
return self.TASKMAN_STATUS_SHUTDOWN
if (statusString == "current.." and
detailedStatus.find("RUN") >= 0):
return self.TASKMAN_STATUS_RUNNING_CURRENT
if (detailedStatus.find("WAIT") >=0 and
detailedStatus.find("Taskman Waiting") >=0):
return self.TASKMAN_STATUS_WAIT
return self.TASKMAN_STATUS_UNKNOWN
DEFAULT_OUTPUT_LOG_FILE_NAME = "TaskmanUtil.log"
def main():
import logging
initConsoleLogging(logging.INFO)
testClientParser = createTestClientArgParser()
parser = argparse.ArgumentParser(description='VistA Taskman Utilities',
parents=[testClientParser])
parser.add_argument('-a', '--action', required=True,
choices=['Start', 'Stop', 'Shutdown'],
help='Start:Start Taskman, Stop:Stop Taskman, Shutdown:Shutdown all tasks')
result = parser.parse_args();
print result
""" create the VistATestClient"""
testClient = VistATestClientFactory.createVistATestClientWithArgs(result)
assert testClient
with testClient as vistAClient:
logFilename = getTempLogFile(DEFAULT_OUTPUT_LOG_FILE_NAME)
print "Log File is %s" % logFilename
vistAClient.setLogFile(logFilename)
taskmanUtil = VistATaskmanUtil()
actionMap = {"Start": taskmanUtil.startTaskman,
"Stop": taskmanUtil.stopTaskman,
"Shutdown": taskmanUtil.shutdownAllTasks}
actionMap[result.action](vistAClient)
if __name__ == '__main__':
main()