Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delay-prediction-service: refactor scripts, add cleanup timed task #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion roles/delay-prediction-service/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
dest: /var/delay-prediction/
mode: "+x"
with_items:
- delay-prediction-service
- build
- start
- stop
- cleanup

- name: Install base packages
package:
Expand Down
20 changes: 20 additions & 0 deletions roles/delay-prediction-service/templates/build
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions roles/delay-prediction-service/templates/cleanup
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the timer file won't be copied to the server. You need to add them to the "Install systemd files" section.

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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 \
Expand All @@ -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}
3 changes: 3 additions & 0 deletions roles/delay-prediction-service/templates/stop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /bin/bash -e

docker stop --time 5 delay-prediction-service