Cronjob works now between Nextcloud container recreation
If the Nextcloud container is recreated while the cronjob container is still running, the ID of the container is obtained each time the cron tasks are executed. Previously it was cached on entrypoint, so if the ID changed this container would fail.
This commit is contained in:
parent
0bfd68a109
commit
6416dd8731
5 changed files with 35 additions and 36 deletions
26
README.md
26
README.md
|
@ -53,16 +53,16 @@ entirely.
|
||||||
the service, if `NEXTCLOUD_PROJECT_NAME` is specified).
|
the service, if `NEXTCLOUD_PROJECT_NAME` is specified).
|
||||||
|
|
||||||
* `NEXTCLOUD_PROJECT_NAME`<br>
|
* `NEXTCLOUD_PROJECT_NAME`<br>
|
||||||
The name of the project if you're using Docker Compose. The name of
|
The name of the project if you're using Docker Compose. The name of the project, by default, is
|
||||||
the project, by default, is the name of the context directory you ran your `docker-compose.yml`
|
the name of the context directory you ran your `docker-compose.yml` from. This helps to build a
|
||||||
from. This helps to build a "hint" used to identify the Nextcloud container by name. The hint is
|
"hint" used to identify the Nextcloud container by name. The hint is built as:
|
||||||
built as:
|
|
||||||
|
|
||||||
|
```txt
|
||||||
${NEXTCLOUD_PROJECT_NAME}_${NEXTCLOUD_CONTAINER_NAME}
|
${NEXTCLOUD_PROJECT_NAME}_${NEXTCLOUD_CONTAINER_NAME}
|
||||||
|
```
|
||||||
|
|
||||||
* `NEXTCLOUD_CRON_MINUTE_INTERVAL`<br>
|
* `NEXTCLOUD_CRON_MINUTE_INTERVAL`<br>
|
||||||
The interval, in minutes, of how often the cron task
|
The interval, in minutes, of how often the cron task executes. The default is 15 minutes.
|
||||||
executes. The default is 15 minutes.
|
|
||||||
|
|
||||||
* `NEXTCLOUD_EXEC_USER`<br>
|
* `NEXTCLOUD_EXEC_USER`<br>
|
||||||
The user that should be used to run the cron tasks inside the Nextcloud container. This parameter
|
The user that should be used to run the cron tasks inside the Nextcloud container. This parameter
|
||||||
|
@ -79,10 +79,12 @@ is checked every interval of the health check. If any of these checks fail, it i
|
||||||
container's health status will become *unhealthy*. In this case, you should restart the container.
|
container's health status will become *unhealthy*. In this case, you should restart the container.
|
||||||
|
|
||||||
1. The `crond` process must be running.
|
1. The `crond` process must be running.
|
||||||
2. The Nextcloud container must be available and running. One important note here: When this
|
2. The Nextcloud container must be available and running.
|
||||||
container starts up, it immediately searches for the container by name and remembers it by the
|
|
||||||
container's ID. If for whatever reason the Nextcloud container changes in such a way that the ID
|
Because the Nextcloud container can be restarted while the the cronjob container is running, its
|
||||||
is no longer valid, the health check would fail.
|
container ID is not cached. Each time the cron task executes, it searches for the ID of the
|
||||||
|
container. This ensures that even if you restart the Nextcloud container, the cronjob container will
|
||||||
|
always function.
|
||||||
|
|
||||||
## Customizing Cron Tasks
|
## Customizing Cron Tasks
|
||||||
|
|
||||||
|
@ -99,7 +101,7 @@ in addition to the default `cron.php` task. To add your custom tasks, follow the
|
||||||
php -f /var/www/html/cron.php
|
php -f /var/www/html/cron.php
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Mount this shell script inside the `/cron-scripts` directory. Here's an example if you're using
|
2. Mount this shell script inside the `/cron-scripts` directory. Here's an example if you're using
|
||||||
`docker-compose.yml`:
|
`docker-compose.yml`:
|
||||||
|
|
||||||
```yml
|
```yml
|
||||||
|
@ -110,7 +112,7 @@ in addition to the default `cron.php` task. To add your custom tasks, follow the
|
||||||
- ./my-scripts/do-something.sh:/cron-scripts/do-something.sh:ro
|
- ./my-scripts/do-something.sh:/cron-scripts/do-something.sh:ro
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Recreate the container. Your script will now execute in the Nextcloud container at a regular
|
3. Recreate the container. Your script will now execute in the Nextcloud container at a regular
|
||||||
interval.
|
interval.
|
||||||
|
|
||||||
### Notes
|
### Notes
|
||||||
|
|
|
@ -11,13 +11,22 @@ if [[ -n "$NEXTCLOUD_EXEC_USER" ]]; then
|
||||||
exec_user="--user $NEXTCLOUD_EXEC_USER"
|
exec_user="--user $NEXTCLOUD_EXEC_USER"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Obtain the ID of the container. We do this each iteration since the Nextcloud container may be
|
||||||
|
# recreated while the cron container is still running. We will need to check for a new container ID
|
||||||
|
# each time.
|
||||||
|
containerId="$(/find-container.sh)"
|
||||||
|
if [[ -z "$containerId" ]]; then
|
||||||
|
echo "ERROR: Unable to find the Nextcloud container"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Loop through all shell scripts and execute the contents of those scripts in the Nextcloud
|
# Loop through all shell scripts and execute the contents of those scripts in the Nextcloud
|
||||||
# container. It's done this way so that the user may mount more scripts to be executed in addition
|
# container. It's done this way so that the user may mount more scripts to be executed in addition
|
||||||
# to the default ones.
|
# to the default ones.
|
||||||
cd /cron-scripts
|
cd /cron-scripts
|
||||||
for script in *.sh; do
|
for script in *.sh; do
|
||||||
echo "> Running Script: $script"
|
echo "> Running Script: $script"
|
||||||
docker exec $exec_user -i "$1" bash < $script
|
docker exec $exec_user -i "$containerId" bash < $script
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "> Done"
|
echo "> Done"
|
||||||
|
|
|
@ -6,25 +6,7 @@ if [[ -z "$NEXTCLOUD_CONTAINER_NAME" ]]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ ! -z "$NEXTCLOUD_PROJECT_NAME" ]]; then
|
echo "*/$NEXTCLOUD_CRON_MINUTE_INTERVAL * * * * /cron-tasks.sh" \
|
||||||
containerName="${NEXTCLOUD_PROJECT_NAME}_"
|
|
||||||
else
|
|
||||||
matchEnd=","
|
|
||||||
fi
|
|
||||||
|
|
||||||
containerName="${containerName}${NEXTCLOUD_CONTAINER_NAME}"
|
|
||||||
|
|
||||||
# Get the ID of the container so we can exec something in it later
|
|
||||||
export containerId=$(/find-container.sh "$containerName" "$matchEnd")
|
|
||||||
|
|
||||||
if [[ -z "$containerId" ]]; then
|
|
||||||
echo "ERROR: Unable to find the Nextcloud container"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "$containerId" > /tmp/containerId
|
|
||||||
|
|
||||||
echo "*/$NEXTCLOUD_CRON_MINUTE_INTERVAL * * * * /cron-tasks.sh $containerId" \
|
|
||||||
> /var/spool/cron/crontabs/root
|
> /var/spool/cron/crontabs/root
|
||||||
|
|
||||||
echo "Starting crond"
|
echo "Starting crond"
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
containerName="$1"
|
if [[ ! -z "$NEXTCLOUD_PROJECT_NAME" ]]; then
|
||||||
matchEnd="$2"
|
containerName="${NEXTCLOUD_PROJECT_NAME}_"
|
||||||
|
else
|
||||||
|
matchEnd=","
|
||||||
|
fi
|
||||||
|
|
||||||
|
containerName="${containerName}${NEXTCLOUD_CONTAINER_NAME}"
|
||||||
|
|
||||||
|
# Get the ID of the container so we can exec something in it later
|
||||||
docker ps --format '{{.Names}},{{.ID}}' | \
|
docker ps --format '{{.Names}},{{.ID}}' | \
|
||||||
egrep "^${containerName}${matchEnd}" | \
|
egrep "^${containerName}${matchEnd}" | \
|
||||||
awk '{split($0,a,","); print a[2]}'
|
awk '{split($0,a,","); print a[2]}'
|
||||||
|
|
|
@ -5,4 +5,4 @@ set -x
|
||||||
ps -o comm | grep crond || exit 1
|
ps -o comm | grep crond || exit 1
|
||||||
|
|
||||||
# Make sure the target container is still running/available
|
# Make sure the target container is still running/available
|
||||||
docker inspect -f '{{.State.Running}}' "$(cat /tmp/containerId)" | grep true || exit 1
|
docker inspect -f '{{.State.Running}}' "$(/find-container.sh)" | grep true || exit 1
|
||||||
|
|
Loading…
Reference in a new issue