diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 71347e3..cfc92ed 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,7 +2,7 @@ name: tests on: pull_request: push: - branches: [ master, main ] + branches: [ main, 20220529_auto_install ] schedule: - cron: '01 07 * * *' @@ -39,8 +39,7 @@ jobs: - uses: actions/checkout@v2 - name: Environment setup run: | - brew install bats-core mkcert - mkcert -install + brew install bats-core - name: Use ddev stable if: matrix.ddev_version == 'stable' @@ -54,13 +53,6 @@ jobs: if: matrix.ddev_version == 'HEAD' run: brew install --HEAD drud/ddev/ddev - - name: Use ddev PR - if: matrix.ddev_version == 'PR' - run: | - curl -sSL -o ddev_linux.zip ${NIGHTLY_DDEV_PR_URL} - unzip ddev_linux.zip - mv ddev /usr/local/bin/ddev && chmod +x /usr/local/bin/ddev - - name: Download docker images run: | mkdir junk && pushd junk && ddev config --auto && ddev debug download-images >/dev/null diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a09c56d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea diff --git a/README.md b/README.md index 35e8f97..3bbae09 100644 --- a/README.md +++ b/README.md @@ -1,59 +1,53 @@ -[![tests](https://github.com/drud/ddev-ddev-cron/actions/workflows/tests.yml/badge.svg)](https://github.com/drud/ddev-ddev-cron/actions/workflows/tests.yml) ![project is maintained](https://img.shields.io/maintenance/yes/2022.svg) +[![tests](https://github.com/tyler36/ddev-cron/actions/workflows/tests.yml/badge.svg)](https://github.com/tyler36/ddev-cron/actions/workflows/tests.yml) ![project is maintained](https://img.shields.io/maintenance/yes/2022.svg) # DDEV-CRON - - [Intro](#intro) - [Getting started](#getting-started) - [Implementation](#implementation) ## Intro -This extension helps to execute a command based on a schedule. +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 +* Installs and runs the cron service inside the web container +* Adds a sample cron configuration that adds to a file every minute. + +**Warning** +If you already have a `.ddev/web-build/Dockerfile` in your project, you'll want to adapt it with +the one provided here, and `ddev get` will override the one you have. -It does this by a creating a new crontab job at startup and executing it at the specified time. This extension is designed to be a generic implentation. See [Running TYPO3 Cron inside the web container](https://github.com/drud/ddev-contrib/tree/master/recipes/cronjob) for a specific example of a manual setup. ## Getting started -- Install the service +- Install the add-on ```shell ddev get tyler36/cron ``` -- Update `./.ddev/config.cron.yml` with your requirments. -- Restart DDEV +- 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 the demonstration lines to your `.ddev/config.yaml`. +- `ddev restart` -```shell -ddev restart -``` ## Implementation -The `config.cron.yml` is a simple implentation of cron within the DDEV web container. - -It has 3 main parts: - -- Install the cron package -- Write a crontab file -- Update the permissions and start the cron service. +The provided `web-build/Dockerfile` and `web-build/cron.conf` configure the traditional cron daemon to run inside the web container. -```yml -# Install required packages -webimage_extra_packages: [cron] +The `config.cron.yaml` is a simple implentation of cron within the DDEV web container. It writes a crontab file to configure the cron daemon. +```yaml hooks: post-start: - # This line creates a job, ddev-cron-time, and configures it to run every minute + # This line creates a cron job, ddev-cron-time, and configures it to run every minute - exec: echo '*/1 * * * * root date | tee -a /var/www/html/time.log' | sudo tee -a /etc/cron.d/ddev-cron-time - # This line sets permissions ands starts the cron service - - exec: sudo chmod 0600 /etc/cron.d/ddev-cron-time && sudo service cron start ``` The default file configures a job (`ddev-cron-time`) 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. +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. **Contributed and maintained by [@tyler36](https://github.com/tyler36) based on the original [Running TYPO3 Cron inside the web container](https://github.com/drud/ddev-contrib/tree/master/recipes/cronjob) by [@thomaskieslich](https://github.com/thomaskieslich)** -**Originally Contributed by [@thomaskieslich](https://github.com/thomaskieslich) in ) +**Originally Contributed by [@thomaskieslich](https://github.com/thomaskieslich) in )** diff --git a/config.cron.yaml b/config.cron.yaml new file mode 100644 index 0000000..8d1cab2 --- /dev/null +++ b/config.cron.yaml @@ -0,0 +1,6 @@ +hooks: + # This will be executed at the whenever DDEV starts + post-start: + # This line creates a job, ddev-cron-time, and configures it to run every minute + # You can just `ls -l time.log` or `tail time.log` to see it happening. + - exec: echo '* * * * * root date | tee -a /var/www/html/time.log' | sudo tee -a /etc/cron.d/ddev-cron-time diff --git a/config.cron.yml b/config.cron.yml deleted file mode 100644 index 6736433..0000000 --- a/config.cron.yml +++ /dev/null @@ -1,10 +0,0 @@ -# Install required packages -webimage_extra_packages: [cron] - -hooks: - # This will be executed at the whenever DDEV starts - post-start: - # This line creates a job, ddev-cron-time, and configures it to run every minute - - exec: echo '*/1 * * * * root date | tee -a /var/www/html/time.log' | sudo tee -a /etc/cron.d/ddev-cron-time - # This line sets permissions ands starts the cron service - - exec: sudo chmod 0600 /etc/cron.d/ddev-cron-time && sudo service cron start diff --git a/install.yaml b/install.yaml index 04d974d..888801e 100644 --- a/install.yaml +++ b/install.yaml @@ -9,9 +9,9 @@ pre_install_actions: # list of files and directories listed that are copied into project .ddev directory project_files: -- config.cron.yml -# - extra_files/ -# - somefile.sh +- config.cron.yaml +- web-build + # 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 ea5707b..d6012ec 100644 --- a/tests/test.bats +++ b/tests/test.bats @@ -9,7 +9,8 @@ setup() { ddev delete -Oy ${PROJNAME} || true cd "${TESTDIR}" ddev config --project-name=${PROJNAME} - ddev start + ddev start -y + echo "# ddev started at $(date)" >&3 } teardown() { @@ -26,8 +27,11 @@ teardown() { ddev get ${DIR} ddev restart - # ASSERT: Service has started - ddev exec service cron status | grep "cron is running." + 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 "install from release" { @@ -37,6 +41,9 @@ teardown() { ddev get tyler36/ddev-cron ddev restart - # ASSERT: Service has started - ddev exec service cron status | grep "cron is running." + 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 } diff --git a/web-build/Dockerfile b/web-build/Dockerfile new file mode 100644 index 0000000..ec909e4 --- /dev/null +++ b/web-build/Dockerfile @@ -0,0 +1,6 @@ +ARG BASE_IMAGE +FROM $BASE_IMAGE +# 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 +ADD cron.conf /etc/supervisor/conf.d diff --git a/web-build/cron.conf b/web-build/cron.conf new file mode 100644 index 0000000..86f02b6 --- /dev/null +++ b/web-build/cron.conf @@ -0,0 +1,4 @@ +[program:cron] +command=sudo /usr/sbin/cron -f +autorestart=true +startretries=10