Skip to content

Commit

Permalink
Merge pull request #4 from rfay/20220529_auto_install
Browse files Browse the repository at this point in the history
Use supervisord to launch cron daemon
  • Loading branch information
tyler36 authored May 30, 2022
2 parents c8da7f8 + ace938b commit cb7bb39
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 53 deletions.
12 changes: 2 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: tests
on:
pull_request:
push:
branches: [ master, main ]
branches: [ main, 20220529_auto_install ]

schedule:
- cron: '01 07 * * *'
Expand Down Expand Up @@ -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'
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea
44 changes: 19 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <!-- omit in toc -->

- [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 <https://github.com/drud/ddev-contrib/tree/master/recipes/cronjob>)
**Originally Contributed by [@thomaskieslich](https://github.com/thomaskieslich) in <https://github.com/drud/ddev-contrib/tree/master/recipes/cronjob>)**
6 changes: 6 additions & 0 deletions config.cron.yaml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 0 additions & 10 deletions config.cron.yml

This file was deleted.

6 changes: 3 additions & 3 deletions install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 12 additions & 5 deletions tests/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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" {
Expand All @@ -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
}
6 changes: 6 additions & 0 deletions web-build/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions web-build/cron.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[program:cron]
command=sudo /usr/sbin/cron -f
autorestart=true
startretries=10

0 comments on commit cb7bb39

Please sign in to comment.