-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcam.py
More file actions
190 lines (128 loc) · 4.97 KB
/
Copy pathcam.py
File metadata and controls
190 lines (128 loc) · 4.97 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
184
185
186
187
188
import requests
import json
import time
import uuid
class client:
def __init__(self, url, workerid = "defaultid"):
self.url = url
self.workerid = workerid
def subscribe(self, topic, lockDuration = 1000, longPolling = 5000):
# Define the endpoint for fetch and lock
endpoint = str(self.url) +"/external-task/fetchAndLock"
# Define unique ID for the worker
#global uid
#uid = uuid.uuid1()
#uid = str(uid)
workerid = str(self.workerid)
#Define the Json for the Request
task= {"workerId": workerid,
"maxTasks":1,
"usePriority":"true",
"asyncResponseTimeout": longPolling,
"topics":
[{"topicName": topic,
"lockDuration": lockDuration
}]
}
#Make the request
global engine
engine = True
try:
fetch_and_lock = requests.post(endpoint, json=task)
print(fetch_and_lock.status_code)
global body
body = fetch_and_lock.text
except:
engine = False
print("Engine is down")
if(engine == True):
while body == '[]':
print("polling")
fetch_and_lock = requests.post(endpoint, json=task)
body = fetch_and_lock.text
time.sleep(5)
if body != '[]':
break
#Complete Call
def complete(self, **kwargs):
response_body = json.loads(body)
taskid = response_body[0]['id']
taskid = str(taskid)
endpoint = str(self.url) + "/external-task/" + taskid + "/complete"
#get workerid
workerid = response_body[0]['workerId']
workerid = str(workerid)
#puts the variables from the dictonary into the nested format for the json response
variables_for_response = {}
for key, val in kwargs.items():
variable_new = {key:{"value": val}}
variables_for_response.update(variable_new)
response= {"workerId": workerid,
"variables": variables_for_response
}
try:
complete = requests.post(endpoint, json =response)
body_complete = complete.text
print(body_complete)
print(complete.status_code)
except:
print('fail')
#BPMN Error
def error(self, bpmn_error, error_message = "not defined", **kwargs):
response_body = json.loads(body)
taskid = response_body[0]['id']
taskid = str(taskid)
endpoint = str(self.url) + "/external-task/"+ taskid + "/bpmnError"
workerid = response_body[0]['workerId']
workerid = str(workerid)
variables_for_response = {}
for key, val in kwargs.items():
variable_new = {key:{"value": val}}
variables_for_response.update(variable_new)
response = {
"workerId": workerid,
"errorCode": bpmn_error,
"errorMessage": error_message,
"variables": variables_for_response
}
try:
error = requests.post(endpoint, json = response)
print(error.status_code)
except:
print('fail')
#Create an incident
def fail(self, error_message, retries = 0, retry_timeout= 0):
response_body = json.loads(body)
taskid = response_body[0]['id']
taskid = str(taskid)
endpoint = str(self.url) + "/external-task/"+ taskid + "/failure"
workerid = response_body[0]['workerId']
workerid = str(workerid)
response = {
"workerId": workerid,
"errorMessage": error_message,
"retries": retries,
"retryTimeout": retry_timeout}
try:
fail = requests.post(endpoint, json = response)
print(fail.status_code)
except:
print('fail')
# New Lockduration
def new_lockduration(self, new_duration):
response_body = json.loads(body)
taskid = response_body[0]['id']
taskid = str(taskid)
endpoint = str(self.url) + "/external-task/"+ taskid + "/extendLock"
workerid = response_body[0]['workerId']
workerid = str(workerid)
response = {
"workerId": workerid,
"newDuration": new_duration
}
try:
newDuration = requests.post(endpoint, json = response)
print(newDuration.status_code)
print(workerid)
except:
print('fail')