markshust / docker-magento

Mark Shust's Docker Configuration for Magento
https://m.academy/courses/set-up-magento-2-development-environment-docker/
MIT License
2.58k stars 1.01k forks source link

How to run cron permenantly #760

Open cornernote opened 2 years ago

cornernote commented 2 years ago

I saw this was asked in #691, however it's not really a permenant start.

I added restart: always to the container settings in docker-compose.yml, but cron doesn't restart.

You can reproduce this with:

$ bin/cron status
cron is running.
$ bin/restart && bin/cron status
cron is not running ... failed!

How can I get it to start whenever the container starts?

markshust commented 1 year ago

Sorry, I tried everything here but I'm very stuck in figuring this out. Tagging this as help needed.

ssx commented 1 year ago

@cornernote are you working on a commerce project?

apresources commented 1 year ago

Maybe you could incorporate the idea in this repo https://github.com/markhilton/docker-php-fpm ?

The main php-fpm container calls a script called docker-php-entrypoint, into which is inserted a script which then runs whatever scripts are required. Amongst these is a script that sets up the crontab and starts cron.

markshust commented 1 year ago

Thanks for the suggestion. I looked at the PHP 8.1 image and it doesn't look like it has a different CMD command other than the default, but perhaps I didn't look into it in enough detail. I'm willing to take a PR for something like this as long as it doesn't overly complicate the bootstrap process of the entry PHP script.

apresources commented 1 year ago

Thanks for the suggestion. I looked at the PHP 8.1 image and it doesn't look like it has a different CMD command other than the default, but perhaps I didn't look into it in enough detail. I'm willing to take a PR for something like this as long as it doesn't overly complicate the bootstrap process of the entry PHP script.

Before looking at this, I was working on doing logrotate for our docker-m2 site, which I am running in a separate alpine container with the var/log directory mounted. That container runs the crond as it's CMD. Was wondering if that could be an alternative approach, i.e. run cron on a separate container with the same deps/volumes as php-fpm? Any thoughts?

apresources commented 1 year ago

I have got this working but would not suggest making it part of the repo as it gives the app user sudo and messes with the php entrypoint.

I created images/phpfpm directory and added a Dockerfile

FROM markoshust/magento-php:8.1-fpm-1

USER root

RUN apt-get update && apt-get -y install sudo

COPY ./docker-php-entrypoint /usr/local/bin
RUN chmod +x /usr/local/bin/docker-php-entrypoint

RUN usermod -aG sudo app

RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >>/etc/sudoers

USER app

And a new docker-php-entrypoint script in the same directory

#!/bin/sh
set -e

### setup cron, requires sudo to be installed and no password set
if [ "$PHP_START_CRON" = "1" ]
then
    echo "Starting cron"
    /var/www/html/bin/magento cron:install
    sudo service cron start
    sudo touch /var/www/html/var/.setup_cronjob_status /var/www/html/var/.update_cronjob_status
    sudo chown app:app /var/www/html/var/.setup_cronjob_status /var/www/html/var/.update_cronjob_status
fi

# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
    set -- php-fpm "$@"
fi

exec "$@"

Update compose.yaml and change

    image: markoshust/magento-php:8.1-fpm-1

to

    build: ./images/phpfpm

Then to enable add the following to env/phpfpm.env

PHP_START_CRON=1

The issue of course is that crond wants to run as root and php-fpm as the app user. Another approach would be to run php-fpm as app in the entrypoint script using something like gosu, but I have been unable to get that to work so far.

markshust commented 1 year ago

Appreciate the update! I'll keep this ticket open in case someone else can chime in here.

condor872 commented 1 year ago

I solved adding these 2 lines at the bottom of bin/start script

force reinstall magento crons and start

bin/magento cron:install --force bin/cron start

markshust commented 8 months ago

@condor872 thank you, someone I completely missed this comment.

I'm going to take a deeper look into @jasonhildebrand's PR as it seems to fix this and do a lot more.

If for some reason that doesn't make it in, we can do a little workaround. When we enable cron, we can create a hidden file named something like .cron-enabled, and when we disable cron, we can delete it. Then, in bin/start we can check for the existence of the .cron-enabled file, and if it exists, run the bin/cron start command. This is a really simple solution to this that I'm surprised I didn't think of before.