Skip to content
Draft
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ Synchronizes crontab jobs for the project.

---

### lameco:cleanup_blitz_cache

Cleans up Blitz cache from old releases for Craft CMS projects.

- Only runs for Craft CMS projects (`lameco_project_type === 'craftcms'`).
- Removes the `web/cache/blitz` directory from all previous releases except the current active one.
- Runs after successful deployment to ensure the deployment is complete before cleanup.
- Helps prevent storage bloat and ensures outdated cache files do not accumulate in old releases.
- **Why this is needed for Craft CMS**: Blitz is a static cache plugin for Craft CMS that stores cached files in `web/cache/blitz`. During deployment, these cache files can accumulate across multiple releases, consuming unnecessary disk space. Since the cache is environment-specific and tied to the current release, old cache files serve no purpose and should be cleaned up to maintain optimal server performance.

---

## Parameters

- `lameco_project_type`: Project type (auto-detected: `symfony`, `kunstmaan`, `craftcms`, `laravel`)
Expand All @@ -148,6 +160,7 @@ Synchronizes crontab jobs for the project.
- `lameco:upload_assets` runs after `lameco:build_assets`
- `lameco:restart_php` and `lameco:restart_supervisor` run after `deploy:cleanup`
- `crontab:sync` runs after `deploy:success`
- `lameco:cleanup_blitz_cache` runs after `deploy:success` for Craft CMS projects

## Usage

Expand Down
86 changes: 0 additions & 86 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,92 +48,6 @@
});


// Laméco

// Detect project type based on key files.
if (file_exists('bin/console') && file_exists('src/Kernel.php')) {
if (composerHasPackage('kunstmaan/admin-bundle')) {
$projectType = 'kunstmaan';
} else {
$projectType = 'symfony';
}

$dumpDir = 'var';
$publicDir = 'public';
} elseif (file_exists('craft')) {
$projectType = 'craftcms';
$dumpDir = 'storage';
$publicDir = 'web';
} elseif (file_exists('artisan')) {
$projectType = 'laravel';
$dumpDir = 'storage';
$publicDir = 'public';
} else {
throw new \RuntimeException('Unknown project type: cannot determine from current directory.');
}

set('lameco_project_type', $projectType);
set('lameco_dump_dir', $dumpDir);
set('lameco_public_dir', $publicDir);

set('crontab:jobs', function () {
$jobs = [];

if (composerHasPackage('putyourlightson/craft-blitz')) {
$jobs[] = '5 * * * * cd {{current_path}} && {{bin/php}} craft blitz/cache/refresh-expired';
}

if (composerHasPackage('verbb/formie')) {
$jobs[] = '5 * * * * cd {{current_path}} && {{bin/php}} craft formie/gc/prune-data-retention-submissions';
}

return $jobs;
});


// Laméco

// Detect project type based on key files.
if (file_exists('bin/console') && file_exists('src/Kernel.php')) {
if (composerHasPackage('kunstmaan/admin-bundle')) {
$projectType = 'kunstmaan';
} else {
$projectType = 'symfony';
}

$dumpDir = 'var';
$publicDir = 'public';
} elseif (file_exists('craft')) {
$projectType = 'craftcms';
$dumpDir = 'storage';
$publicDir = 'web';
} elseif (file_exists('artisan')) {
$projectType = 'laravel';
$dumpDir = 'storage';
$publicDir = 'public';
} else {
throw new \RuntimeException('Unknown project type: cannot determine from current directory.');
}

set('lameco_project_type', $projectType);
set('lameco_dump_dir', $dumpDir);
set('lameco_public_dir', $publicDir);

set('crontab:jobs', function () {
$jobs = [];

if (composerHasPackage('putyourlightson/craft-blitz')) {
$jobs[] = '5 * * * * cd {{current_path}} && {{bin/php}} craft blitz/cache/refresh-expired';
}

if (composerHasPackage('verbb/formie')) {
$jobs[] = '5 * * * * cd {{current_path}} && {{bin/php}} craft formie/gc/prune-data-retention-submissions';
}

return $jobs;
});


// Laméco

// Detect project type based on key files.
Expand Down
44 changes: 44 additions & 0 deletions src/tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,49 @@
}
});

// Clean up Blitz cache from old releases for Craft CMS projects.
desc('Clean up Blitz cache from old releases for Craft CMS projects');
task('lameco:cleanup_blitz_cache', function () {
// Only run for Craft CMS projects
if (get('lameco_project_type') !== 'craftcms') {
return;
}

writeln('Cleaning up Blitz cache from old releases...');

// Get the current release name and all releases using Deployer's built-in functions
$currentRelease = get('release_name');
$allReleases = get('releases_list');

// Filter out the current release
$oldReleases = array_filter($allReleases, function($release) use ($currentRelease) {
return $release !== $currentRelease;
});

if (empty($oldReleases)) {
writeln('No old releases found to clean up.');
return;
}

$cleanedCount = 0;
foreach ($oldReleases as $release) {
$blitzCachePath = "{{deploy_path}}/releases/{$release}/{{lameco_public_dir}}/cache/blitz";

// Check if the Blitz cache directory exists before attempting to remove it
if (test("[ -d {$blitzCachePath} ]")) {
writeln("Removing Blitz cache from release: {$release}");
run("rm -rf {$blitzCachePath}");
$cleanedCount++;
}
}

if ($cleanedCount > 0) {
writeln("Successfully cleaned Blitz cache from {$cleanedCount} old release(s).");
} else {
writeln('No Blitz cache directories found in old releases.');
}
});

before('deploy', 'lameco:stage_prompt');

before('deploy:symlink', 'lameco:build_assets');
Expand All @@ -286,6 +329,7 @@
after('deploy:cleanup', 'lameco:restart_supervisor');

after('deploy:success', 'crontab:sync');
after('deploy:success', 'lameco:cleanup_blitz_cache');

if (in_array(get('lameco_project_type'), ['symfony', 'kunstmaan'], true)) {
before('deploy:symlink', 'database:migrate');
Expand Down