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