-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticImport.py
100 lines (95 loc) · 2.88 KB
/
StaticImport.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
#Written by Wang Weipeng on 2017/07/19
#This file is to trigger HCA content import using REST interface
import json
import os
import urllib.request
import time
from SNS import snsInform
from CDRinform import CDRinform
from logModule import logModule
class HttpHand:
def __init__(self,host,backend):
self.data={}
self.respCode = 0
self.respMsg = ""
self.host = host
self.headers = {'Authorization':'Basic'}
self.sns = snsInform()
self.backend = backend
self.cdrinform = CDRinform()
self.logger = logModule()
def PostHand(self):
#Trigger start import
packageStatus = [0,0]
packageStatus = self.GetHand()
thisTime = 0
while (1):
if(packageStatus == [1,0]):
if(thisTime == 0):
#print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())+ ' Start Import')
self.logger.info('Start Import')
try:
host = self.host+"/start"
req=urllib.request.Request(host,headers=self.headers,method='POST')
response = urllib.request.urlopen(req)
a = response.read().decode()
a = json.loads(a)
packageStatus = [0,0]
thisTime = 1
except Exception as e:
self.logger.error('Import Error due to %s'%e)
self.sns.informError()
print (e)
break
else:
#print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())+' Import success')
self.logger.info('Import Success')
self.sns.informComplete(self.backend)
self.cdrinform.sendInform(self.backend)
break
elif(packageStatus == [1,1]):
#print (thisTime)
#print (packageStatus)
if(thisTime == 1):
# print (thisTime)
self.logger.debug('Import finished but have errors')
#print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())+ ' Import finished but have errors.')
self.sns.informFail(self.backend)
self.cdrinform.sendInform(self.backend)
return 0
break
else:
packageStatus = [1, 0]
else:
#If importing waiting and get the status after 30 seconds
#Generally it will take about 7mins to import and in first 3 mins HCA couldn't reply importing status which will lead to error(Request start import but HCA is actually importing))
self.logger.info('Importing')
time.sleep(30)
packageStatus = self.GetHand()
def GetHand(self):
#Get current import progress
completedMark = 0
errorMark = 0
packageStatus = [0,0]
try:
host = self.host+"/report"
req=urllib.request.Request(host,headers=self.headers,method='GET')
response = urllib.request.urlopen(req)
a = response.read().decode()
a = json.loads(a)
#print(a)
for b in a.keys():
if (b == 'duration'):
completedMark = 1
packageStatus = [1,0]
if (completedMark==1):
for b in a.keys():
# print (b)
if(b=='errors'):
errorMark = 1
packageStatus = [1,1]
except Exception as e:
self.logger.error('Import failed due to %s'%e)
self.sns.informError()
print (e)
return packageStatus