Skip to content

Commit

Permalink
ISSUE-186: Add cron to supervisord
Browse files Browse the repository at this point in the history
  • Loading branch information
robiningelbrecht committed Jan 24, 2025
1 parent 2132028 commit 0a89629
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
28 changes: 21 additions & 7 deletions bin/cron/import-and-build.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@
<?php

use Crunz\Schedule;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

require_once dirname(__DIR__, 2).'/vendor/autoload_runtime.php';

$schedule = new Schedule();

if (!$cronExpression = getenv('IMPORT_AND_BUILD_SCHEDULE')) {
return $schedule;
}

$task = $schedule
->run(function () {
$process = new Process(
command: ['/var/www/bin/console', 'app:strava:build-files'],
timeout: null
);
$process->run();
$processesToRun = [
new Process(['/var/www/bin/console', 'app:strava:import-data']),
new Process(['/var/www/bin/console', 'app:strava:build-files']),
];

echo $process->getOutput();
foreach ($processesToRun as $process) {
try {
$process->setTimeout(null);
$process->mustRun();
echo $process->getOutput();
} catch (ProcessFailedException $exception) {
echo $exception->getMessage();
break;
}
}
})
->description('Import Strava data and build HTML files')
->cron('* * * * *')
->cron($cronExpression)
->preventOverlapping();

return $schedule;
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
working_dir: /var/www
environment:
- PHP_CS_FIXER_IGNORE_ENV=true
- IMPORT_AND_BUILD_SCHEDULE=* * * * *
profiles:
- on-demand
networks:
Expand Down
6 changes: 6 additions & 0 deletions docker/app/config/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ stderr_logfile_maxbytes=0
autorestart=false
startretries=0

[program:cron]
command= sh -c "vendor/bin/crunz schedule:run && sleep 60"
directory=/var/www
autostart=true
autorestart=true

[program:nginx]
command=nginx -g 'daemon off;'
stdout_logfile=/dev/stdout
Expand Down
4 changes: 2 additions & 2 deletions src/Console/BuildStravaActivityFilesConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (!$this->migrationRunner->isAtLatestVersion()) {
$output->writeln('<error>Your database is not up to date with the migration schema. Run the import command before building the HTML files</error>');

return Command::SUCCESS;
return Command::FAILURE;
}
if (!$this->stravaDataImportStatus->isCompleted()) {
$output->writeln('<error>Wait until all Strava data has been imported before building the app</error>');

return Command::SUCCESS;
return Command::FAILURE;
}
$this->resourceUsage->startTimer();

Expand Down
2 changes: 1 addition & 1 deletion src/Console/ImportStravaDataConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} catch (UnableToWriteFile|UnableToCreateDirectory) {
$output->writeln('<error>Make sure the container has write permissions to "storage/database" and "storage/files" on the host system</error>');

return Command::SUCCESS;
return Command::FAILURE;
}

$this->resourceUsage->startTimer();
Expand Down

0 comments on commit 0a89629

Please sign in to comment.