Skip to content

Commit

Permalink
Switch to use cron job
Browse files Browse the repository at this point in the history
Dockerfile:
    * Updating base image

Signed-off-by: Tuan T. Pham <[email protected]>
  • Loading branch information
neofob committed Nov 26, 2023
1 parent 60c4624 commit 28b3c04
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
16 changes: 11 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
FROM python:alpine3.18 as builder
FROM python:3.9.18-alpine3.18 as builder
LABEL maintainer "tuan t. pham" <[email protected]>

ENV PKGS="py3-virtualenv"

RUN apk update && apk add ${PKGS} \
&& mkdir -p /opt/pp-code-metrics

COPY ./requirements.txt /opt/pp-code-metrics

RUN cd /opt/pp-code-metrics \
&& virtualenv metrics \
&& source /opt/pp-code-metrics/metrics/bin/activate \
Expand All @@ -17,9 +15,10 @@ RUN cd /opt/pp-code-metrics \

FROM mikefarah/yq:latest as yq_base

FROM python:alpine3.18 as deploy
FROM python:3.9.18-alpine3.18 as deploy

ENV PKGS="py3-virtualenv"
ENV DOCKERIZE_URL="https://github.com/jwilder/dockerize/releases/download/v0.7.0/dockerize-alpine-linux-amd64-v0.7.0.tar.gz"

RUN apk update && apk add ${PKGS} \
&& mkdir -p /opt/pp-code-metrics
Expand All @@ -34,4 +33,11 @@ COPY --from=yq_base /usr/bin/yq /usr/bin/yq
WORKDIR /opt/pp-code-metrics
ENV VIRT_PYTHON=/opt/pp-code-metrics/metrics/bin/python3

CMD ["/opt/pp-code-metrics/run_get_metrics.sh", "my_settings.yml"]
RUN wget -O - ${DOCKERIZE_URL} | tar xzf - -C /usr/local/bin

RUN mkdir -p /etc/cron.d
COPY crontab/pp-code-metrics.tmpl /etc/cron.d/
ENTRYPOINT dockerize --template /etc/cron.d/pp-code-metrics.tmpl:/etc/cron.d/pp-code-metrics crontab /etc/cron.d/pp-code-metrics && \
/usr/sbin/crond -f -d 0

#CMD ["/opt/pp-code-metrics/run_get_metrics.sh", "my_settings.yml"]
5 changes: 5 additions & 0 deletions crontab/pp-code-metrics.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Run get_metrics.py every 10 minutes

#

*/{{ .Env.INTERVAL }} * * * * /opt/pp-code-metrics/run_get_metrics.sh my_settings.yml
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ services:
hostname: pp-code-metrics
image: neofob/pp-code-metrics:latest
restart: always
environment:
INTERVAL: 10
volumes:
- ./my_settings.yml:/opt/pp-code-metrics/my_settings.yml:ro
deploy:
resources:
limits:
memory: 32M
memory: 64M
logging:
driver: "json-file"
options:
Expand Down
2 changes: 1 addition & 1 deletion get_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def getNodeMetrics(node):
for i in range(3):
try:
write_api.write(bucket=influxdb_bucket, record=p)
except NewConnectionError:
except ConnectionError:
time.sleep(5)
countinue
if try_again:
Expand Down
10 changes: 4 additions & 6 deletions run_get_metrics.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env sh
set -o nounset # Treat unset variables as an error

INTERVAL=$(cat $1 | yq '.General.interval')
echo "INTERVAL=$INTERVAL"
#INTERVAL=$(cat $1 | yq '.General.interval')
#echo "INTERVAL=$INTERVAL"
#INTERVAL=${INTERVAL:=600}

VIRT_PYTHON=${VIRT_PYTHON:=/home/vagrant/.virtualenvs/metrics/bin/python3}

while true; do
${VIRT_PYTHON} ./get_metrics.py --config $1
sleep ${INTERVAL}s
done
cd /opt/pp-code-metrics
${VIRT_PYTHON} ./get_metrics.py --config $1
3 changes: 2 additions & 1 deletion sample_settings.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Default settings
---
General:
Interval: 600
# It is in minutes for cron job config now
Interval: 10

InfluxDB:
# avoid using 'localhost' when this is deployed to be used by get_metrics.py
Expand Down

0 comments on commit 28b3c04

Please sign in to comment.