From f66afd2735eb19f2cd17c422775bc8fae6e95e42 Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:03:27 +0900 Subject: [PATCH 01/18] Move to Dockerfile installation --- config.cron.yaml | 11 ----------- install.yaml | 4 ++-- web-build/Dockerfile.ddev-cron | 23 +++++++++++++---------- web-build/cron.conf | 8 ++++++++ web-build/custom.cron | 2 ++ 5 files changed, 25 insertions(+), 23 deletions(-) delete mode 100644 config.cron.yaml create mode 100644 web-build/cron.conf create mode 100644 web-build/custom.cron diff --git a/config.cron.yaml b/config.cron.yaml deleted file mode 100644 index 83c9dc7..0000000 --- a/config.cron.yaml +++ /dev/null @@ -1,11 +0,0 @@ -#ddev-generated -# Remove the line above if you don't want this file to be overwritten when you run -# ddev get ddev/ddev-cron -hooks: - post-start: - # This adds an every-minute cronjob for your user; it runs "date" and appends that - # to the "time.log" in your project root. - # You can just `ls -l time.log` or `tail -f time.log` to see it happening. - # The crontab can have more than one line for multiple jobs. - # `ddev exec crontab -l` will show you the current crontab configuration - - exec: printf "SHELL=/bin/bash\n* * * * * date | tee -a /var/www/html/time.log\n" | crontab diff --git a/install.yaml b/install.yaml index bb4df5f..bc0aaa7 100644 --- a/install.yaml +++ b/install.yaml @@ -9,12 +9,12 @@ pre_install_actions: # list of files and directories listed that are copied into project .ddev directory project_files: -- config.cron.yaml - web-build/Dockerfile.ddev-cron +- web-build/cron.conf +- web-build/custom.cron # List of files and directories that are copied into the global .ddev directory global_files: post_install_actions: - diff --git a/web-build/Dockerfile.ddev-cron b/web-build/Dockerfile.ddev-cron index eb6889d..83d4c3d 100644 --- a/web-build/Dockerfile.ddev-cron +++ b/web-build/Dockerfile.ddev-cron @@ -1,15 +1,18 @@ #ddev-generated # Install cron package; this can be done in webimage_extra_packages, but put it here for now. RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-confold" --no-install-recommends --no-install-suggests cron -# Tell supervisord to start cron service in cron.conf -RUN echo " \n \ -[program:cron] \n \ -command=sudo /usr/sbin/cron -f -L7 \n \ -autorestart=true \n \ -startretries=10 \n \ -stdout_logfile=/proc/self/fd/2 \n \ -stdout_logfile_maxbytes=0 \n \ -redirect_stderr=true \n \ -" > /etc/supervisor/conf.d/cron.conf + +# Add out custom config +ADD ./cron.conf /etc/supervisor/conf.d/cron.conf + # Make it so you can add to cron.d without root privileges RUN chmod 777 /etc/cron.d /var/run + +# Copy over our custom jobs +ADD ./custom.cron /etc/cron.d/custom.cron + +# Give execution rights on the cron job +RUN chmod 0644 /etc/cron.d/custom.cron + +# Apply cron job +RUN crontab /etc/cron.d/custom.cron diff --git a/web-build/cron.conf b/web-build/cron.conf new file mode 100644 index 0000000..0edde38 --- /dev/null +++ b/web-build/cron.conf @@ -0,0 +1,8 @@ +#ddev-generated +[program:cron] +command=sudo /usr/sbin/cron -f -L7 +autorestart=true +startretries=10 +stdout_logfile=/proc/self/fd/2 +stdout_logfile_maxbytes=0 +redirect_stderr=true diff --git a/web-build/custom.cron b/web-build/custom.cron new file mode 100644 index 0000000..5702ee4 --- /dev/null +++ b/web-build/custom.cron @@ -0,0 +1,2 @@ +#ddev-generated +* * * * * date | tee -a /var/www/html/time.log From 2c3ceaef6cf7525e752898489588527aa69e65ea Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:03:28 +0900 Subject: [PATCH 02/18] fix README typos --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a8dd6b9..d741e6d 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,22 @@ # DDEV-CRON -- [Intro](#intro) +- [Introduction](#introduction) - [Getting started](#getting-started) - [Implementation](#implementation) - [Examples](#examples) -## Intro +## Introduction This DDEV add-on helps to execute a command in the web container based on a cron schedule. Cron is a classic Linux/Unix service with a well-known configuration syntax. -The add-on +The add-on: - Installs and runs the cron service inside the web container - Adds a sample cron configuration that adds to a file every minute. - Required DDEV v1.19.3 or higher. -*This extension is designed to be a generic implentation. See [Running TYPO3 Cron inside the web container](https://github.com/ddev/ddev-contrib/tree/master/recipes/cronjob) for a specific example of a manual setup.* +*This extension is designed to be a generic implementation. See [Running TYPO3 Cron inside the web container](https://github.com/ddev/ddev-contrib/tree/master/recipes/cronjob) for a specific example of a manual setup.* ## Getting started @@ -43,7 +43,7 @@ hooks: ``` The default file configures a job to write the date to a log file `time.log` every minute. -It is a simple arbitary example to show the service is working, and remind the user to change it to something more appropriate. You can add additional files into /etc/cron.d, or add additional lines to this one. +It is a simple arbitrary example to show the service is working, and remind the user to change it to something more appropriate. You can add additional files into /etc/cron.d, or add additional lines to this one. - If you need help figuring out the syntax of a cron job, see [crontab guru](https://crontab.guru/). - For the usage of `crontab` see [crontab man page](https://manpages.debian.org/buster/cron/crontab.1.en.html). From bd3e3e86b9b057dfe95ab646e70221f9f0dd332d Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:03:28 +0900 Subject: [PATCH 03/18] Update readme --- README.md | 65 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index d741e6d..f1937b9 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,9 @@ - [Getting started](#getting-started) - [Implementation](#implementation) - [Examples](#examples) + - [TYPO3 scheduler](#typo3-scheduler) + - [Drupal cron](#drupal-cron) + - [Laravel cron](#laravel-cron) ## Introduction @@ -14,65 +17,65 @@ This DDEV add-on helps to execute a command in the web container based on a cron The add-on: - Installs and runs the cron service inside the web container -- Adds a sample cron configuration that adds to a file every minute. +- Adds an example job that writes out the current time. - Required DDEV v1.19.3 or higher. *This extension is designed to be a generic implementation. See [Running TYPO3 Cron inside the web container](https://github.com/ddev/ddev-contrib/tree/master/recipes/cronjob) for a specific example of a manual setup.* ## Getting started -- Install the add-on with `ddev get ddev/ddev-cron` -- Update the provided `.ddev/config.cron.yaml` as you see fit with your expected cron jobs (and remove the demonstration line). You can also just add those demonstration lines to your `.ddev/config.yaml` and delete the `.ddev/config.cron.yaml`. -- `ddev restart` +- Install the DDEV cron add-on: -## Implementation + ```shell + ddev get ddev/ddev-browsersync + ``` -The provided `web-build/Dockerfile.ddev-cron` and `web-build/cron.conf` configure the traditional cron daemon to run inside the web container. +- Update `./.ddev/web-build/custom.cron` with your required commands. + - Remove `#ddev-generated` to prevent DDEV from overriding the file. +- Custom the cron service, if required, by updating `./.ddev/web-build/cron.conf` + - Remove `#ddev-generated` to prevent DDEV from overriding the file. +- Restart DDEV to apply any changes: -The `config.cron.yaml` is a simple setup of a trivial cron job within the DDEV web container. It writes a crontab file to configure the cron daemon. + ```shell + ddev restart + ``` -```yaml -hooks: - post-start: - # This adds an every-minute cronjob for your user; it runs "date" and appends that - # to the "time.log" in your project root. - # You can just `ls -l time.log` or `tail -f time.log` to see it happening. - # The crontab can have more than one line for multiple jobs. - # `ddev exec crontab -l` will show you the current crontab configuration - - exec: printf "SHELL=/bin/bash\n* * * * * date >> /var/www/html/time.log\n" | crontab -``` +## Implementation -The default file configures a job to write the date to a log file `time.log` every minute. -It is a simple arbitrary example to show the service is working, and remind the user to change it to something more appropriate. You can add additional files into /etc/cron.d, or add additional lines to this one. +Out of the box, this add-on writes the date to a log file `time.log` every minute. +This serves as an example implementation, provides proof the service is working, and is the basis for tests. - If you need help figuring out the syntax of a cron job, see [crontab guru](https://crontab.guru/). - For the usage of `crontab` see [crontab man page](https://manpages.debian.org/buster/cron/crontab.1.en.html). -- You can experiment with the `crontab` command inside the container by `ddev ssh` and then `crontab -e` for example, or use `ddev exec crontab -e`. +- Experiment with the `crontab` command inside the container by `ddev ssh` and then `crontab -e` for example, or use `ddev exec crontab -e`. - If you want the cron to run on your local time instead of UTC, make sure to set `timezone` in your `.ddev/config.yaml`. - Make sure that when you have tried manually executing the command you want to run inside the container and that it gets the expected results. - If you are running a CMS command that requires access to the database, set the environment variable `IS_DDEV_PROJECT=true` ## Examples -**TYPO3 scheduler**: A cron to add on to the example and then run the TYPO3 scheduler every minute might be: +### TYPO3 scheduler -```yaml - - exec: printf "SHELL=/bin/bash\n* * * * * date |& tee -a /var/www/html/time.log\n* * * * * IS_DDEV_PROJECT=true /var/www/html/vendor/bin/typo3 scheduler:run -vv |& tee -a /var/www/html/scheduler-log.txt\n" | crontab +A cron to add on to the example and then run the TYPO3 scheduler every minute might be: +```cron + * * * * * cd /var/www/html && IS_DDEV_PROJECT=true vendor/bin/typo3 scheduler:run -vv |& tee -a /var/www/html/scheduler-log.txt ``` -See the results of this with `ddev exec crontab -l`: +### Drupal cron +A cron to run drupal's cron every 10 minutes via drush might be: + +```cron +*/10 * * * * IS_DDEV_PROJECT=true DDEV_PHP_VERSION=8.0 /var/www/html/vendor/bin/drush cron -v |& tee -a /var/www/html/cron-log.txt ``` -SHELL=/bin/bash -* * * * * date |& tee -a /var/www/html/time.log -* * * * * cd /var/www/html && IS_DDEV_PROJECT=true vendor/bin/typo3 scheduler:run -vv |& tee -a /var/www/html/scheduler-log.txt -``` -**Drupal cron**: A cron to run drupal's cron every 10 minutes via drush might be: +### Laravel cron + +A cron to run the Laravel scheduler every minute would be: -```yaml - - exec: printf "SHELL=/bin/bash\n*/10 * * * * IS_DDEV_PROJECT=true DDEV_PHP_VERSION=8.0 /var/www/html/vendor/bin/drush cron -v |& tee -a /var/www/html/cron-log.txt\n" | crontab +```cron +* * * * * cd /var/www/html && IS_DDEV_PROJECT=true php artisan schedule:run >> /dev/null 2>&1 ``` **Contributed and maintained by [@tyler36](https://github.com/tyler36) based on the original [Running TYPO3 Cron inside the web container](https://github.com/ddev/ddev-contrib/tree/master/recipes/cronjob) by [@thomaskieslich](https://github.com/thomaskieslich)** From 602a7b94f0ec34969c60617ae777b29e0dc9bc07 Mon Sep 17 00:00:00 2001 From: tyler36 Date: Fri, 25 Aug 2023 10:01:24 +0900 Subject: [PATCH 04/18] Update README.md Co-authored-by: Randy Fay --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f1937b9..e956e16 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ The add-on: - Install the DDEV cron add-on: ```shell - ddev get ddev/ddev-browsersync + ddev get ddev/ddev-cron ``` - Update `./.ddev/web-build/custom.cron` with your required commands. From 1757c98c75671af3cd6c97fe116832896011569b Mon Sep 17 00:00:00 2001 From: tyler36 Date: Fri, 25 Aug 2023 10:01:49 +0900 Subject: [PATCH 05/18] Update README.md Co-authored-by: Randy Fay --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e956e16..e07fddd 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ The add-on: - Update `./.ddev/web-build/custom.cron` with your required commands. - Remove `#ddev-generated` to prevent DDEV from overriding the file. -- Custom the cron service, if required, by updating `./.ddev/web-build/cron.conf` +- Customize the cron service, if required, by updating `./.ddev/web-build/cron.conf` - Remove `#ddev-generated` to prevent DDEV from overriding the file. - Restart DDEV to apply any changes: From 85e38e4ae0eac5057b7b9054c3bba4e44dc452c2 Mon Sep 17 00:00:00 2001 From: tyler36 Date: Fri, 25 Aug 2023 10:02:56 +0900 Subject: [PATCH 06/18] Update README.md Co-authored-by: Randy Fay --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e07fddd..1b9563f 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ The add-on: - Remove `#ddev-generated` to prevent DDEV from overriding the file. - Customize the cron service, if required, by updating `./.ddev/web-build/cron.conf` - Remove `#ddev-generated` to prevent DDEV from overriding the file. -- Restart DDEV to apply any changes: +- Restart DDEV to apply changes: ```shell ddev restart From 092c9b3707dc74b0ff85a3f4ee64984aed3b24e3 Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Fri, 25 Aug 2023 02:06:15 +0000 Subject: [PATCH 07/18] Use `COPY` command because it's preferred https://www.geeksforgeeks.org/difference-between-the-copy-and-add-commands-in-a-dockerfile/ --- web-build/Dockerfile.ddev-cron | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/web-build/Dockerfile.ddev-cron b/web-build/Dockerfile.ddev-cron index 83d4c3d..11b03ba 100644 --- a/web-build/Dockerfile.ddev-cron +++ b/web-build/Dockerfile.ddev-cron @@ -2,14 +2,14 @@ # Install cron package; this can be done in webimage_extra_packages, but put it here for now. RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::="--force-confold" --no-install-recommends --no-install-suggests cron -# Add out custom config -ADD ./cron.conf /etc/supervisor/conf.d/cron.conf +# Copy our custom config +COPY ./cron.conf /etc/supervisor/conf.d/cron.conf # Make it so you can add to cron.d without root privileges RUN chmod 777 /etc/cron.d /var/run # Copy over our custom jobs -ADD ./custom.cron /etc/cron.d/custom.cron +COPY ./custom.cron /etc/cron.d/custom.cron # Give execution rights on the cron job RUN chmod 0644 /etc/cron.d/custom.cron From 6e871db92e2868021f9dfa544fc62df4963d056a Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Fri, 25 Aug 2023 02:07:19 +0000 Subject: [PATCH 08/18] Dynamically add `.cron` files --- web-build/Dockerfile.ddev-cron | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/web-build/Dockerfile.ddev-cron b/web-build/Dockerfile.ddev-cron index 11b03ba..de19400 100644 --- a/web-build/Dockerfile.ddev-cron +++ b/web-build/Dockerfile.ddev-cron @@ -9,10 +9,10 @@ COPY ./cron.conf /etc/supervisor/conf.d/cron.conf RUN chmod 777 /etc/cron.d /var/run # Copy over our custom jobs -COPY ./custom.cron /etc/cron.d/custom.cron +COPY ./*.cron /etc/cron.d/*.cron -# Give execution rights on the cron job -RUN chmod 0644 /etc/cron.d/custom.cron +# Give execution rights on the cron jobs +RUN chmod -f 0644 /etc/cron.d/*.cron || true -# Apply cron job -RUN crontab /etc/cron.d/custom.cron +# Apply cron jobs +RUN crontab /etc/cron.d/*.cron || true From 4ca42b3b35527d611e27539db8ac11a39519ff98 Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Fri, 25 Aug 2023 07:26:41 +0000 Subject: [PATCH 09/18] Concatenate project cron jobs --- README.md | 64 ++++++++++++++++---- install.yaml | 2 +- tests/test.bats | 19 ++++++ web-build/Dockerfile.ddev-cron | 4 +- web-build/{custom.cron => time.cron.example} | 0 5 files changed, 75 insertions(+), 14 deletions(-) rename web-build/{custom.cron => time.cron.example} (100%) diff --git a/README.md b/README.md index 1b9563f..8132e1f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,11 @@ - [Introduction](#introduction) - [Getting started](#getting-started) - [Implementation](#implementation) + - [./ddev/web-build/cron.conf](#ddevweb-buildcronconf) + - [\*.cron](#cron) +- [Useful sites and debugging](#useful-sites-and-debugging) - [Examples](#examples) + - [Logging current time](#logging-current-time) - [TYPO3 scheduler](#typo3-scheduler) - [Drupal cron](#drupal-cron) - [Laravel cron](#laravel-cron) @@ -30,10 +34,9 @@ The add-on: ddev get ddev/ddev-cron ``` -- Update `./.ddev/web-build/custom.cron` with your required commands. - - Remove `#ddev-generated` to prevent DDEV from overriding the file. - Customize the cron service, if required, by updating `./.ddev/web-build/cron.conf` - Remove `#ddev-generated` to prevent DDEV from overriding the file. +- Add at least one `./ddev/web-build/*.cron` file. This will be automatically added to crontab on startup. See [Implementation](#implementation) - Restart DDEV to apply changes: ```shell @@ -42,21 +45,58 @@ The add-on: ## Implementation -Out of the box, this add-on writes the date to a log file `time.log` every minute. -This serves as an example implementation, provides proof the service is working, and is the basis for tests. +This extension does the follow: + +- Adds required cron service to the web container. +- Configures the cron service using `./ddev/web-build/cron.conf`. +- Adds all `./ddev/web-build/*.cron` files to crontab scheduler. + +### ./ddev/web-build/cron.conf + +This addon provides basic configuration for the cron service out of the box. + +To change the configuration for the cron service, update `./ddev/web-build/cron.conf`: + +- Remove `#ddev-generated` to prevent DDEV from overriding the file. +- Make any required changes. + +### *.cron + +This addon uses `*.cron` files to populate crontab. This allows projects to track and manage cron jobs via git. -- If you need help figuring out the syntax of a cron job, see [crontab guru](https://crontab.guru/). -- For the usage of `crontab` see [crontab man page](https://manpages.debian.org/buster/cron/crontab.1.en.html). -- Experiment with the `crontab` command inside the container by `ddev ssh` and then `crontab -e` for example, or use `ddev exec crontab -e`. +On `ddev start`, all `./ddev/web-build/*.cron` files are: + +- Copied into the `/etc/cron.d`. +- Have their permissions updated. +- Added to crontab. + +See `.ddev/web-build/time.cron.example` and [Examples](#examples) section below for specific example. + +## Useful sites and debugging + +- [crontab guru](https://crontab.guru/) is a helpful for generating cron schedule expressions. - If you want the cron to run on your local time instead of UTC, make sure to set `timezone` in your `.ddev/config.yaml`. -- Make sure that when you have tried manually executing the command you want to run inside the container and that it gets the expected results. +- To help debug, connect to the web container session (`ddev ssh`) and manually run the commands to confirm expected results. - If you are running a CMS command that requires access to the database, set the environment variable `IS_DDEV_PROJECT=true` ## Examples +The following examples are provide as guides. +PRs are welcome for changes and updates for current best practices for specific frameworks. + +### Logging current time + +This addon provides an example that can check if the cron service is running. +Every minute, it writes the current time (UTC timezone) to `./time.log`. + +- Rename `./ddev/web-build/time.cron.example` to `./ddev/web-build/time.cron` +- Restart the DDEV project to start the time example. +- After at least a minute, you should see `./time.log` containing the web container's current time. + ### TYPO3 scheduler -A cron to add on to the example and then run the TYPO3 scheduler every minute might be: +- Create a `./.ddev/web-build/typo3.cron` file +- Add the following code to run the typo3 scheduler every minute and write to a log file. ```cron * * * * * cd /var/www/html && IS_DDEV_PROJECT=true vendor/bin/typo3 scheduler:run -vv |& tee -a /var/www/html/scheduler-log.txt @@ -64,7 +104,8 @@ A cron to add on to the example and then run the TYPO3 scheduler every minute mi ### Drupal cron -A cron to run drupal's cron every 10 minutes via drush might be: +- Create a `./.ddev/web-build/drupal.cron` file +- Add the following code to run the drupal scheduler every 10 minute and write to a log file. ```cron */10 * * * * IS_DDEV_PROJECT=true DDEV_PHP_VERSION=8.0 /var/www/html/vendor/bin/drush cron -v |& tee -a /var/www/html/cron-log.txt @@ -72,7 +113,8 @@ A cron to run drupal's cron every 10 minutes via drush might be: ### Laravel cron -A cron to run the Laravel scheduler every minute would be: +- Create a `./.ddev/web-build/drupal.cron` file +- Add the following code to run the drupal scheduler minute. ```cron * * * * * cd /var/www/html && IS_DDEV_PROJECT=true php artisan schedule:run >> /dev/null 2>&1 diff --git a/install.yaml b/install.yaml index bc0aaa7..584fc51 100644 --- a/install.yaml +++ b/install.yaml @@ -11,7 +11,7 @@ pre_install_actions: project_files: - web-build/Dockerfile.ddev-cron - web-build/cron.conf -- web-build/custom.cron +- web-build/time.cron.example # List of files and directories that are copied into the global .ddev directory global_files: diff --git a/tests/test.bats b/tests/test.bats index 55c8630..60f66d0 100644 --- a/tests/test.bats +++ b/tests/test.bats @@ -25,8 +25,11 @@ teardown() { cd ${TESTDIR} echo "# ddev get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3 ddev get ${DIR} + # Set the example cron job as an actual cron job. + mv ./.ddev/web-build/time.cron.example ./.ddev/web-build/time.cron ddev restart + # The example runs every minute so we should wait at least the length. sleep 61 # Make sure cron process is running ddev exec 'sudo killall -0 cron' @@ -39,11 +42,27 @@ teardown() { cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 ) echo "# ddev get ddev/ddev-cron with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3 ddev get ddev/ddev-cron + # Set the example cron job as an actual cron job. + mv ./.ddev/web-build/time.cron.example ./.ddev/web-build/time.cron ddev restart + # The example runs every minute so we should wait at least the length. sleep 61 # Make sure cron process is running ddev exec 'sudo killall -0 cron' # ASSERT: Make sure time.log got a line written to it. grep UTC time.log } + +@test "services work when no valid jobs are present" { + set -eu -o pipefail + cd ${TESTDIR} + echo "# ddev get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3 + ddev get ${DIR} + ddev restart + + # We should wait at least one cycle. + sleep 61 + # Make sure cron process is running + ddev exec 'sudo killall -0 cron' +} diff --git a/web-build/Dockerfile.ddev-cron b/web-build/Dockerfile.ddev-cron index de19400..254882e 100644 --- a/web-build/Dockerfile.ddev-cron +++ b/web-build/Dockerfile.ddev-cron @@ -14,5 +14,5 @@ COPY ./*.cron /etc/cron.d/*.cron # Give execution rights on the cron jobs RUN chmod -f 0644 /etc/cron.d/*.cron || true -# Apply cron jobs -RUN crontab /etc/cron.d/*.cron || true +# Concatenate files +RUN { cat /etc/cron.d/*.cron; } | crontab - diff --git a/web-build/custom.cron b/web-build/time.cron.example similarity index 100% rename from web-build/custom.cron rename to web-build/time.cron.example From 256ff7bd46c0aa056e6562dd955fc166d54a74e5 Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Fri, 25 Aug 2023 08:52:23 +0000 Subject: [PATCH 10/18] attempt to add cron jobs to current user --- README.md | 2 ++ tests/test.bats | 6 ++++++ web-build/Dockerfile.ddev-cron | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8132e1f..237034e 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,8 @@ See `.ddev/web-build/time.cron.example` and [Examples](#examples) section below ## Useful sites and debugging - [crontab guru](https://crontab.guru/) is a helpful for generating cron schedule expressions. +- For `crontab` usage, see [crontab man page](https://manpages.debian.org/buster/cron/crontab.1.en.html). +- Check crontab by running `ddev exec crontab -e`. - If you want the cron to run on your local time instead of UTC, make sure to set `timezone` in your `.ddev/config.yaml`. - To help debug, connect to the web container session (`ddev ssh`) and manually run the commands to confirm expected results. - If you are running a CMS command that requires access to the database, set the environment variable `IS_DDEV_PROJECT=true` diff --git a/tests/test.bats b/tests/test.bats index 60f66d0..429c5be 100644 --- a/tests/test.bats +++ b/tests/test.bats @@ -35,6 +35,9 @@ teardown() { ddev exec 'sudo killall -0 cron' # ASSERT: Make sure time.log got a line written to it. grep UTC time.log + + # ASSERT job displays under current user's crontab + ddev exec crontab -l | grep '* * * * * date | tee -a /var/www/html/time.log' } @test "install from release" { @@ -52,6 +55,9 @@ teardown() { ddev exec 'sudo killall -0 cron' # ASSERT: Make sure time.log got a line written to it. grep UTC time.log + + # ASSERT job displays under current user's crontab + ddev exec crontab -l | grep '* * * * * date | tee -a /var/www/html/time.log' } @test "services work when no valid jobs are present" { diff --git a/web-build/Dockerfile.ddev-cron b/web-build/Dockerfile.ddev-cron index 254882e..ab0d1f0 100644 --- a/web-build/Dockerfile.ddev-cron +++ b/web-build/Dockerfile.ddev-cron @@ -15,4 +15,4 @@ COPY ./*.cron /etc/cron.d/*.cron RUN chmod -f 0644 /etc/cron.d/*.cron || true # Concatenate files -RUN { cat /etc/cron.d/*.cron; } | crontab - +RUN { cat /etc/cron.d/*.cron; } | crontab -u ${username} - From 28b766152f396af600f2d75fa633d3ff557d3b4c Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Fri, 25 Aug 2023 08:59:25 +0000 Subject: [PATCH 11/18] centralize health checks and example tests --- tests/test.bats | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/tests/test.bats b/tests/test.bats index 429c5be..b656839 100644 --- a/tests/test.bats +++ b/tests/test.bats @@ -20,6 +20,19 @@ teardown() { [ "${TESTDIR}" != "" ] && rm -rf ${TESTDIR} } +health_checks() { + # Make sure cron process is running + ddev exec 'sudo killall -0 cron' +} + +time_cron_checks() { + # ASSERT time.log was written to + grep UTC time.log + + # ASSERT job displays under current user's crontab + ddev exec crontab -l | grep '* * * * * date | tee -a /var/www/html/time.log' +} + @test "install from directory" { set -eu -o pipefail cd ${TESTDIR} @@ -31,13 +44,12 @@ teardown() { # The example runs every minute so we should wait at least the length. sleep 61 - # Make sure cron process is running - ddev exec 'sudo killall -0 cron' - # ASSERT: Make sure time.log got a line written to it. - grep UTC time.log - # ASSERT job displays under current user's crontab - ddev exec crontab -l | grep '* * * * * date | tee -a /var/www/html/time.log' + # Check service works + health_checks + + # Check example cron job works + time_cron_checks } @test "install from release" { @@ -51,13 +63,12 @@ teardown() { # The example runs every minute so we should wait at least the length. sleep 61 - # Make sure cron process is running - ddev exec 'sudo killall -0 cron' - # ASSERT: Make sure time.log got a line written to it. - grep UTC time.log - # ASSERT job displays under current user's crontab - ddev exec crontab -l | grep '* * * * * date | tee -a /var/www/html/time.log' + # Check service works + health_checks + + # Check example cron job works + time_cron_checks } @test "services work when no valid jobs are present" { @@ -69,6 +80,7 @@ teardown() { # We should wait at least one cycle. sleep 61 - # Make sure cron process is running - ddev exec 'sudo killall -0 cron' + + # Check service works + health_checks } From 6986d00c1446e0f635b5ca3afc995e1af6170e1f Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Tue, 29 Aug 2023 01:31:17 +0000 Subject: [PATCH 12/18] Fix COPY command preventing multiple jobs --- web-build/Dockerfile.ddev-cron | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web-build/Dockerfile.ddev-cron b/web-build/Dockerfile.ddev-cron index ab0d1f0..bdf3cf1 100644 --- a/web-build/Dockerfile.ddev-cron +++ b/web-build/Dockerfile.ddev-cron @@ -9,7 +9,7 @@ COPY ./cron.conf /etc/supervisor/conf.d/cron.conf RUN chmod 777 /etc/cron.d /var/run # Copy over our custom jobs -COPY ./*.cron /etc/cron.d/*.cron +COPY ./*.cron /etc/cron.d/ # Give execution rights on the cron jobs RUN chmod -f 0644 /etc/cron.d/*.cron || true From 67a48ac6d498564c54f39a1b19670dc1e5c92a6c Mon Sep 17 00:00:00 2001 From: tyler36 Date: Wed, 30 Aug 2023 09:20:39 +0900 Subject: [PATCH 13/18] Fix typo Co-authored-by: Randy Fay --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 237034e..d59dad8 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ The add-on: ## Implementation -This extension does the follow: +This extension does the following: - Adds required cron service to the web container. - Configures the cron service using `./ddev/web-build/cron.conf`. From 5aed29f027ef2bde168edb80a6dc0c7b15916fad Mon Sep 17 00:00:00 2001 From: tyler36 Date: Wed, 30 Aug 2023 09:26:52 +0900 Subject: [PATCH 14/18] Remove repeated section Co-authored-by: Randy Fay --- README.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/README.md b/README.md index d59dad8..efcca7f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ - [Introduction](#introduction) - [Getting started](#getting-started) - [Implementation](#implementation) - - [./ddev/web-build/cron.conf](#ddevweb-buildcronconf) - [\*.cron](#cron) - [Useful sites and debugging](#useful-sites-and-debugging) - [Examples](#examples) @@ -51,14 +50,6 @@ This extension does the following: - Configures the cron service using `./ddev/web-build/cron.conf`. - Adds all `./ddev/web-build/*.cron` files to crontab scheduler. -### ./ddev/web-build/cron.conf - -This addon provides basic configuration for the cron service out of the box. - -To change the configuration for the cron service, update `./ddev/web-build/cron.conf`: - -- Remove `#ddev-generated` to prevent DDEV from overriding the file. -- Make any required changes. ### *.cron From a8ee0c586e982eae7ea4fd844512373f60034071 Mon Sep 17 00:00:00 2001 From: tyler36 Date: Wed, 30 Aug 2023 09:29:52 +0900 Subject: [PATCH 15/18] Remove requirement statement Co-authored-by: Randy Fay --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index efcca7f..ccc8e1e 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,6 @@ The add-on: - Installs and runs the cron service inside the web container - Adds an example job that writes out the current time. -- Required DDEV v1.19.3 or higher. *This extension is designed to be a generic implementation. See [Running TYPO3 Cron inside the web container](https://github.com/ddev/ddev-contrib/tree/master/recipes/cronjob) for a specific example of a manual setup.* From 642fe9ab2c94cfd4ad7f2ac0d5e3c40575954c58 Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Wed, 30 Aug 2023 00:35:23 +0000 Subject: [PATCH 16/18] Use `crontab -l` in example because it is simpler --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ccc8e1e..b774893 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ See `.ddev/web-build/time.cron.example` and [Examples](#examples) section below - [crontab guru](https://crontab.guru/) is a helpful for generating cron schedule expressions. - For `crontab` usage, see [crontab man page](https://manpages.debian.org/buster/cron/crontab.1.en.html). -- Check crontab by running `ddev exec crontab -e`. +- Check crontab by running `ddev exec crontab -l`. - If you want the cron to run on your local time instead of UTC, make sure to set `timezone` in your `.ddev/config.yaml`. - To help debug, connect to the web container session (`ddev ssh`) and manually run the commands to confirm expected results. - If you are running a CMS command that requires access to the database, set the environment variable `IS_DDEV_PROJECT=true` From ebd76f6bd1fa1109986d038496eb70d07f4357f6 Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Wed, 30 Aug 2023 06:34:32 +0000 Subject: [PATCH 17/18] Remove customize instructions --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index b774893..cb6a94d 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,6 @@ The add-on: ddev get ddev/ddev-cron ``` -- Customize the cron service, if required, by updating `./.ddev/web-build/cron.conf` - - Remove `#ddev-generated` to prevent DDEV from overriding the file. - Add at least one `./ddev/web-build/*.cron` file. This will be automatically added to crontab on startup. See [Implementation](#implementation) - Restart DDEV to apply changes: From 2c2f87fdfbdd4ead8956b309e00e64816312452c Mon Sep 17 00:00:00 2001 From: tyler36 <7234392+tyler36@users.noreply.github.com> Date: Wed, 30 Aug 2023 06:36:54 +0000 Subject: [PATCH 18/18] Remove extra line --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index cb6a94d..1170636 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,6 @@ This extension does the following: - Configures the cron service using `./ddev/web-build/cron.conf`. - Adds all `./ddev/web-build/*.cron` files to crontab scheduler. - ### *.cron This addon uses `*.cron` files to populate crontab. This allows projects to track and manage cron jobs via git.