Skip to content

Commit 06a08aa

Browse files
authored
add script (#10)
1 parent b4f9138 commit 06a08aa

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ AWS_USE_PATH_STYLE_ENDPOINT=false
6565
VITE_APP_NAME="${APP_NAME}"
6666

6767
HEALTH_CHECK_INTERVAL=24
68+
CLEANUP_SITE_DELAY=7
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\Site;
6+
use Carbon\Carbon;
7+
use Illuminate\Console\Command;
8+
use Illuminate\Database\Eloquent\Collection;
9+
10+
class CleanupSitesList extends Command
11+
{
12+
protected int $totalDeleted = 0;
13+
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'app:cleanup-sites-list';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Cleanup sites that are too old and haven\'t been seen for a while' ;
27+
28+
/**
29+
* Execute the console command.
30+
*/
31+
public function handle(): int
32+
{
33+
$this->output->writeln('Cleanup sites ');
34+
35+
Site::query()
36+
->whereDate(
37+
'last_seen',
38+
'<',
39+
Carbon::now()->subDays((int) config('autoupdates.cleanup_site_delay')) // @phpstan-ignore-line
40+
)
41+
->chunkById(
42+
100,
43+
function (Collection $chunk) {
44+
// Show progress
45+
$this->output->write('.');
46+
47+
$this->totalDeleted += $chunk->count();
48+
49+
// Push each site check to queue
50+
$chunk->each(fn ($site) => $site->delete());
51+
}
52+
);
53+
54+
// Result
55+
$this->output->writeln("");
56+
$this->output->writeln('Deleted ' . $this->totalDeleted . ' Sites');
57+
58+
return Command::SUCCESS;
59+
}
60+
}

config/autoupdates.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

33
return [
4-
'healthcheck_interval' => env('HEALTH_CHECK_INTERVAL', 24)
4+
'healthcheck_interval' => env('HEALTH_CHECK_INTERVAL', 24),
5+
'cleanup_site_delay' => env('CLEANUP_SITE_DELAY', 7),
56
];

0 commit comments

Comments
 (0)