From 6fdda8147402810bced3c183a8faf3bb5b49c472 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Tue, 29 Jun 2021 20:03:09 +0200 Subject: [PATCH 1/2] delay-prediction-service: split into 3 scripts build, start & stop --- roles/delay-prediction-service/tasks/main.yml | 4 +++- .../delay-prediction-service/templates/build | 20 +++++++++++++++++++ .../templates/delay-prediction.service | 4 ++-- .../{delay-prediction-service => start} | 10 ++-------- roles/delay-prediction-service/templates/stop | 3 +++ 5 files changed, 30 insertions(+), 11 deletions(-) create mode 100755 roles/delay-prediction-service/templates/build rename roles/delay-prediction-service/templates/{delay-prediction-service => start} (73%) mode change 100644 => 100755 create mode 100755 roles/delay-prediction-service/templates/stop diff --git a/roles/delay-prediction-service/tasks/main.yml b/roles/delay-prediction-service/tasks/main.yml index fa99a44f..460917f8 100644 --- a/roles/delay-prediction-service/tasks/main.yml +++ b/roles/delay-prediction-service/tasks/main.yml @@ -15,7 +15,9 @@ dest: /var/delay-prediction/ mode: "+x" with_items: - - delay-prediction-service + - build + - start + - stop - name: Install base packages package: diff --git a/roles/delay-prediction-service/templates/build b/roles/delay-prediction-service/templates/build new file mode 100755 index 00000000..ad0ad0a7 --- /dev/null +++ b/roles/delay-prediction-service/templates/build @@ -0,0 +1,20 @@ +#! /bin/bash -e + +DOCKER_IMAGE=stadtnavi/delay-prediction-service:{{ delay_prediction_service_version }} + +docker pull ${DOCKER_IMAGE} +docker run --rm -i \ + --network delay \ + -v delay-prediction-service-data:/app/data \ + -e TIMEZONE=Europe/Berlin \ + -e LOCALE=de-DE \ + -e GTFS_NAME=vvs \ + -e GTFS_URL='https://www.openvvs.de/dataset/e66f03e4-79f2-41d0-90f1-166ca609e491/resource/bfbb59c7-767c-4bca-bbb2-d8d32a3e0378/download/google_transit.zip' \ + -e PGHOST="delay-postgis" \ + -e PGUSER="{{ delay_postgres_user }}" \ + -e PGPASSWORD="{{ delay_postgres_password }}" \ + -e PGDATABASE="{{ delay_postgres_db }}" \ + -e LOG_LEVEL=debug \ + --name delay-prediction-service-build \ + ${DOCKER_IMAGE} \ + ./scripts/build.sh diff --git a/roles/delay-prediction-service/templates/delay-prediction.service b/roles/delay-prediction-service/templates/delay-prediction.service index 9cc57c8c..c142ea55 100644 --- a/roles/delay-prediction-service/templates/delay-prediction.service +++ b/roles/delay-prediction-service/templates/delay-prediction.service @@ -9,8 +9,8 @@ StartLimitIntervalSec=180 Restart=always RestartSec=30 -ExecStart=/var/delay-prediction/delay-prediction-service service -ExecStop=-docker stop delay-prediction-service +ExecStart=/var/delay-prediction/start +ExecStop=/var/delay-prediction/stop [Install] WantedBy=multi-user.target diff --git a/roles/delay-prediction-service/templates/delay-prediction-service b/roles/delay-prediction-service/templates/start old mode 100644 new mode 100755 similarity index 73% rename from roles/delay-prediction-service/templates/delay-prediction-service rename to roles/delay-prediction-service/templates/start index c1ac3c2b..9155a6f8 --- a/roles/delay-prediction-service/templates/delay-prediction-service +++ b/roles/delay-prediction-service/templates/start @@ -1,9 +1,6 @@ #! /bin/bash -e DOCKER_IMAGE=stadtnavi/delay-prediction-service:{{ delay_prediction_service_version }} -CONTAINER_NAME=delay-prediction-$1 - -shift docker pull ${DOCKER_IMAGE} docker run --rm -i \ @@ -16,14 +13,11 @@ docker run --rm -i \ -e THINGSBOARD_PASSWORD="{{ thingsboard_password }}" \ -e THINGSBOARD_DEVICE_GROUP="f1740240-7b3f-11eb-970d-837f8b4d738d" \ -e MQTT_URI="{{ mqtt_uri }}" \ - -e GTFS_NAME=vvs \ - -e GTFS_URL='https://www.openvvs.de/dataset/e66f03e4-79f2-41d0-90f1-166ca609e491/resource/bfbb59c7-767c-4bca-bbb2-d8d32a3e0378/download/google_transit.zip' \ -e PGHOST="delay-postgis" \ -e PGUSER="{{ delay_postgres_user }}" \ -e PGPASSWORD="{{ delay_postgres_password }}" \ -e PGDATABASE="{{ delay_postgres_db }}" \ -e LOG_LEVEL=debug \ -p {{ delay_prediction_service_port }}:3000 \ - --name ${CONTAINER_NAME} \ - ${DOCKER_IMAGE} \ - $@ + --name delay-prediction-service \ + ${DOCKER_IMAGE} diff --git a/roles/delay-prediction-service/templates/stop b/roles/delay-prediction-service/templates/stop new file mode 100755 index 00000000..f53f9653 --- /dev/null +++ b/roles/delay-prediction-service/templates/stop @@ -0,0 +1,3 @@ +#! /bin/bash -e + +docker stop --time 5 delay-prediction-service From 762f7b583d33eb749ae254d9556981c4ad745860 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Tue, 29 Jun 2021 20:03:58 +0200 Subject: [PATCH 2/2] delay-prediction-service: add periodic cleanup task --- roles/delay-prediction-service/tasks/main.yml | 1 + .../templates/cleanup | 19 +++++++++++++++++++ .../delay-prediction-cleanup.service | 17 +++++++++++++++++ .../templates/delay-prediction-cleanup.timer | 9 +++++++++ 4 files changed, 46 insertions(+) create mode 100755 roles/delay-prediction-service/templates/cleanup create mode 100644 roles/delay-prediction-service/templates/delay-prediction-cleanup.service create mode 100644 roles/delay-prediction-service/templates/delay-prediction-cleanup.timer diff --git a/roles/delay-prediction-service/tasks/main.yml b/roles/delay-prediction-service/tasks/main.yml index 460917f8..a66839b2 100644 --- a/roles/delay-prediction-service/tasks/main.yml +++ b/roles/delay-prediction-service/tasks/main.yml @@ -18,6 +18,7 @@ - build - start - stop + - cleanup - name: Install base packages package: diff --git a/roles/delay-prediction-service/templates/cleanup b/roles/delay-prediction-service/templates/cleanup new file mode 100755 index 00000000..3c8a31c0 --- /dev/null +++ b/roles/delay-prediction-service/templates/cleanup @@ -0,0 +1,19 @@ +#! /bin/bash -e + +DOCKER_IMAGE=stadtnavi/delay-prediction-service:{{ delay_prediction_service_version }} + +docker pull ${DOCKER_IMAGE} +docker run --rm -i \ + --network delay \ + -v delay-prediction-service-data:/app/data \ + -e TIMEZONE=Europe/Berlin \ + -e LOCALE=de-DE \ + -e GTFS_NAME=vvs \ + -e PGHOST="delay-postgis" \ + -e PGUSER="{{ delay_postgres_user }}" \ + -e PGPASSWORD="{{ delay_postgres_password }}" \ + -e PGDATABASE="{{ delay_postgres_db }}" \ + -e LOG_LEVEL=debug \ + --name delay-prediction-service-cleanup \ + ${DOCKER_IMAGE} \ + ./scripts/cleanup.js diff --git a/roles/delay-prediction-service/templates/delay-prediction-cleanup.service b/roles/delay-prediction-service/templates/delay-prediction-cleanup.service new file mode 100644 index 00000000..6e48db21 --- /dev/null +++ b/roles/delay-prediction-service/templates/delay-prediction-cleanup.service @@ -0,0 +1,17 @@ +[Unit] +Description=delete old delay-prediction-service data +Requires=docker.service +After=docker.service + +# restart once within 20m to handle an intermittent network failure gracefully +StartLimitIntervalSec=1200 +StartLimitBurst=1 + +[Service] +ExecStart=/var/delay-prediction/cleanup +Restart=on-failure + +ExecStopPost=/bin/sh -c 'if [ "$$EXIT_STATUS" != "0" ]; then send-to-matrix "delay-prediction-cleanup failed on {{ inventory_hostname }}. EXIT_STATUS:$$EXIT_STATUS, SERVICE_RESULT:$$SERVICE_RESULT, EXIT_CODE:$$EXIT_CODE"; fi' + +[Install] +WantedBy=multi-user.target diff --git a/roles/delay-prediction-service/templates/delay-prediction-cleanup.timer b/roles/delay-prediction-service/templates/delay-prediction-cleanup.timer new file mode 100644 index 00000000..f9662157 --- /dev/null +++ b/roles/delay-prediction-service/templates/delay-prediction-cleanup.timer @@ -0,0 +1,9 @@ +[Unit] +Description=delete old delay-prediction-service data every night + +[Timer] +OnCalendar=*-*-* 04:30:00 +Persistent=true + +[Install] +WantedBy=timers.target