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

feat(php-fpm): add cron-control-runner #783

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 23 additions & 6 deletions php-fpm/Dockerfile.81
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ RUN \
eatmydata apt-get -y upgrade && \
eatmydata apt-get install -y software-properties-common gnupg --no-install-recommends && \
eatmydata add-apt-repository -y ppa:ondrej/php && \
eatmydata apt-get install -y curl less git jq mysql-client openssl wget cron vim nano && \
eatmydata apt-get install -y curl less git jq mysql-client openssl wget vim nano && \
eatmydata apt-get install -y ghostscript msmtp --no-install-recommends && \
eatmydata apt-get install -y \
php8.1-cli php8.1-fpm \
Expand All @@ -33,21 +33,38 @@ RUN \
eatmydata apt-get remove --purge -y software-properties-common && \
rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/* && \
eatmydata apt-get autoremove --purge -y && \
ln -s /usr/sbin/php-fpm8.1 /usr/sbin/php-fpm && \
chmod 02755 /usr/bin/crontab
ln -s /usr/sbin/php-fpm8.1 /usr/sbin/php-fpm

RUN \
usermod -d /home/www-data -s /bin/bash www-data && \
install -d -D -m 0750 -o www-data -g www-data /home/www-data && \
install -d -D -m 0777 -o www-data -g www-data /var/www/html && \
install -d -D -m 0755 -o www-data -g www-data /run/php
install -d -D -m 0755 -o www-data -g www-data /run/php && \
install -d -D -m 0755 -o root -g root /usr/local/share

RUN \
wget -O /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-9.phar && chmod 0755 /usr/local/bin/phpunit && \
wget -O /usr/local/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && chmod 0755 /usr/local/bin/wp && \
wget -O /usr/local/bin/wp.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
php -r '(new Phar("/usr/local/bin/wp.phar"))->extractTo("/usr/local/share/wp");' && \
mv /usr/local/bin/wp.phar /usr/local/bin/wp && \
chmod 0755 /usr/local/bin/wp && \
wget -q https://getcomposer.org/installer -O - | php -- --install-dir=/usr/bin/ --filename=composer

ENV WP_CLI_CONFIG_PATH /config/wp-cli.yaml
ENV WP_CLI_CONFIG_PATH=/config/wp-cli.yaml

RUN \
ARCH="$(arch)"; \
LATEST=$(curl -w '%{url_effective}' -ILsS https://github.com/Automattic/cron-control-runner/releases/latest -o /dev/null | sed -e 's|^.*/||'); \
if [ "${ARCH}" = "arm64" ] || [ "${ARCH}" = "aarch64" ]; then \
ARCH="arm64"; \
elif [ "${ARCH}" = "x86_64" ] || [ "${ARCH}" = "amd64" ]; then \
ARCH="amd64"; \
else \
echo "(!) Unsupported architecture: ${ARCH}"; \
exit 1; \
fi && \
curl -SL "https://github.com/Automattic/cron-control-runner/releases/download/${LATEST}/cron-control-runner-linux-${ARCH}" -o /usr/bin/cron-control-runner && \
chmod +x /usr/bin/cron-control-runner

