diff --git a/docker/Dockerfile b/docker/Dockerfile index a40bd91fe..558c9c556 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -9,7 +9,8 @@ ENV DEBIAN_FRONTEND noninteractive ENV BUILD_PACKAGES apt-utils apt-file libpq-dev graphviz-dev build-essential git pkg-config \ python3-dev libxml2-dev libjpeg-dev libssl-dev libffi-dev libxslt1-dev \ - libcairo2-dev software-properties-common python3-setuptools python3-pip + libcairo2-dev software-properties-common python3-setuptools python3-pip \ + cron ## NAO EH PRA TIRAR O vim DA LISTA DE COMANDOS INSTALADOS!!! ENV RUN_PACKAGES graphviz python3-lxml python3-magic postgresql-client python3-psycopg2 \ @@ -71,3 +72,9 @@ EXPOSE 80/tcp 443/tcp VOLUME ["/var/interlegis/sapl/data", "/var/interlegis/sapl/media"] CMD ["/var/interlegis/sapl/start.sh"] + +COPY cronjob /etc/cron.d/update_solr_index +RUN chmod 0644 /etc/cron.d/update_solr_index +RUN crontab /etc/cron.d/update_solr_index +RUN touch /var/log/cron.log +CMD cron && tail -f /var/log/cron.log diff --git a/docker/random_cron_job.sh b/docker/random_cron_job.sh new file mode 100644 index 000000000..daa478a6a --- /dev/null +++ b/docker/random_cron_job.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +SOLR_URL=$1 +# Define the interval of time to run the cronjob +RANDOM_MINUTE_MIN=0 +RANDOM_MINUTE_MAX=60 +RANDOM_HOUR_MIN=0 +RANDOM_HOUR_MAX=3 + +# Generate a random minute within the interval +RANDOM_MINUTE=$((RANDOM % ($RANDOM_MINUTE_MAX-$RANDOM_MINUTE_MIN+1) + $RANDOM_MINUTE_MIN)) +RANDOM_HOUR=$((RANDOM % ($RANDOM_HOUR_MAX-$RANDOM_HOUR_MIN+1) + $RANDOM_HOUR_MIN)) + +# Add the cronjob to the crontab +echo "$RANDOM_MINUTE $RANDOM_HOUR * * * /path/to/command" >> /etc/crontab + +# Start the cron daemon +crond -f -L /dev/stdout diff --git a/docker/solr_cli.py b/docker/solr_cli.py index d452d1fe9..c375ed8c7 100755 --- a/docker/solr_cli.py +++ b/docker/solr_cli.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- import argparse +import datetime import logging import re import secrets @@ -277,9 +278,10 @@ if __name__ == '__main__': help='Replication factor (default=1)', default=1) parser.add_argument('-ms', type=int, dest='max_shards_per_node', nargs='?', help='Max shards per node (default=1)', default=1) - parser.add_argument("--embedded_zk", default=False, action="store_true", help="Embedded ZooKeeper") + parser.add_argument("--rebuild_index", default=False, action="store_true",) + parser.add_argument("--update_index", default=False, action="store_true",) try: args = parser.parse_args() @@ -315,3 +317,12 @@ if __name__ == '__main__': if num_docs == 0: print("Performing a full reindex of '%s' collection..." % collection) p = subprocess.call(["python3", "manage.py", "rebuild_index", "--noinput"]) + + if args.rebuild_index: + print("Rebuilding index of '%s' collection..." % collection) + p = subprocess.call(["python3", "manage.py", "rebuild_index", "--noinput"]) + + if args.update_index: + print("Updating index of '%s' collection..." % collection) + p = subprocess.call(["python3", "manage.py", "update_index", "--noinput"]) + diff --git a/docker/start.sh b/docker/start.sh index 558b7d7b5..65689ebfa 100755 --- a/docker/start.sh +++ b/docker/start.sh @@ -85,6 +85,21 @@ if [ "${USE_SOLR-False}" == "True" ] || [ "${USE_SOLR-False}" == "true" ]; then fi python3 solr_cli.py -u $SOLR_URL -c $SOLR_COLLECTION -s $NUM_SHARDS -rf $RF -ms $MAX_SHARDS_PER_NODE $ZK_EMBEDDED & + + RANDOM_MINUTE_MIN=0 + RANDOM_MINUTE_MAX=60 + RANDOM_HOUR_MIN=0 + RANDOM_HOUR_MAX=3 + + # Generate a random minute within the interval + RANDOM_MINUTE=$((RANDOM % ($RANDOM_MINUTE_MAX-$RANDOM_MINUTE_MIN+1) + $RANDOM_MINUTE_MIN)) + RANDOM_HOUR=$((RANDOM % ($RANDOM_HOUR_MAX-$RANDOM_HOUR_MIN+1) + $RANDOM_HOUR_MIN)) + + # Add the cronjob to the crontab + echo "$RANDOM_MINUTE $RANDOM_HOUR * * * python3 solr_cli.py -u $SOLR_URL -c $SOLR_COLLECTION --update-index" >> /etc/cron.daily/update_solr_index + + # Start the cron daemon + crond -f -L /dev/stdout else echo "Solr is offline, not possible to connect." fi