Skip to content

Commit 587ed84

Browse files
authored
Merge pull request #437 from ember-nexus/feature/gh-433-add-cron
Feature/gh 433 add cron
2 parents 69a8aaa + 9b2ecff commit 587ed84

File tree

14 files changed

+128
-90
lines changed

14 files changed

+128
-90
lines changed

.github/workflows/ci-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ jobs:
277277
docker exec --user "$(id -u):$(id -g)" ember-nexus-api-dev bash -c "php bin/console healthcheck"
278278
- name: Controller Example Generation Test
279279
run: |
280-
docker exec --user "$(id -u):$(id -g)" ember-nexus-api-dev bash -c "export API_DOMAIN=\"http://ember-nexus-api\" && echo \$API_DOMAIN && composer test:example-generation-controller && composer test:example-generation-controller:deprecated"
280+
docker exec --user "$(id -u):$(id -g)" ember-nexus-api-dev bash -c "export API_DOMAIN=\"http://ember-nexus-api\" && echo \$API_DOMAIN && composer test:example-generation-controller && composer test:example-generation-controller:with-different-configuration && composer test:example-generation-controller:deprecated"
281281
282282
283283
test-example-generation-command:

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## Unreleased
8+
### Added
9+
- Add support for executing cron jobs in a plug-n-play ready fashion, closes #433.
10+
811
### Changed
912
- Get me endpoint returns status code 403 instead of 500, if anonymous user is missing. Related to #415.
1013

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"test:feature:test": "php bin/console cache:clear && ./bin/test-feature-prepare && php vendor/phpunit/phpunit/phpunit --configuration tests/phpunit.feature.xml.dist ./tests/FeatureTests --group test",
106106
"test:feature:test-fast": "php vendor/phpunit/phpunit/phpunit --configuration tests/phpunit.feature.xml.dist ./tests/FeatureTests --group test",
107107
"test:example-generation-controller": "php bin/console cache:clear && ./bin/test-feature-prepare && php vendor/phpunit/phpunit/phpunit --configuration tests/phpunit.feature.xml.dist ./tests/ExampleGenerationController --exclude-group deprecated && php bin/console cache:clear",
108+
"test:example-generation-controller:with-different-configuration": "cp -f ./config/packages/ember_nexus.yaml ./config/packages/ember_nexus_orig.yaml.dis && cp -f ./tests/ExampleGenerationControllerWithDifferentConfiguration/configuration.yml ./config/packages/ember_nexus.yaml && php bin/console cache:clear && php vendor/phpunit/phpunit/phpunit --configuration tests/phpunit.feature.xml.dist ./tests/ExampleGenerationControllerWithDifferentConfiguration && mv -f ./config/packages/ember_nexus_orig.yaml.dis ./config/packages/ember_nexus.yaml && php bin/console cache:clear",
108109
"test:example-generation-controller:deprecated": "php bin/console cache:clear && ./bin/test-feature-prepare && php vendor/phpunit/phpunit/phpunit --configuration tests/phpunit.feature.xml.dist ./tests/ExampleGenerationController --group deprecated && php bin/console cache:clear",
109110
"test:example-generation-controller:test-orig": "php bin/console cache:clear && ./bin/test-feature-prepare && php vendor/phpunit/phpunit/phpunit --configuration tests/phpunit.feature.xml.dist ./tests/ExampleGenerationController --group test && php bin/console cache:clear",
110111
"test:example-generation-controller:test": "php vendor/phpunit/phpunit/phpunit --configuration tests/phpunit.feature.xml.dist ./tests/ExampleGenerationController --group test",

config/internal-parameters.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
parameters:
22
_dev_version: 'dev'
3+
_false: false
34
version: '%env(default:_dev_version:VERSION)%'
4-
anonymousUserUUID: '%env(ANONYMOUS_USER_UUID)%'
5+
anonymousUserUUID: '%env(string:ANONYMOUS_USER_UUID)%'
6+
isCronDisabled: '%env(bool:default:_false:DISABLE_CRON)%'

docker/Caddyfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
output stdout
99
format json
1010
}
11+
supervisor {
12+
supercronic -json /etc/crontabs/supercronic
13+
}
1114
}
1215