COPY --from=build /usr/lib/php/20210902/timezonedb.so /usr/lib/php/20210902/timezonedb.so
COPY rootfs/ /
Expand Down
29 changes: 23 additions & 6 deletions php-fpm/Dockerfile.82
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ RUN \
eatmydata apt-get -y upgrade && \
eatmydata apt-get install -y software-properties-common gnupg --no-install-recommends && \
eatmydata add-apt-repository -y ppa:ondrej/php && \
eatmydata apt-get install -y curl less git jq mysql-client openssl wget cron vim nano && \
eatmydata apt-get install -y curl less git jq mysql-client openssl wget vim nano && \
eatmydata apt-get install -y ghostscript msmtp --no-install-recommends && \
eatmydata apt-get install -y \
php8.2-cli php8.2-fpm \
Expand All @@ -33,21 +33,38 @@ RUN \
eatmydata apt-get remove --purge -y software-properties-common && \
rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/* && \
eatmydata apt-get autoremove --purge -y && \
ln -s /usr/sbin/php-fpm8.2 /usr/sbin/php-fpm && \
chmod 02755 /usr/bin/crontab
ln -s /usr/sbin/php-fpm8.2 /usr/sbin/php-fpm

RUN \
usermod -d /home/www-data -s /bin/bash www-data && \
install -d -D -m 0750 -o www-data -g www-data /home/www-data && \
install -d -D -m 0777 -o www-data -g www-data /var/www/html && \
install -d -D -m 0755 -o www-data -g www-data /run/php
install -d -D -m 0755 -o www-data -g www-data /run/php && \
install -d -D -m 0755 -o root -g root /usr/local/share

RUN \
wget -O /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-9.phar && chmod 0755 /usr/local/bin/phpunit && \
wget -O /usr/local/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && chmod 0755 /usr/local/bin/wp && \
wget -O /usr/local/bin/wp.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
php -r '(new Phar("/usr/local/bin/wp.phar"))->extractTo("/usr/local/share/wp");' && \
mv /usr/local/bin/wp.phar /usr/local/bin/wp && \
chmod 0755 /usr/local/bin/wp && \
wget -q https://getcomposer.org/installer -O - | php -- --install-dir=/usr/bin/ --filename=composer

ENV WP_CLI_CONFIG_PATH /config/wp-cli.yaml
ENV WP_CLI_CONFIG_PATH=/config/wp-cli.yaml

RUN \
ARCH="$(arch)"; \
LATEST=$(curl -w '%{url_effective}' -ILsS https://github.com/Automattic/cron-control-runner/releases/latest -o /dev/null | sed -e 's|^.*/||'); \
if [ "${ARCH}" = "arm64" ] || [ "${ARCH}" = "aarch64" ]; then \
ARCH="arm64"; \
elif [ "${ARCH}" = "x86_64" ] || [ "${ARCH}" = "amd64" ]; then \
ARCH="amd64"; \
else \
echo "(!) Unsupported architecture: ${ARCH}"; \
exit 1; \
fi && \
curl -SL "https://github.com/Automattic/cron-control-runner/releases/download/${LATEST}/cron-control-runner-linux-${ARCH}" -o /usr/bin/cron-control-runner && \
chmod +x /usr/bin/cron-control-runner

COPY --from=build /usr/lib/php/20220829/timezonedb.so /usr/lib/php/20220829/timezonedb.so
COPY rootfs/ /
Expand Down
29 changes: 23 additions & 6 deletions php-fpm/Dockerfile.83
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ RUN \
eatmydata apt-get -y upgrade && \
eatmydata apt-get install -y software-properties-common gnupg libmcrypt4 zlib1g libmemcached11 libgraphicsmagick-q16-3 --no-install-recommends && \
eatmydata add-apt-repository -y ppa:ondrej/php && \
eatmydata apt-get install -y curl less git jq mysql-client openssl wget cron vim nano && \
eatmydata apt-get install -y curl less git jq mysql-client openssl wget vim nano && \
eatmydata apt-get install -y ghostscript msmtp --no-install-recommends && \
eatmydata apt-get install -y \
php8.3-cli php8.3-fpm \
Expand All @@ -33,21 +33,38 @@ RUN \
eatmydata apt-get remove --purge -y software-properties-common && \
rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/* && \
eatmydata apt-get autoremove --purge -y && \
ln -s /usr/sbin/php-fpm8.3 /usr/sbin/php-fpm && \
chmod 02755 /usr/bin/crontab
ln -s /usr/sbin/php-fpm8.3 /usr/sbin/php-fpm

RUN \
usermod -d /home/www-data -s /bin/bash www-data && \
install -d -D -m 0750 -o www-data -g www-data /home/www-data && \
install -d -D -m 0777 -o www-data -g www-data /var/www/html && \
install -d -D -m 0755 -o www-data -g www-data /run/php
install -d -D -m 0755 -o www-data -g www-data /run/php && \
install -d -D -m 0755 -o root -g root /usr/local/share

RUN \
wget -O /usr/local/bin/phpunit https://phar.phpunit.de/phpunit-9.phar && chmod 0755 /usr/local/bin/phpunit && \
wget -O /usr/local/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && chmod 0755 /usr/local/bin/wp && \
wget -O /usr/local/bin/wp.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
php -r '(new Phar("/usr/local/bin/wp.phar"))->extractTo("/usr/local/share/wp");' && \
mv /usr/local/bin/wp.phar /usr/local/bin/wp && \
chmod 0755 /usr/local/bin/wp && \
wget -q https://getcomposer.org/installer -O - | php -- --install-dir=/usr/bin/ --filename=composer

ENV WP_CLI_CONFIG_PATH /config/wp-cli.yaml
ENV WP_CLI_CONFIG_PATH=/config/wp-cli.yaml

RUN \
ARCH="$(arch)"; \
LATEST=$(curl -w '%{url_effective}' -ILsS https://github.com/Automattic/cron-control-runner/releases/latest -o /dev/null | sed -e 's|^.*/||'); \
if [ "${ARCH}" = "arm64" ] || [ "${ARCH}" = "aarch64" ]; then \
ARCH="arm64"; \
elif [ "${ARCH}" = "x86_64" ] || [ "${ARCH}" = "amd64" ]; then \
ARCH="amd64"; \
else \
echo "(!) Unsupported architecture: ${ARCH}"; \
exit 1; \
fi && \
curl -SL "https://github.com/Automattic/cron-control-runner/releases/download/${LATEST}/cron-control-runner-linux-${ARCH}" -o /usr/bin/cron-control-runner && \
chmod +x /usr/bin/cron-control-runner

