Skip to content

Commit ae8320e

Browse files
committed
loop (+13 squashed commits)
Squashed commits: [c38c34b] loop [92e1c01] loop [74a6566] loop [89953c0] add async io [6d223d1] add async io [e02271b] add async io [fde0a26] add async io [999e375] add async io [69fbc35] add async io [f054b3f] add async io [d599139] downgrade python [6c768a4] downgrade python [7d6219b] downgrade python
1 parent 9da325d commit ae8320e

File tree

5 files changed

+60
-46
lines changed

5 files changed

+60
-46
lines changed

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ psycopg2 = "2.9.1"
2626
gunicorn = "20.1.0"
2727

2828
[requires]
29-
python_version = "3.9"
29+
python_version = "3.8.10"

Pipfile.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bpmn_types.py

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1+
import asyncio
12
from copy import deepcopy
23
from uuid import uuid4
34
from xml import etree
45

56
import aiohttp
67
import requests
78
import os
8-
from aiohttp.client import ClientSession, ClientTimeout, ContentTypeError
9+
910
import xml.etree.ElementTree as ET
1011

12+
from aiohttp import ContentTypeError, ClientSession, ClientTimeout
13+
1114
import env
1215
from bpmn_model import *
1316

14-
15-
timeout = ClientTimeout(sock_connect=5)
16-
client_session = ClientSession(timeout=timeout)
17-
1817
from utils.common import parse_expression, nested_dict_get, nested_dict_set
1918

2019
NS = {
@@ -24,6 +23,8 @@
2423

2524
BPMN_MAPPINGS = {}
2625

26+
timeout = ClientTimeout(sock_connect=5)
27+
2728

2829
def bpmn_tag(tag):
2930
def wrap(object):
@@ -252,44 +253,52 @@ async def run_connector(self, variables, instance_id):
252253
)
253254

254255
# Check method and make request
256+
async with aiohttp.ClientSession(timeout=timeout) as client_session:
257+
if method := self.connector_fields["input_variables"].get("method") or "GET":
258+
if method == "POST":
259+
call_function = client_session.post
260+
elif method == "PATCH":
261+
call_function = client_session.patch
262+
else:
263+
call_function = client_session.get
264+
if not isinstance(data, dict):
265+
data = dict(data)
266+
response = await call_function(
267+
url,
268+
params=parameters,
269+
json=data,
270+
headers={'content-type': 'application/json'}
271+
)
272+
if response.status not in (200, 201):
273+
raise Exception(response.text)
274+
275+
r = {}
276+
try:
277+
r = await response.json()
278+
except Exception as e:
279+
print("error")
280+
if not isinstance(e, ContentTypeError):
281+
raise e
282+
283+
# Check for output variables
284+
285+
if self.output_variables:
286+
for key in self.output_variables:
287+
value = self.output_variables.get(key)
288+
if len(value) > 0:
289+
variables[key] = parse_expression(expression=value, process_variables=r)
290+
if key in r:
291+
variables[key] = r[key]
292+
print(variables)
293+
return r
294+
295+
296+
255297

256-
if method := self.connector_fields["input_variables"].get("method") or "GET":
257298

258-
if method == "POST":
259-
call_function = client_session.post
260-
elif method == "PATCH":
261-
call_function = client_session.patch
262-
else:
263-
call_function = client_session.get
264-
if not isinstance(data,dict):
265-
data = dict(data)
266-
response = await call_function(
267-
url,
268-
params=parameters,
269-
json=data,
270-
headers={'content-type': 'application/json'}
271-
)
272-
if response.status not in (200, 201):
273-
raise Exception(response.text)
274299

275-
r = {}
276-
try:
277-
r = await response.json()
278-
except Exception as e:
279-
print("error")
280-
if not isinstance(e, ContentTypeError):
281-
raise e
282300

283-
# Check for output variables
284301

285-
if self.output_variables:
286-
for key in self.output_variables:
287-
value = self.output_variables.get(key)
288-
if len(value) > 0:
289-
variables[key] = parse_expression(expression=value, process_variables=r)
290-
if key in r:
291-
variables[key] = r[key]
292-
print(variables)
293302

294303
async def run(self, variables, instance_id):
295304

runtime.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-3.8.10

server.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import uuid
2+
13
import aiohttp
24
import os
3-
from aiohttp import web
45
from uuid import uuid4
56
import asyncio
7+
from aiohttp import web, ClientSession, ClientTimeout
68

79
from aiohttp.web_ws import WebSocketResponse
810

9-
from bpmn_model import BpmnModel, UserFormMessage, get_model_for_instance, ReceiveMessage, BpmnInstance
11+
from bpmn_model import BpmnModel, UserFormMessage, get_model_for_instance, ReceiveMessage
1012
import aiohttp_cors
1113
import db_connector
1214
from functools import reduce
@@ -86,7 +88,8 @@ async def handle_new_instance(request):
8688

8789
@routes.get("/model/{model_name}/instance")
8890
async def handle_new_instance(request):
89-
_id = str(uuid4())
91+
_id = str(uuid4()) + str(uuid.uuid1())
92+
9093
model = request.match_info.get("model_name")
9194
instance = await app["bpmn_models"][model].create_instance(_id, {})
9295
asyncio.create_task(instance.run())
@@ -171,7 +174,7 @@ async def handle_task_info(request):
171174
m: BpmnModel = get_model_for_instance(instance_id)
172175
if not m:
173176
raise aiohttp.web.HTTPNotFound
174-
instance: BpmnInstance = m.instances[instance_id]
177+
instance = m.instances[instance_id]
175178
task = instance.model.elements[task_id]
176179

177180
return web.json_response(task.get_info())
@@ -234,7 +237,7 @@ async def handle_instance_state(request):
234237
app = None
235238

236239

237-
def run():
240+
async def run():
238241
global app
239242
app = web.Application()
240243
app.on_startup.append(run_as_server)
@@ -264,4 +267,5 @@ async def serve():
264267

265268
if __name__ == "__main__":
266269
app = run()
270+
267271
web.run_app(app, port=os.environ.get('PORT', 9000))

0 commit comments

Comments
 (0)