Skip to content

Commit

Permalink
feat: Add thread to clear expire TD periodically
Browse files Browse the repository at this point in the history
The period can be defined with an environment variable
  • Loading branch information
Murloc6 committed Aug 30, 2023
1 parent 911483a commit 9521931
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The `config.toml` file can also be used to define FLask server configuration (c.
| [TDD__]TD_JSONSCHEMA | ./tdd/data/td-json-schema-validation.json | The path to the file containing JSON-Schema to validate the TDs |
| [TDD__]TD_ONTOLOGY | ./tdd/data/td.ttl | The path to the file containing the TD OWL Ontology (only used for SHACL validation) |
| [TDD__]TD_SHACL_VALIDATOR | ./tdd/data/td-validation.ttl | The path to the file containing the SHACL shapes (only used for SHACL validation) |
| [TDD__]PERIOD_CLEAR_EXPIRE_TD | 3600 | The number of seconds between each clearing of expire TD |

## Deploy to develop on the API

Expand Down
18 changes: 18 additions & 0 deletions tdd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import time
from threading import Thread
from flask import Flask, request, Response, stream_with_context
import json
import json_merge_patch
Expand All @@ -14,6 +15,7 @@
WrongMimeType,
)
from tdd.td import (
clear_expired_td,
get_all_tds,
get_paginated_tds,
get_total_number,
Expand Down Expand Up @@ -59,12 +61,28 @@ def wait_for_sparqlendpoint():
sys.exit(1)


def thread_clear_expire_td():
while True:
print("Clearing expired TD ...")
clear_expired_td()
print(
"Expired TD cleared (next clear in "
f"{CONFIG['PERIOD_CLEAR_EXPIRE_TD']} seconds)"
)
time.sleep(CONFIG["PERIOD_CLEAR_EXPIRE_TD"])


def create_app():
app = Flask(__name__)
app.config.from_file("../config.toml", load=toml.load)
wait_for_sparqlendpoint()
register_error_handler(app)
register_routes(app)

# Launch thread to clear expired TD regularly
t = Thread(target=thread_clear_expire_td)
t.start()

return app


Expand Down
2 changes: 2 additions & 0 deletions tdd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"CHECK_SCHEMA": False,
"MAX_TTL": None,
"MANDATE_TTL": False,
"PERIOD_CLEAR_EXPIRE_TD": 3600,
}

CONFIG = ConfigurationSet(
Expand Down Expand Up @@ -69,3 +70,4 @@ def _cast_to_int(fieldname):
CONFIG["MAX_TTL"] = _cast_to_int("MAX_TTL")
CONFIG["MANDATE_TTL"] = _cast_to_boolean("MANDATE_TTL")
CONFIG["ENDPOINT_TYPE"] = check_possible_endpoints()
CONFIG["PERIOD_CLEAR_EXPIRE_TD"] = _cast_to_int("PERIOD_CLEAR_EXPIRE_TD")

0 comments on commit 9521931

Please sign in to comment.