COPY --from=build /usr/lib/php/20230831/timezonedb.so /usr/lib/php/20230831/
COPY rootfs/ /
Expand Down
14 changes: 4 additions & 10 deletions php-fpm/rootfs/usr/local/bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@ else
phpdismod mailhog mailpit
fi

cleanup() {
if [ -n "${ENABLE_CRON}" ]; then
/usr/sbin/service cron stop
fi
}

trap cleanup EXIT INT TERM

if [ -n "${ENABLE_CRON}" ]; then
echo "*/10 * * * * /usr/bin/flock -n /tmp/wp-cron.lock /usr/local/bin/wp-cron.sh" | crontab -u www-data -
/usr/sbin/service cron start
/usr/bin/cron-control-runner -fpm-url tcp://127.0.0.1:9000 -wp-cli-path /usr/local/bin/wp -wp-path /wp -prom-metrics-address :4444 &
PID=$!
# shellcheck disable=SC2064
trap "kill ${PID}" EXIT INT TERM
fi

/usr/sbin/php-fpm
19 changes: 0 additions & 19 deletions php-fpm/rootfs/usr/local/bin/wp-cron.sh

This file was deleted.

81 changes: 81 additions & 0 deletions php-fpm/rootfs/var/wpvip/fpm-cron-runner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace WPCLI\FPM;
error_reporting( 0 );

if ( isset( $_POST['payload'] ) ) {
$payload_str = $_POST['payload'];
unset( $_POST['payload'] );
} elseif ( isset ( $_GET['payload'] ) ) {
$payload_str = $_GET['payload'];
unset( $_GET['payload'] );
} else {
header( 'Status: 400 Bad Request' );
header( 'Content-Type: text/plain' );
echo 'no payload given' . "\n";
exit( 1 );
}

try {
$payload = json_decode( $payload_str, null, 512, JSON_THROW_ON_ERROR | JSON_OBJECT_AS_ARRAY );
if ( ! is_array( $payload ) ) {
throw new \Exception( "not a json array" );
}
} catch ( \Exception $e ) {
header( 'Status: 400 Bad Request' );
header( 'Content-Type: text/plain' );
echo 'payload cannot be decoded as json: ' . $e->getMessage() . "\n";
exit( 1 );
}

array_unshift( $payload, '/usr/local/bin/wp' );

global $_SERVER;
$_SERVER['argv'] = $payload;
$_SERVER['SCRIPT_NAME'] = '/usr/local/bin/wp';
$_SERVER['SCRIPT_FILENAME'] = '/usr/local/bin/wp';
unset( $_SERVER['FCGI_ROLE'] );
unset( $_SERVER['GATEWAY_INTERFACE'] );
unset( $_SERVER['QUERY_STRING'] );
unset( $_SERVER['REQUEST_METHOD'] );

global $_ENV;
$_ENV['SCRIPT_NAME'] = '/usr/local/bin/wp';
$_ENV['SCRIPT_FILENAME'] = '/usr/local/bin/wp';
unset( $_ENV['FCGI_ROLE'] );
unset( $_ENV['GATEWAY_INTERFACE'] );
unset( $_ENV['QUERY_STRING'] );
unset( $_ENV['REQUEST_METHOD'] );

global $argv;
$argv = $payload;

$outfh = tmpfile();
$errfh = tmpfile();

register_shutdown_function( function () use ( $outfh, $errfh ) {
$result = [
'buf' => ob_get_contents(),
];
ob_end_clean();
fseek( $outfh, 0 );
$result['stdout'] = stream_get_contents( $outfh );
fclose( $outfh );
fseek( $errfh, 0 );
$result['stderr'] = stream_get_contents( $errfh );
fclose( $errfh );
header( 'Status: 200 OK' );
header( 'Content-Type: application/json' );
echo json_encode( $result );
} );

define( 'WP_CLI_ROOT', '/usr/local/share/wp/vendor/wp-cli/wp-cli' );
define( 'STDIN', fopen( '/dev/null', 'r' ) );
define( 'STDOUT', $outfh );
define( 'STDERR', $errfh );

ob_start();

require_once WP_CLI_ROOT . '/php/wp-cli.php';

exit( 0 );