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
28
README.md
28
README.md
|
@ -53,16 +53,16 @@ entirely.
|
|||
the service, if `NEXTCLOUD_PROJECT_NAME` is specified).
|
||||
|
||||
* `NEXTCLOUD_PROJECT_NAME`<br>
|
||||
The name of the project if you're using Docker Compose. The name of
|
||||
the project, by default, is the name of the context directory you ran your `docker-compose.yml`
|
||||
from. This helps to build a "hint" used to identify the Nextcloud container by name. The hint is
|
||||
built as:
|
||||
The name of the project if you're using Docker Compose. The name of the project, by default, is
|
||||
the name of the context directory you ran your `docker-compose.yml` from. This helps to build a
|
||||
"hint" used to identify the Nextcloud container by name. The hint is built as:
|
||||
|
||||
${NEXTCLOUD_PROJECT_NAME}_${NEXTCLOUD_CONTAINER_NAME}
|
||||
```txt
|
||||
${NEXTCLOUD_PROJECT_NAME}_${NEXTCLOUD_CONTAINER_NAME}
|
||||
```
|
||||
|
||||
* `NEXTCLOUD_CRON_MINUTE_INTERVAL`<br>
|
||||
The interval, in minutes, of how often the cron task
|
||||
executes. The default is 15 minutes.
|
||||
The interval, in minutes, of how often the cron task executes. The default is 15 minutes.
|
||||
|
||||
* `NEXTCLOUD_EXEC_USER`<br>
|
||||
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.
|
||||
|
||||
1. The `crond` process must be running.
|
||||
2. The Nextcloud container must be available and running. One important note here: When this
|
||||
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
|
||||
is no longer valid, the health check would fail.
|
||||
2. The Nextcloud container must be available and running.
|
||||
|
||||
Because the Nextcloud container can be restarted while the the cronjob container is running, its
|
||||
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
|
||||
|
||||
|
@ -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
|
||||
```
|
||||
|
||||
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`:
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
### Notes
|
||||
|
|
|
@ -11,13 +11,22 @@ if [[ -n "$NEXTCLOUD_EXEC_USER" ]]; then
|
|||
exec_user="--user $NEXTCLOUD_EXEC_USER"
|
||||
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
|
||||
# container. It's done this way so that the user may mount more scripts to be executed in addition
|
||||
# to the default ones.
|
||||
cd /cron-scripts
|
||||
for script in *.sh; do
|
||||
echo "> Running Script: $script"
|
||||
docker exec $exec_user -i "$1" bash < $script
|
||||
docker exec $exec_user -i "$containerId" bash < $script
|
||||
done
|
||||
|
||||
echo "> Done"
|
||||
|
|
|
@ -6,25 +6,7 @@ if [[ -z "$NEXTCLOUD_CONTAINER_NAME" ]]; then
|
|||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -z "$NEXTCLOUD_PROJECT_NAME" ]]; then
|
||||
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" \
|
||||
echo "*/$NEXTCLOUD_CRON_MINUTE_INTERVAL * * * * /cron-tasks.sh" \
|
||||
> /var/spool/cron/crontabs/root
|
||||
|
||||
echo "Starting crond"
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
containerName="$1"
|
||||
matchEnd="$2"
|
||||
if [[ ! -z "$NEXTCLOUD_PROJECT_NAME" ]]; then
|
||||
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}}' | \
|
||||
egrep "^${containerName}${matchEnd}" | \
|
||||
awk '{split($0,a,","); print a[2]}'
|
||||
|
|
|
@ -5,4 +5,4 @@ set -x
|
|||
ps -o comm | grep crond || exit 1
|
||||
|
||||
# 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