1316
:80 {

docker/Dockerfile

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
ARG FRANKENPHP_VERSION='1.11.1'
2+
ARG PHP_VERSION='8.5.1'
3+
4+
FROM dunglas/frankenphp:${FRANKENPHP_VERSION}-builder-php${PHP_VERSION}-alpine AS frankenphp_builder
5+
COPY --from=caddy:2-builder-alpine /usr/bin/xcaddy /usr/bin/xcaddy
6+
RUN CGO_ENABLED=1 \
7+
XCADDY_SETCAP=1 \
8+
XCADDY_GO_BUILD_FLAGS="-tags=nobadger,nomysql,nopgx -ldflags='-w -s -extldflags \'-Wl,-z,stack-size=0x80000\''" \
9+
CGO_CFLAGS=$(php-config --includes) \
10+
CGO_LDFLAGS="$(php-config --ldflags) $(php-config --libs)" \
11+
xcaddy build \
12+
--output /usr/local/bin/frankenphp \
13+
--with github.com/dunglas/frankenphp=./ \
14+
--with github.com/dunglas/frankenphp/caddy=./caddy/ \
15+
--with github.com/dunglas/caddy-cbrotli \
16+
--with github.com/baldinof/caddy-supervisor
17+
18+
119
FROM golang:1.25-alpine AS go_builder
220
COPY ./lib/ExprCli /build
321
RUN cd /build && \
@@ -6,7 +24,8 @@ RUN cd /build && \
624
go build -ldflags="-s -w" -o expr-cli main.go && \
725
upx --best --lzma expr-cli
826

9-
FROM dunglas/frankenphp:1.11.1-php8.5.1-alpine AS php
27+
28+
FROM dunglas/frankenphp:${FRANKENPHP_VERSION}-php${PHP_VERSION}-alpine AS php
1029
# see also https://github.com/opencontainers/image-spec/blob/main/annotations.md
1130
LABEL org.opencontainers.image.vendor="Ember Nexus" \
1231
org.opencontainers.image.authors="Sören Klein / Syndesi <soerenklein98@gmail.com>" \
@@ -18,7 +37,10 @@ LABEL org.opencontainers.image.vendor="Ember Nexus" \
1837
# Background: If the en user uses the dev mode, it is epected that he knows what he does.
1938
ARG VERSION=dev
2039
ENV VERSION=${VERSION}
40+
ARG FRANKENPHP_VERSION
41+
ENV FRANKENPHP_VERSION=${FRANKENPHP_VERSION}
2142

43+
COPY --from=frankenphp_builder /usr/local/bin/frankenphp /usr/local/bin/frankenphp
2244
RUN apk add --no-cache \
2345
ca-certificates \
2446
curl \
@@ -27,6 +49,7 @@ RUN apk add --no-cache \
2749
# https://github.com/docker-library/php/issues/494
2850
openssl \
2951
pcre \
52+
supercronic \
3053
tar \
3154
xz \
3255
zlib \
@@ -49,12 +72,13 @@ RUN apk add --no-cache \
4972

5073
COPY ./docker/Caddyfile /etc/frankenphp/Caddyfile
5174
COPY ./docker/docker-entrypoint.sh /usr/local/bin/
75+
COPY ./docker/supercronic /etc/crontabs/supercronic
5276
COPY --from=go_builder /build/expr-cli /usr/local/bin/expr-cli
5377
RUN set -x \
5478
&& chmod +x /usr/local/bin/docker-entrypoint.sh \
5579
&& adduser -D worker \
5680
&& setcap CAP_NET_BIND_SERVICE=+eip /usr/local/bin/frankenphp \
57-
&& chown -R worker:worker /config/caddy /data/caddy
81+
&& chown -R worker:worker /config/caddy /data/caddy /etc/crontabs/supercronic
5882

5983
STOPSIGNAL SIGTERM
6084

@@ -103,6 +127,8 @@ LABEL org.opencontainers.image.vendor="Ember Nexus" \
103127
# Background: If the en user uses the dev mode, it is epected that he knows what he does.
104128
ARG VERSION=dev
105129
ENV VERSION=${VERSION}
130+
ARG FRANKENPHP_VERSION
131+
ENV FRANKENPHP_VERSION=${FRANKENPHP_VERSION}
106132

107133
COPY --from=production-build / /
108134

docker/supercronic

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# + seconds
2+
# | + minutes
3+
# | | + hours
4+
# | | | + day of month
5+
# | | | | + month
6+
# | | | | | + day of week
7+
# | | | | | | + year
8+
# | | | | | | | + command
9+
# | | | | | | | |
10+
0 */2 * * * * * php bin/console cron

docs/commands/assets/system-healthcheck.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555

5656
<span class="bold "> Check internal services </span>──────────────────────────────────────────────────────
5757
│ Alpine version: 3.23.2
58-
│ FrankenPHP version: 1.11.1
5958
│ Caddy version: 2.10.2
59+
│ FrankenPHP version: 1.11.1
6060
│ PHP version: 8.5.1
6161
└ Internal services are ok.
6262

src/Command/CronCommand.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Command;
6+
7+
use App\Style\EmberNexusStyle;
8+
use LogicException;
9+
use Symfony\Component\Console\Attribute\AsCommand;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
use Symfony\Component\Console\Style\OutputStyle;
14+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
15+
16+
/**
17+
* @psalm-suppress PropertyNotSetInConstructor $io
18+
*/
19+
#[AsCommand(name: 'cron', description: 'Executes background tasks')]
20+
class CronCommand extends Command
21+
{
22+
private OutputStyle $io;
23+
24+
public function __construct(
25+
private ParameterBagInterface $bag,
26+
) {
27+
parent::__construct();
28+
}
29+
30+
protected function execute(InputInterface $input, OutputInterface $output): int
31+
{
32+
$this->io = new EmberNexusStyle($input, $output);
33+
34+
$this->io->title('Cron');
35+
36+
$this->io->writeln('This command is currently a placeholder.');
37+
$this->io->newLine();
38+
39+
$isCronDisabled = $this->bag->get('isCronDisabled');
40+
if (!is_bool($isCronDisabled)) {
41+
throw new LogicException(sprintf('Expected "isCronDisabled" to be of type boolean, got %s.', get_debug_type($isCronDisabled)));
42+
}
43+
if ($isCronDisabled) {
44+
$this->io->finalMessage('Cron is disabled; this command terminates early.');
45+
46+
return Command::SUCCESS;
47+
}
48+
49+
$this->io->finalMessage('Finished.');
50+
51+
return Command::SUCCESS;
52+
}
53+
}

src/Command/HealthcheckCommand.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
128128
$alpineVersion
129129
));
130130

