wodby / docker4wordpress

Docker-based WordPress stack
https://wodby.com/docker4wordpress
MIT License
744 stars 218 forks source link

Cron not running on Page Load #74

Open ChexWarrior opened 3 years ago

ChexWarrior commented 3 years ago

The local WordPress environment doesn't seem to be running/checking cron jobs on every page load as it should be according to the WordPress docs.

The hourly run of the crond container does successfully execute any pending jobs (as does running wp cron event run --due-now manually).

I've used the following file cron-job-plugin.php to test adding a cron job via a plugin on both a vanilla WP site and a site with a mounted codebase:

<?php
/**
 * Plugin Name:     Cron Job Test
 * Plugin URI:      N/A
 * Description:     This plugin is a test to demonstrate a bug with Wodby's local WP environment
 * Author:          chexwarrior
 * Author URI:      https://ytmnd.com
 * Text Domain:     cron-job-plugin
 * Domain Path:     /languages
 * Version:         0.1.0
 *
 * @package         YTMND
 */

add_filter('cron_schedules', 'cjp_add_cron_interval');
function cjp_add_cron_interval($schedules) {
    $schedules['one_minute'] = [
        'interval' => 60,
        'display' => 'Every Minute',
    ];

    return $schedules;
}

add_action('cjp_cron_hook', 'cjp_cron_exec');
function cjp_cron_exec() {
    $time = (string) time();
    wp_insert_post([
        'post_type' => 'page',
        'post_status' => 'publish',
        'post_content' => $time,
        'post_title' => "Cron Post: $time",
    ]);
}

if (!wp_next_scheduled('cjp_cron_hook')) {
    wp_schedule_event(time(), 'one_minute', 'cjp_cron_hook');
}

Codebase

Occurs in both, but the info provided below is for vanilla WordPress (5.4.33)

Host OS

Linux Ubuntu 20.04

Docker info output

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 7
  Running: 6
  Paused: 0
  Stopped: 1
 Images: 104
 Server Version: 20.10.7
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc version: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.11.0-7614-generic
 Operating System: Ubuntu 20.04.2 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 12
 Total Memory: 15.58GiB
 Name: lildrum
 ID: 6PR4:PDAG:GBBA:R62P:2LLV:TIXS:VT3L:L4UD:INMT:GDFX:ADKT:RKBV
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Username: chexwarrior
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Docker compose file

Make sure you remove all commented services.

version: "3"

services:
  mariadb:
    image: wodby/mariadb:$MARIADB_TAG
    container_name: "${PROJECT_NAME}_mariadb"
    stop_grace_period: 30s
    environment:
      MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
      MYSQL_DATABASE: $DB_NAME
      MYSQL_USER: $DB_USER
      MYSQL_PASSWORD: $DB_PASSWORD
#    volumes:
#    - ./mariadb-init:/docker-entrypoint-initdb.d # Place init .sql file(s) here.
#    - /path/to/mariadb/data/on/host:/var/lib/mysql # I want to manage volumes manually.

  php:
    image: wodby/wordpress-php:$PHP_TAG
    container_name: "${PROJECT_NAME}_php"
    environment:
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      DB_HOST: $DB_HOST
      DB_USER: $DB_USER
      DB_PASSWORD: $DB_PASSWORD
      DB_NAME: $DB_NAME
      PHP_FPM_USER: wodby
      PHP_FPM_GROUP: wodby
#      # Read instructions at https://wodby.com/docs/stacks/wordpress/local#xdebug
#      PHP_XDEBUG: 1
#      PHP_XDEBUG_MODE: debug
#      PHP_IDE_CONFIG: serverName=my-ide
#      PHP_XDEBUG_IDEKEY: "my-ide"
#      PHP_XDEBUG_CLIENT_HOST: 172.17.0.1 # Linux
#      PHP_XDEBUG_CLIENT_HOST: host.docker.internal # Docker 18.03+ Mac/Win
#      PHP_XDEBUG_CLIENT_HOST: 10.0.75.1 # Windows
#      PHP_XDEBUG_LOG: /tmp/php-xdebug.log
    volumes:
    - ./:/var/www/html:cached
## Alternative for macOS users: Mutagen https://wodby.com/docs/stacks/wordpress/local#docker-for-mac
#    - mutagen:/var/www/html
#    # For XHProf and Xdebug profiler traces
#    - files:/mnt/files

  crond:
    image: wodby/wordpress-php:$PHP_TAG
    container_name: "${PROJECT_NAME}_crond"
    environment:
      CRONTAB: "0 * * * * wp cron event run --due-now --path=/var/www/html"
    command: sudo -E LD_PRELOAD=/usr/lib/preloadable_libiconv.so crond -f -d 0
    volumes:
    - ./:/var/www/html:cached

  nginx:
    image: wodby/nginx:$NGINX_TAG
    container_name: "${PROJECT_NAME}_nginx"
    depends_on:
    - php
    environment:
      NGINX_STATIC_OPEN_FILE_CACHE: "off"
      NGINX_ERROR_LOG_LEVEL: debug
      NGINX_BACKEND_HOST: php
      NGINX_VHOST_PRESET: wordpress
      #NGINX_SERVER_ROOT: /var/www/html/subdir
    volumes:
    - ./:/var/www/html:cached
## Alternative for macOS users: Mutagen https://wodby.com/docs/stacks/wordpress/local#docker-for-mac
#    - mutagen:/var/www/html
    labels:
    - "traefik.http.routers.${PROJECT_NAME}_nginx.rule=Host(`${PROJECT_BASE_URL}`)"

  mailhog:
    image: mailhog/mailhog
    container_name: "${PROJECT_NAME}_mailhog"
    labels:
    - "traefik.http.services.${PROJECT_NAME}_mailhog.loadbalancer.server.port=8025"
    - "traefik.http.routers.${PROJECT_NAME}_mailhog.rule=Host(`mailhog.${PROJECT_BASE_URL}`)"

  traefik:
    image: traefik:v2.0
    container_name: "${PROJECT_NAME}_traefik"
    command: --api.insecure=true --providers.docker
    ports:
    - '8000:80'
#    - '8080:8080' # Dashboard
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock

Docker Compose Override File

version: "3"

services:
  php:
    image: wodby/wordpress:$WORDPRESS_TAG
    environment:
      PHP_FPM_CLEAR_ENV: "no"
      WP_AUTH_KEY: $WP_AUTH_KEY
      WP_AUTH_SALT: $WP_AUTH_SALT
      WP_SECURE_AUTH_KEY: $WP_SECURE_AUTH_KEY
      WP_SECURE_AUTH_SALT: $WP_SECURE_AUTH_SALT
      WP_LOGGED_IN_KEY: $WP_LOGGED_IN_KEY
      WP_LOGGED_IN_SALT: $WP_LOGGED_IN_SALT
      WP_NONCE_KEY: $WP_NONCE_KEY
      WP_NONCE_SALT: $WP_NONCE_SALT

  crond:
    image: wodby/wordpress:$WORDPRESS_TAG
    environment:
      PHP_FPM_CLEAR_ENV: "no"

Logs output

If you'd like any specific logs please let me know.

cc @mralexho

ChexWarrior commented 3 years ago

Just want to say that actually it seems like cron isn't working on page load for my staging or prod sites either.