Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to Dockerfile installation #32

Merged
merged 18 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 38 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,80 @@

# DDEV-CRON <!-- omit in toc -->

- [Intro](#intro)
- [Introduction](#introduction)
- [Getting started](#getting-started)
- [Implementation](#implementation)
- [Examples](#examples)
- [TYPO3 scheduler](#typo3-scheduler)
- [Drupal cron](#drupal-cron)
- [Laravel cron](#laravel-cron)

## 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.
- Adds an example job that writes out the current time.
- Required DDEV v1.19.3 or higher.
tyler36 marked this conversation as resolved.
Show resolved Hide resolved

*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

- 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
tyler36 marked this conversation as resolved.
Show resolved Hide resolved
```

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.
tyler36 marked this conversation as resolved.
Show resolved Hide resolved
- Custom the cron service, if required, by updating `./.ddev/web-build/cron.conf`
tyler36 marked this conversation as resolved.
Show resolved Hide resolved
- Remove `#ddev-generated` to prevent DDEV from overriding the file.
tyler36 marked this conversation as resolved.
Show resolved Hide resolved
- Restart DDEV to apply any changes:
tyler36 marked this conversation as resolved.
Show resolved Hide resolved

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 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.
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:
tyler36 marked this conversation as resolved.
Show resolved Hide resolved

```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
tyler36 marked this conversation as resolved.
Show resolved Hide resolved
```

**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)**
Expand Down
11 changes: 0 additions & 11 deletions config.cron.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:

23 changes: 13 additions & 10 deletions web-build/Dockerfile.ddev-cron
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions web-build/cron.conf
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions web-build/custom.cron
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#ddev-generated
* * * * * date | tee -a /var/www/html/time.log