lasthourcloud/app/Console/Kernel.php

115 lines
4.0 KiB
PHP
Raw Normal View History

2023-03-17 14:33:48 +00:00
<?php
namespace App\Console;
2023-07-14 09:27:08 +00:00
use App\Jobs\CheckResaleLicenseJob;
2023-08-15 12:11:38 +00:00
use App\Jobs\CleanupInstanceStuffsJob;
use App\Jobs\DatabaseBackupJob;
use App\Jobs\DockerCleanupJob;
2023-05-25 12:23:49 +00:00
use App\Jobs\InstanceAutoUpdateJob;
2023-09-14 10:45:50 +00:00
use App\Jobs\ContainerStatusJob;
use App\Jobs\PullHelperImageJob;
2023-09-01 14:30:50 +00:00
use App\Models\InstanceSettings;
2023-08-08 15:28:36 +00:00
use App\Models\ScheduledDatabaseBackup;
2023-09-11 20:29:34 +00:00
use App\Models\Server;
2023-03-17 14:33:48 +00:00
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule): void
{
if (isDev()) {
// 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();
// 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);
$this->cleanup_servers($schedule);
2023-10-17 17:00:23 +00:00
$this->check_scheduled_backups($schedule);
$this->pull_helper_image($schedule);
2023-06-06 06:43:01 +00:00
} else {
// Instance Jobs
2023-06-06 06:43:01 +00:00
$schedule->command('horizon:snapshot')->everyFiveMinutes();
2023-09-15 09:19:36 +00:00
$schedule->job(new CleanupInstanceStuffsJob)->everyTwoMinutes()->onOneServer();
2023-11-02 10:45:43 +00:00
// $schedule->job(new CheckResaleLicenseJob)->hourly()->onOneServer();
// Server Jobs
2023-09-12 13:47:30 +00:00
$this->instance_auto_update($schedule);
$this->check_scheduled_backups($schedule);
$this->check_resources($schedule);
$this->cleanup_servers($schedule);
$this->pull_helper_image($schedule);
}
}
private function pull_helper_image($schedule)
{
$servers = Server::all()->where('settings.is_usable', true)->where('settings.is_reachable', true);
foreach ($servers as $server) {
$schedule->job(new PullHelperImageJob($server))->everyTenMinutes()->onOneServer();
}
}
private function cleanup_servers($schedule)
{
$servers = Server::all()->where('settings.is_usable', true)->where('settings.is_reachable', true);
foreach ($servers as $server) {
$schedule->job(new DockerCleanupJob($server))->everyTenMinutes()->onOneServer();
2023-09-11 20:29:34 +00:00
}
2023-09-02 13:53:12 +00:00
}
private function check_resources($schedule)
{
if (isCloud()) {
$servers = Server::all()->whereNotNull('team.subscription')->where('team.subscription.stripe_trial_already_ended', false);
} else {
$servers = Server::all();
}
2023-09-14 10:45:50 +00:00
foreach ($servers as $server) {
$schedule->job(new ContainerStatusJob($server))->everyMinute()->onOneServer();
2023-09-02 13:53:12 +00:00
}
2023-08-08 15:28:36 +00:00
}
2023-09-11 20:29:34 +00:00
private function instance_auto_update($schedule)
{
2023-09-01 14:30:50 +00:00
if (isDev()) {
return;
}
$settings = InstanceSettings::get();
if ($settings->is_auto_update_enabled) {
$schedule->job(new InstanceAutoUpdateJob)->everyTenMinutes()->onOneServer();
}
}
2023-08-08 15:28:36 +00: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 15:18:50 +00:00
if (!$scheduled_backup->enabled) {
continue;
}
2023-09-11 20:29:34 +00:00
if (is_null(data_get($scheduled_backup, 'database'))) {
ray('database not found');
$scheduled_backup->delete();
continue;
}
2023-08-16 15:18:50 +00:00
if (isset(VALID_CRON_STRINGS[$scheduled_backup->frequency])) {
$scheduled_backup->frequency = VALID_CRON_STRINGS[$scheduled_backup->frequency];
}
$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-03-17 14:33:48 +00:00
protected function commands(): void
{
2023-04-28 07:00:47 +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
}