131-
$frankenPhpVersion = 'unknown';
132131
$caddyVersion = 'unknown';
133-
$frankenPhpVersionOutput = explode(' ', \Safe\shell_exec('frankenphp -v 2>&1') ?? '');
134-
if (count($frankenPhpVersionOutput) > 6) {
135-
$frankenPhpVersion = \Safe\preg_replace('/[^0-9.]/', '', $frankenPhpVersionOutput[1]);
136-
$caddyVersion = \Safe\preg_replace('/[^0-9.]/', '', $frankenPhpVersionOutput[5]);
132+
$caddyVersionOutput = explode(' ', \Safe\shell_exec('frankenphp -v 2>&1') ?? '');
133+
if (count($caddyVersionOutput) > 1) {
134+
$caddyVersion = \Safe\preg_replace('/[^0-9.]/', '', $caddyVersionOutput[0]);
137135
}
138-
/** @var string $frankenPhpVersion */
139136
/** @var string $caddyVersion */
140-
$this->io->writeln(sprintf(
141-
'FrankenPHP version: %s',
142-
$frankenPhpVersion
143-
));
144137
$this->io->writeln(sprintf(
145138
'Caddy version: %s',
146139
$caddyVersion
147140
));
148141

142+
$frankenPhpVersion = 'unknown';
143+
$frankenPhpVersionOutput = getenv('FRANKENPHP_VERSION');
144+
if (is_string($frankenPhpVersionOutput)) {
145+
$frankenPhpVersion = \Safe\preg_replace('/[^0-9.]/', '', $frankenPhpVersionOutput);
146+
}
147+
/** @var string $frankenPhpVersion */
148+
$this->io->writeln(sprintf(
149+
'FrankenPHP version: %s',
150+
$frankenPhpVersion
151+
));
152+
149153
/**
150154
* @psalm-suppress PossiblyFalseArgument
151155
*/

0 commit comments

Comments
 (0)