2023-03-17 14:33:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console;
|
|
|
|
|
2023-11-17 20:16:25 +00:00
|
|
|
use App\Jobs\CheckLogDrainContainerJob;
|
2023-08-15 12:11:38 +00:00
|
|
|
use App\Jobs\CleanupInstanceStuffsJob;
|
2023-09-14 10:45:50 +00:00
|
|
|
use App\Jobs\ContainerStatusJob;
|
2024-06-10 20:43:34 +00:00
|
|
|
use App\Jobs\DatabaseBackupJob;
|
2024-07-18 12:43:21 +00:00
|
|
|
use App\Jobs\DockerCleanupJob;
|
2024-06-04 19:57:00 +00:00
|
|
|
use App\Jobs\PullCoolifyImageJob;
|
2023-10-26 09:38:37 +00:00
|
|
|
use App\Jobs\PullHelperImageJob;
|
2024-05-08 17:19:32 +00:00
|
|
|
use App\Jobs\PullSentinelImageJob;
|
2024-05-30 10:56:29 +00:00
|
|
|
use App\Jobs\PullTemplatesFromCDN;
|
2024-06-10 20:43:34 +00:00
|
|
|
use App\Jobs\ScheduledTaskJob;
|
2023-11-16 10:53:37 +00:00
|
|
|
use App\Jobs\ServerStatusJob;
|
2023-08-08 15:28:36 +00:00
|
|
|
use App\Models\ScheduledDatabaseBackup;
|
2024-01-02 02:23:29 +00:00
|
|
|
use App\Models\ScheduledTask;
|
2023-09-11 20:29:34 +00:00
|
|
|
use App\Models\Server;
|
2023-11-17 14:11:29 +00:00
|
|
|
use App\Models\Team;
|
2023-03-17 14:33:48 +00:00
|
|
|
use Illuminate\Console\Scheduling\Schedule;
|
|
|
|
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
|
|
|
|
|
|
|
class Kernel extends ConsoleKernel
|
|
|
|
{
|
2024-05-16 19:36:01 +00:00
|
|
|
private $all_servers;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-03-17 14:33:48 +00:00
|
|
|
protected function schedule(Schedule $schedule): void
|
|
|
|
{
|
2024-05-16 19:36:01 +00:00
|
|
|
$this->all_servers = Server::all();
|
2023-08-27 13:23:47 +00:00
|
|
|
if (isDev()) {
|
2023-10-26 09:38:37 +00:00
|
|
|
// Instance Jobs
|
2023-10-18 13:43:14 +00:00
|
|
|
$schedule->command('horizon:snapshot')->everyMinute();
|
2023-09-15 09:19:36 +00:00
|
|
|
$schedule->job(new CleanupInstanceStuffsJob)->everyMinute()->onOneServer();
|
2024-05-30 10:56:29 +00:00
|
|
|
$schedule->job(new PullTemplatesFromCDN)->everyTwoHours()->onOneServer();
|
2023-10-26 09:38:37 +00:00
|
|
|
// Server Jobs
|
2023-10-18 13:43:14 +00:00
|
|
|
$this->check_scheduled_backups($schedule);
|
2023-09-14 10:45:50 +00:00
|
|
|
$this->check_resources($schedule);
|
2023-10-17 17:00:23 +00:00
|
|
|
$this->check_scheduled_backups($schedule);
|
2024-01-02 02:23:29 +00:00
|
|
|
$this->check_scheduled_tasks($schedule);
|
2024-04-11 10:56:24 +00:00
|
|
|
$schedule->command('uploads:clear')->everyTwoMinutes();
|
2023-06-06 06:43:01 +00:00
|
|
|
} else {
|
2023-10-26 09:38:37 +00:00
|
|
|
// Instance Jobs
|
2023-06-06 06:43:01 +00:00
|
|
|
$schedule->command('horizon:snapshot')->everyFiveMinutes();
|
2024-01-30 09:52:57 +00:00
|
|
|
$schedule->command('cleanup:unreachable-servers')->daily();
|
2024-06-04 19:57:00 +00:00
|
|
|
$schedule->job(new PullCoolifyImageJob)->everyTenMinutes()->onOneServer();
|
|
|
|
$schedule->job(new PullTemplatesFromCDN)->everyThirtyMinutes()->onOneServer();
|
2024-04-09 11:02:22 +00:00
|
|
|
$schedule->job(new CleanupInstanceStuffsJob)->everyTwoMinutes()->onOneServer();
|
2023-11-02 10:45:43 +00:00
|
|
|
// $schedule->job(new CheckResaleLicenseJob)->hourly()->onOneServer();
|
2023-10-26 09:38:37 +00:00
|
|
|
|
|
|
|
// Server Jobs
|
2023-09-12 13:47:30 +00:00
|
|
|
$this->check_scheduled_backups($schedule);
|
|
|
|
$this->check_resources($schedule);
|
2024-06-04 19:57:00 +00:00
|
|
|
$this->pull_images($schedule);
|
2024-01-02 02:23:29 +00:00
|
|
|
$this->check_scheduled_tasks($schedule);
|
2024-03-04 11:17:33 +00:00
|
|
|
|
2024-03-26 10:17:48 +00:00
|
|
|
$schedule->command('cleanup:database --yes')->daily();
|
2024-04-11 10:56:24 +00:00
|
|
|
$schedule->command('uploads:clear')->everyTwoMinutes();
|
2023-10-26 09:38:37 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-06-04 19:57:00 +00:00
|
|
|
private function pull_images($schedule)
|
2023-10-26 09:38:37 +00:00
|
|
|
{
|
2024-05-16 19:36:01 +00:00
|
|
|
$servers = $this->all_servers->where('settings.is_usable', true)->where('settings.is_reachable', true)->where('ip', '!=', '1.2.3.4');
|
2023-10-26 09:38:37 +00:00
|
|
|
foreach ($servers as $server) {
|
2024-06-20 11:17:06 +00:00
|
|
|
if ($server->isSentinelEnabled()) {
|
2024-05-10 07:21:19 +00:00
|
|
|
$schedule->job(new PullSentinelImageJob($server))->everyFiveMinutes()->onOneServer();
|
|
|
|
}
|
2024-05-09 13:27:14 +00:00
|
|
|
$schedule->job(new PullHelperImageJob($server))->everyFiveMinutes()->onOneServer();
|
2023-09-29 12:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-02 13:53:12 +00:00
|
|
|
private function check_resources($schedule)
|
|
|
|
{
|
2023-10-06 22:51:01 +00:00
|
|
|
if (isCloud()) {
|
2024-05-16 19:36:01 +00:00
|
|
|
$servers = $this->all_servers->whereNotNull('team.subscription')->where('team.subscription.stripe_trial_already_ended', false)->where('ip', '!=', '1.2.3.4');
|
2023-11-17 14:11:29 +00:00
|
|
|
$own = Team::find(0)->servers;
|
|
|
|
$servers = $servers->merge($own);
|
2024-01-16 14:19:14 +00:00
|
|
|
$containerServers = $servers->where('settings.is_swarm_worker', false)->where('settings.is_build_server', false);
|
2023-10-06 22:51:01 +00:00
|
|
|
} else {
|
2024-05-16 19:36:01 +00:00
|
|
|
$servers = $this->all_servers->where('ip', '!=', '1.2.3.4');
|
2024-01-16 14:19:14 +00:00
|
|
|
$containerServers = $servers->where('settings.is_swarm_worker', false)->where('settings.is_build_server', false);
|
2023-10-06 22:51:01 +00:00
|
|
|
}
|
2023-12-18 13:01:25 +00:00
|
|
|
foreach ($containerServers as $server) {
|
2024-05-16 09:23:31 +00:00
|
|
|
$schedule->job(new ContainerStatusJob($server))->everyMinute()->onOneServer();
|
2024-03-04 10:01:14 +00:00
|
|
|
if ($server->isLogDrainEnabled()) {
|
2024-05-16 09:23:31 +00:00
|
|
|
$schedule->job(new CheckLogDrainContainerJob($server))->everyMinute()->onOneServer();
|
2023-11-17 20:16:25 +00:00
|
|
|
}
|
2023-09-02 13:53:12 +00:00
|
|
|
}
|
2023-12-18 13:01:25 +00:00
|
|
|
foreach ($servers as $server) {
|
2024-05-16 09:23:31 +00:00
|
|
|
$schedule->job(new ServerStatusJob($server))->everyMinute()->onOneServer();
|
2024-07-18 12:43:21 +00:00
|
|
|
$schedule->job(new DockerCleanupJob($server))->everyTenMinutes()->onOneServer();
|
2023-12-18 13:01:25 +00:00
|
|
|
}
|
2023-08-08 15:28:36 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-08 15:28:36 +00:00
|
|
|
private function check_scheduled_backups($schedule)
|
|
|
|
{
|
|
|
|
$scheduled_backups = ScheduledDatabaseBackup::all();
|
|
|
|
if ($scheduled_backups->isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach ($scheduled_backups as $scheduled_backup) {
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $scheduled_backup->enabled) {
|
2023-08-16 15:18:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-09-11 20:29:34 +00:00
|
|
|
if (is_null(data_get($scheduled_backup, 'database'))) {
|
2023-09-09 11:18:49 +00:00
|
|
|
ray('database not found');
|
|
|
|
$scheduled_backup->delete();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-09 11:18:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-08-16 15:18:50 +00:00
|
|
|
|
2023-08-09 15:57:27 +00:00
|
|
|
if (isset(VALID_CRON_STRINGS[$scheduled_backup->frequency])) {
|
|
|
|
$scheduled_backup->frequency = VALID_CRON_STRINGS[$scheduled_backup->frequency];
|
|
|
|
}
|
2023-08-10 13:52:54 +00:00
|
|
|
$schedule->job(new DatabaseBackupJob(
|
2023-08-08 15:28:36 +00:00
|
|
|
backup: $scheduled_backup
|
2023-09-01 14:30:50 +00:00
|
|
|
))->cron($scheduled_backup->frequency)->onOneServer();
|
2023-08-08 15:28:36 +00:00
|
|
|
}
|
2023-03-17 14:33:48 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-01-16 14:19:14 +00:00
|
|
|
private function check_scheduled_tasks($schedule)
|
|
|
|
{
|
2024-01-02 02:23:29 +00:00
|
|
|
$scheduled_tasks = ScheduledTask::all();
|
|
|
|
if ($scheduled_tasks->isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach ($scheduled_tasks as $scheduled_task) {
|
2024-04-09 11:03:06 +00:00
|
|
|
if ($scheduled_task->enabled === false) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-02-14 14:14:06 +00:00
|
|
|
$service = $scheduled_task->service;
|
|
|
|
$application = $scheduled_task->application;
|
2024-01-02 02:23:29 +00:00
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $application && ! $service) {
|
2024-01-02 02:23:29 +00:00
|
|
|
ray('application/service attached to scheduled task does not exist');
|
|
|
|
$scheduled_task->delete();
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-01-02 02:23:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-05-14 10:52:01 +00:00
|
|
|
if ($application) {
|
|
|
|
if (str($application->status)->contains('running') === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($service) {
|
2024-05-14 13:18:59 +00:00
|
|
|
if (str($service->status())->contains('running') === false) {
|
2024-05-14 10:52:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-01-02 02:23:29 +00:00
|
|
|
if (isset(VALID_CRON_STRINGS[$scheduled_task->frequency])) {
|
|
|
|
$scheduled_task->frequency = VALID_CRON_STRINGS[$scheduled_task->frequency];
|
|
|
|
}
|
|
|
|
$schedule->job(new ScheduledTaskJob(
|
|
|
|
task: $scheduled_task
|
|
|
|
))->cron($scheduled_task->frequency)->onOneServer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 14:33:48 +00:00
|
|
|
protected function commands(): void
|
|
|
|
{
|
2024-06-10 20:43:34 +00:00
|
|
|
$this->load(__DIR__.'/Commands');
|
2023-03-17 14:33:48 +00:00
|
|
|
|
|
|
|
require base_path('routes/console.php');
|
|
|
|
}
|
2023-07-14 09:27:08 +00:00
|
|
|
}
|