Skip to content

Commit 93ad0c3

Browse files
committed
Merge branch 'master' into feature/better_error_handling
2 parents 7c20c4e + e9c2709 commit 93ad0c3

File tree

8 files changed

+65
-23
lines changed

8 files changed

+65
-23
lines changed

.woodpecker/.feature.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
pipeline:
1+
steps:
22
build-and-push:
3-
image: plugins/docker
3+
image: woodpeckerci/plugin-docker-buildx
44
settings:
55
repo: "${CI_REPO_OWNER##mu-}/${CI_REPO_NAME}"
66
tags: "feature-${CI_COMMIT_BRANCH##feature/}"
7-
secrets: [docker_username, docker_password]
7+
username:
8+
from_secret: docker_username
9+
password:
10+
from_secret: docker_password
811
when:
9-
event: push
10-
branch: feature/*
12+
- event: push
13+
branch: [feature/*]

.woodpecker/.latest.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
pipeline:
1+
steps:
22
build-and-push:
3-
image: plugins/docker
3+
image: woodpeckerci/plugin-docker-buildx
44
settings:
55
repo: "${CI_REPO_OWNER##mu-}/${CI_REPO_NAME}"
66
tags: latest
7-
secrets: [docker_username, docker_password]
7+
username:
8+
from_secret: docker_username
9+
password:
10+
from_secret: docker_password
811
when:
9-
event: push
10-
branch: [master, main]
12+
- event: push
13+
branch: [master, main]

.woodpecker/.release.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
pipeline:
1+
steps:
22
release:
3-
image: plugins/docker
3+
image: woodpeckerci/plugin-docker-buildx
44
settings:
55
repo: "${CI_REPO_OWNER##mu-}/${CI_REPO_NAME}"
66
tags: "${CI_COMMIT_TAG##v}"
7-
secrets: [ docker_username, docker_password ]
7+
username:
8+
from_secret: docker_username
9+
password:
10+
from_secret: docker_password
811
when:
9-
event: tag
10-
tag: v*
12+
- event: tag
13+
ref: refs/tags/v*

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM tiangolo/meinheld-gunicorn:python3.8
2-
MAINTAINER Michaël Dierick "[email protected]"
2+
LABEL maintainer="[email protected]"
33

44
# Gunicorn Docker config
55
ENV MODULE_NAME web
@@ -14,9 +14,11 @@ RUN chmod +x /start.sh
1414
# Template config
1515
ENV APP_ENTRYPOINT web
1616
ENV LOG_LEVEL info
17+
ENV LOG_SPARQL_ALL True
1718
ENV MU_SPARQL_ENDPOINT 'http://database:8890/sparql'
1819
ENV MU_SPARQL_UPDATEPOINT 'http://database:8890/sparql'
1920
ENV MU_APPLICATION_GRAPH 'http://mu.semte.ch/application'
21+
ENV MODE 'production'
2022

2123
RUN mkdir -p /usr/src/app
2224
WORKDIR /usr/src/app

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Mu Python template
22

3-
Template for [mu.semte.ch](http://mu.semte.ch)-microservices written in Python3. Based on the [Flask](https://palletsprojects.com/p/flask/)-framework.
3+
Template for [mu.semte.ch](http://mu.semte.ch)-microservices written in Python3.8. Based on the [Flask](https://palletsprojects.com/p/flask/)-framework.
44

55
## Quickstart
66

77
Create a `Dockerfile` which extends the `semtech/mu-python-template`-image and set a maintainer.
88
```docker
99
FROM semtech/mu-python-template:2.0.0-beta.2
10-
LABEL maintainer="sam.landuydt@gmail.com"
10+
LABEL maintainer="maintainer@example.com"
1111
```
1212

1313
Create a `web.py` entrypoint-file. (naming of the entrypoint can be configured through `APP_ENTRYPOINT`)
@@ -300,6 +300,7 @@ my-python:
300300
```
301301

302302
### Environment variables
303+
#### General
303304

304305
- `LOG_LEVEL` takes the same options as defined in the Python [logging](https://docs.python.org/3/library/logging.html#logging-levels) module.
305306

@@ -317,7 +318,16 @@ my-python:
317318

318319
- `MU_SPARQL_TIMEOUT` is used to configure the timeout (in seconds) for SPARQL queries.
319320

321+
#### SPARQL Query Logging
322+
- `LOG_SPARQL_ALL`: Log *all* executed queries, read as well as update (default `True`)
320323

324+
- `LOG_SPARQL_QUERIES`: Log *read* queries (default: `undefined`). Overrules `LOG_SPARQL_ALL`
325+
326+
- `LOG_SPARQL_UPDATES`: Log *update* queries (default `undefined`). Overrules `LOG_SPARQL_ALL`.
327+
328+
The string "true", ignoring casing, is considered `True`. All other values are considered `False`.
329+
330+
#### Meinheld Gunicorn Docker Variables
321331
Since this template is based on the meinheld-gunicorn-docker image, all possible environment config for that image is also available for the template. See [meinheld-gunicorn-docker#environment-variables](https://github.com/tiangolo/meinheld-gunicorn-docker#environment-variables) for more info. The template configures `WEB_CONCURRENCY` in particular to `1` by default.
322332

323333
### Production

escape_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def sparql_escape_int(obj):
5353

5454
def sparql_escape_float(obj):
5555
"""Converts the given float to a SPARQL-safe RDF object string with the right RDF-datatype. """
56-
if not isinstance(obj, int):
56+
if not isinstance(obj, float):
5757
warn("You are escaping something that isn't a float with \
5858
the 'sparql_escape_float'-method. Implicit casting will occurr.")
5959
obj = str(float(obj))

helpers.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@
4242
consoleHandler = logging.StreamHandler(stream=sys.stdout)# or stderr?
4343
logger.addHandler(consoleHandler)
4444

45+
LOG_SPARQL_ALL_VAR = os.environ.get('LOG_SPARQL_ALL')
46+
LOG_SPARQL_QUERIES = os.environ.get(
47+
'LOG_SPARQL_QUERIES',
48+
default=LOG_SPARQL_ALL_VAR
49+
).lower() == 'true'
50+
LOG_SPARQL_UPDATES = os.environ.get(
51+
'LOG_SPARQL_UPDATES',
52+
default=LOG_SPARQL_ALL_VAR
53+
).lower() == 'true'
54+
4555
def generate_uuid():
4656
"""Generates a random unique user id (UUID) based on the host ID and current time"""
4757
return str(uuid.uuid1())
@@ -119,15 +129,20 @@ def validate_resource_type(expected_type, data):
119129

120130
def query(the_query):
121131
"""Execute the given SPARQL query (select/ask/construct) on the triplestore and returns the results in the given return Format (JSON by default)."""
122-
log("execute query: \n" + the_query)
123132
for header in MU_HEADERS:
124133
if header in request.headers:
125134
sparqlQuery.customHttpHeaders[header] = request.headers[header]
126135
else: # Make sure headers used for a previous query are cleared
127136
if header in sparqlQuery.customHttpHeaders:
128137
del sparqlQuery.customHttpHeaders[header]
129138
sparqlQuery.setQuery(the_query)
130-
return sparqlQuery.query().convert()
139+
if LOG_SPARQL_QUERIES:
140+
log("Execute query: \n" + the_query)
141+
try:
142+
return sparqlQuery.query().convert()
143+
except Exception as e:
144+
log("Failed Query: \n" + the_query)
145+
raise e
131146

132147

133148
def update(the_query):
@@ -140,7 +155,13 @@ def update(the_query):
140155
del sparqlUpdate.customHttpHeaders[header]
141156
sparqlUpdate.setQuery(the_query)
142157
if sparqlUpdate.isSparqlUpdateRequest():
143-
sparqlUpdate.query()
158+
if LOG_SPARQL_UPDATES:
159+
log("Execute query: \n" + the_query)
160+
try:
161+
sparqlUpdate.query()
162+
except Exception as e:
163+
log("Failed Query: \n" + the_query)
164+
raise e
144165

145166

146167
def update_modified(subject, modified=datetime.datetime.now()):

start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /usr/bin/env bash
22
set -eu
3-
if [ ${MODE:-""} == "development" ]; then
3+
if [ "${MODE}" == "development" ]; then
44
if [ -f /app/requirements.txt ]; then pip install -r /app/requirements.txt; fi
55
exec flask --app web.py run --debug --port 80 --host "0.0.0.0"
66
else

0 commit comments

Comments
 (0)