Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
memoriav
Memobase 2020
services
Import Process
Import API
Commits
335a3d74
Commit
335a3d74
authored
May 10, 2021
by
Jonas Waeber
Browse files
Add process config values to headers.
Handle KafkaTimeoutErrors.
parent
337b76d7
Changes
1
Hide whitespace changes
Inline
Side-by-side
import_api_app/resources/UpdateRecordSet.py
View file @
335a3d74
import
uuid
from
flask_restful
import
Resource
,
current_app
from
kafka
import
KafkaProducer
import
requests
import
json
import
traceback
from
kafka.errors
import
KafkaTimeoutError
from
requests.auth
import
HTTPBasicAuth
class
UpdateRecordSet
(
Resource
):
producer
=
KafkaProducer
(
bootstrap_servers
=
current_app
.
config
[
'kafka-broker-url'
],
value_serializer
=
lambda
m
:
json
.
dumps
(
m
,
ensure_ascii
=
False
)
.
encode
(
'utf-8'
))
# Todo write/correct comment for swagger
def
get
(
self
,
record_set_id
):
"""
...
...
@@ -211,24 +219,38 @@ class UpdateRecordSet(Resource):
'topic_key'
:
result_topic_value
.
get
(
'field_memobase_id'
),
'result_topic_value'
:
result_topic_value
,
},
500
self
.
send_message
(
result_topic_value
,
record_set_id
)
# 2. write info into kafka topic
def
send_message
(
self
,
result_topic_value
,
record_set_id
):
headers
=
[
(
'recordSetId'
,
bytes
(
record_set_id
)),
(
'sessionId'
,
uuid
.
uuid4
()),
(
'institutionId'
,
bytes
(
'none'
)),
(
'isPublished'
,
bytes
(
result_topic_value
[
'status'
]))
]
try
:
producer
=
KafkaProducer
(
bootstrap_servers
=
current_app
.
config
[
'kafka-broker-url'
],
value_serializer
=
lambda
m
:
json
.
dumps
(
m
,
ensure_ascii
=
False
)
.
encode
(
'utf-8'
))
key
=
bytes
(
result_topic_value
.
get
(
'field_memobase_id'
),
encoding
=
'utf-8'
)
producer
.
send
(
current_app
.
config
[
'topic-drupal-export'
],
result_topic_value
,
key
=
key
)
self
.
producer
.
send
(
current_app
.
config
[
'topic-drupal-export'
],
result_topic_value
,
key
=
key
,
headers
=
headers
)
except
KafkaTimeoutError
as
ex
:
msg
=
f
'KafkaTimeoutError (
{
record_set_id
}
):
{
ex
}
.'
current_app
.
logger
.
error
(
msg
)
return
{
'status'
:
'FAILURE'
,
'topic_key'
:
result_topic_value
.
get
(
'field_memobase_id'
),
'result_topic_value'
:
result_topic_value
,
'exception'
:
msg
},
503
except
Exception
as
ex
:
msg
=
'Exception for '
+
record_set_id
+
': '
+
str
(
ex
)
+
'
\n
'
+
\
traceback
.
format_exc
()
msg
=
'Could not import '
+
record_set_id
+
' (Unknown Exception): '
+
str
(
ex
)
current_app
.
logger
.
error
(
msg
)
return
{
'status'
:
'FAILURE'
,
'topic_key'
:
result_topic_value
.
get
(
'field_memobase_id'
),
'result_topic_value'
:
result_topic_value
,
'exception'
:
msg
},
50
0
},
50
3
current_app
.
logger
.
debug
(
'success for '
+
record_set_id
)
return
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment