-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (52 loc) · 2.72 KB
/
main.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
import requests
import time
# --- UPDATE WITH YOUR SERVER HERE ---- #
url = 'https://siddhix.bpmcep.ics.unisg.ch/engine-rest/external-task/' # Replace "siddhix" with your instance if you're using Camunda on our server.
# url = 'https://siddhix.bpmcep.ics.unisg.ch/engine-rest/external-task/' # Use this url if you're using Camunda on your machine.
# ------- #
# This is the payload of the request
fetchAndLockPayload = {"workerId": "myExampleWorker", # ID of the resource to which the task is assigned
"maxTasks": 1, # get only one running instance of the process
"usePriority": False, # don't sort instance by priority
"topics":
[{"topicName": "charge-card",
# name of task's topic (identifies the nature of the work to be performed)
"lockDuration": 30000 # duration of the lock
}]
}
try:
while True:
# Prepare the url to run the method fetchAndLock
fetchAndLock_url = url + 'fetchAndLock'
# Call API FetchAndLock with prepared url
response = requests.post(fetchAndLock_url, json=fetchAndLockPayload)
print('Fetch and lock status code: ', response.status_code)
print('Fetch and lock response: ', response.text)
responseJson = response.json() # JSON of the response
if len(responseJson) != 0:
# Get the first item of the response
task = responseJson[0]
taskId = task['id'] # get ID of the task
value = task['variables']['amount']['value'] # get value of variable amount
# Put you Business Logic here...
# Example: Print the value of the receipt if the amount is greater than 24
print_rec_value = (int(value)>24)
# Prepare the new request...
# Example: add to the request the values that you want to send back to Camunda server, e.g., print_rec = true
new_request = {
"workerId": "myExampleWorker",
"variables": {"print_rec": {"value": print_rec_value}}
}
# Complete the task and update the process variables
# Method: POST /task/{id}/complete
# Prepare the url to run the method complete
complete_url = (url + str(taskId) + '/complete')
# Call method "complete" with prepared url
complete = requests.post(complete_url, json=new_request)
print('complete status code: ', complete.status_code)
else:
time.sleep(2)
except KeyboardInterrupt:
print('Script interrupted by user.')
# except:
# print('Engine is down')