2023-03-17 14:33:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Console;
|
|
|
|
|
|
2024-10-25 09:40:59 +00:00
|
|
|
use App\Jobs\CheckAndStartSentinelJob;
|
2024-08-06 12:36:56 +00:00
|
|
|
use App\Jobs\CheckForUpdatesJob;
|
2024-10-21 10:06:13 +00:00
|
|
|
use App\Jobs\CheckHelperImageJob;
|
2023-08-15 12:11:38 +00:00
|
|
|
use App\Jobs\CleanupInstanceStuffsJob;
|
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-05-30 10:56:29 +00:00
|
|
|
use App\Jobs\PullTemplatesFromCDN;
|
2025-02-05 21:11:10 +00:00
|
|
|
use App\Jobs\RegenerateSslCertJob;
|
2024-06-10 20:43:34 +00:00
|
|
|
use App\Jobs\ScheduledTaskJob;
|
2024-08-05 13:48:15 +00:00
|
|
|
use App\Jobs\ServerCheckJob;
|
feat(server): implement server patch check notifications
- Added a new job, ServerPatchCheckJob, to handle server patch checks and notifications.
- Introduced a new notification class, ServerPatchCheck, for sending updates via email, Discord, Slack, Pushover, and Telegram.
- Updated notification settings models to include server patch notification options for email, Discord, Slack, Pushover, and Telegram.
- Created a migration to add server patch notification fields to the respective settings tables.
- Enhanced the UI to allow users to enable/disable server patch notifications across different channels.
2025-05-26 12:03:59 +00:00
|
|
|
use App\Jobs\ServerPatchCheckJob;
|
2024-11-03 08:10:36 +00:00
|
|
|
use App\Jobs\ServerStorageCheckJob;
|
2024-08-05 18:05:38 +00:00
|
|
|
use App\Jobs\UpdateCoolifyJob;
|
2024-11-02 11:38:22 +00:00
|
|
|
use App\Models\InstanceSettings;
|
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;
|
2025-07-11 13:10:55 +00:00
|
|
|
use Cron\CronExpression;
|
2023-03-17 14:33:48 +00:00
|
|
|
use Illuminate\Console\Scheduling\Schedule;
|
|
|
|
|
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
2024-10-15 11:39:19 +00:00
|
|
|
use Illuminate\Support\Carbon;
|
2025-02-15 12:21:23 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-03-17 14:33:48 +00:00
|
|
|
|
|
|
|
|
class Kernel extends ConsoleKernel
|
|
|
|
|
{
|
2024-10-25 09:40:59 +00:00
|
|
|
private $allServers;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-11-14 09:56:56 +00:00
|
|
|
private Schedule $scheduleInstance;
|
|
|
|
|
|
2024-11-02 11:38:22 +00:00
|
|
|
private InstanceSettings $settings;
|
|
|
|
|
|
2024-11-06 14:16:12 +00:00
|
|
|
private string $updateCheckFrequency;
|
|
|
|
|
|
|
|
|
|
private string $instanceTimezone;
|
|
|
|
|
|
2023-03-17 14:33:48 +00:00
|
|
|
protected function schedule(Schedule $schedule): void
|
|
|
|
|
{
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->scheduleInstance = $schedule;
|
2024-11-03 13:18:43 +00:00
|
|
|
$this->allServers = Server::where('ip', '!=', '1.2.3.4');
|
2024-11-02 11:38:22 +00:00
|
|
|
|
|
|
|
|
$this->settings = instanceSettings();
|
2024-11-06 14:16:12 +00:00
|
|
|
$this->updateCheckFrequency = $this->settings->update_check_frequency ?: '0 * * * *';
|
2024-11-14 09:56:56 +00:00
|
|
|
|
2024-11-06 14:16:12 +00:00
|
|
|
$this->instanceTimezone = $this->settings->instance_timezone ?: config('app.timezone');
|
2024-08-05 14:31:41 +00:00
|
|
|
|
2024-11-14 09:56:56 +00:00
|
|
|
if (validate_timezone($this->instanceTimezone) === false) {
|
|
|
|
|
$this->instanceTimezone = config('app.timezone');
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-23 22:38:31 +00:00
|
|
|
// $this->scheduleInstance->job(new CleanupStaleMultiplexedConnections)->hourly();
|
2025-07-08 08:57:09 +00:00
|
|
|
$this->scheduleInstance->command('cleanup:redis')->weekly();
|
2024-09-08 17:37:00 +00:00
|
|
|
|
2023-08-27 13:23:47 +00:00
|
|
|
if (isDev()) {
|
2023-10-26 09:38:37 +00:00
|
|
|
// Instance Jobs
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->scheduleInstance->command('horizon:snapshot')->everyMinute();
|
|
|
|
|
$this->scheduleInstance->job(new CleanupInstanceStuffsJob)->everyMinute()->onOneServer();
|
|
|
|
|
$this->scheduleInstance->job(new CheckHelperImageJob)->everyTenMinutes()->onOneServer();
|
2024-11-03 08:02:14 +00:00
|
|
|
|
2023-10-26 09:38:37 +00:00
|
|
|
// Server Jobs
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->checkResources();
|
2024-11-03 08:02:14 +00:00
|
|
|
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->checkScheduledBackups();
|
|
|
|
|
$this->checkScheduledTasks();
|
2024-11-03 08:02:14 +00:00
|
|
|
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->scheduleInstance->command('uploads:clear')->everyTwoMinutes();
|
2024-09-08 17:37:00 +00:00
|
|
|
|
2023-06-06 06:43:01 +00:00
|
|
|
} else {
|
2023-10-26 09:38:37 +00:00
|
|
|
// Instance Jobs
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->scheduleInstance->command('horizon:snapshot')->everyFiveMinutes();
|
|
|
|
|
$this->scheduleInstance->command('cleanup:unreachable-servers')->daily()->onOneServer();
|
|
|
|
|
|
|
|
|
|
$this->scheduleInstance->job(new PullTemplatesFromCDN)->cron($this->updateCheckFrequency)->timezone($this->instanceTimezone)->onOneServer();
|
|
|
|
|
|
|
|
|
|
$this->scheduleInstance->job(new CleanupInstanceStuffsJob)->everyTwoMinutes()->onOneServer();
|
|
|
|
|
$this->scheduleUpdates();
|
2023-10-26 09:38:37 +00:00
|
|
|
|
|
|
|
|
// Server Jobs
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->checkResources();
|
2024-11-03 08:02:14 +00:00
|
|
|
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->pullImages();
|
2024-11-03 08:02:14 +00:00
|
|
|
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->checkScheduledBackups();
|
|
|
|
|
$this->checkScheduledTasks();
|
2024-03-04 11:17:33 +00:00
|
|
|
|
2025-02-05 21:11:10 +00:00
|
|
|
$this->scheduleInstance->job(new RegenerateSslCertJob)->twiceDaily();
|
|
|
|
|
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->scheduleInstance->command('cleanup:database --yes')->daily();
|
|
|
|
|
$this->scheduleInstance->command('uploads:clear')->everyTwoMinutes();
|
2023-10-26 09:38:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-11-14 09:56:56 +00:00
|
|
|
private function pullImages(): void
|
2023-10-26 09:38:37 +00:00
|
|
|
{
|
2025-01-10 10:54:45 +00:00
|
|
|
if (isCloud()) {
|
|
|
|
|
$servers = $this->allServers->whereRelation('team.subscription', 'stripe_invoice_paid', true)->whereRelation('settings', 'is_usable', true)->whereRelation('settings', 'is_reachable', true)->get();
|
2025-01-13 08:17:54 +00:00
|
|
|
$own = Team::find(0)->servers;
|
|
|
|
|
$servers = $servers->merge($own);
|
2025-01-10 10:54:45 +00:00
|
|
|
} else {
|
|
|
|
|
$servers = $this->allServers->whereRelation('settings', 'is_usable', true)->whereRelation('settings', 'is_reachable', true)->get();
|
|
|
|
|
}
|
2024-11-02 11:38:22 +00:00
|
|
|
foreach ($servers as $server) {
|
2025-02-15 12:21:23 +00:00
|
|
|
try {
|
|
|
|
|
if ($server->isSentinelEnabled()) {
|
|
|
|
|
$this->scheduleInstance->job(function () use ($server) {
|
|
|
|
|
CheckAndStartSentinelJob::dispatch($server);
|
|
|
|
|
})->cron($this->updateCheckFrequency)->timezone($this->instanceTimezone)->onOneServer();
|
|
|
|
|
}
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('Error pulling images: '.$e->getMessage());
|
2024-05-10 07:21:19 +00:00
|
|
|
}
|
2024-08-05 14:31:41 +00:00
|
|
|
}
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->scheduleInstance->job(new CheckHelperImageJob)
|
2024-11-06 14:16:12 +00:00
|
|
|
->cron($this->updateCheckFrequency)
|
|
|
|
|
->timezone($this->instanceTimezone)
|
2024-09-19 09:24:21 +00:00
|
|
|
->onOneServer();
|
2024-08-05 14:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-14 09:56:56 +00:00
|
|
|
private function scheduleUpdates(): void
|
2024-08-05 14:31:41 +00:00
|
|
|
{
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->scheduleInstance->job(new CheckForUpdatesJob)
|
2024-11-06 14:16:12 +00:00
|
|
|
->cron($this->updateCheckFrequency)
|
|
|
|
|
->timezone($this->instanceTimezone)
|
2024-08-16 14:01:41 +00:00
|
|
|
->onOneServer();
|
2024-08-05 14:31:41 +00:00
|
|
|
|
2024-11-02 11:38:22 +00:00
|
|
|
if ($this->settings->is_auto_update_enabled) {
|
|
|
|
|
$autoUpdateFrequency = $this->settings->auto_update_frequency;
|
2024-11-14 09:56:56 +00:00
|
|
|
$this->scheduleInstance->job(new UpdateCoolifyJob)
|
2024-08-16 14:01:41 +00:00
|
|
|
->cron($autoUpdateFrequency)
|
2024-11-06 14:16:12 +00:00
|
|
|
->timezone($this->instanceTimezone)
|
2024-08-16 14:01:41 +00:00
|
|
|
->onOneServer();
|
2023-09-29 12:26:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-11-14 09:56:56 +00:00
|
|
|
private function checkResources(): void
|
2024-08-06 12:03:41 +00:00
|
|
|
{
|
|
|
|
|
if (isCloud()) {
|
2025-01-10 10:54:45 +00:00
|
|
|
$servers = $this->allServers->whereRelation('team.subscription', 'stripe_invoice_paid', true)->get();
|
2024-08-06 12:03:41 +00:00
|
|
|
$own = Team::find(0)->servers;
|
|
|
|
|
$servers = $servers->merge($own);
|
|
|
|
|
} else {
|
2024-11-03 13:18:43 +00:00
|
|
|
$servers = $this->allServers->get();
|
2024-08-06 12:03:41 +00:00
|
|
|
}
|
2024-11-03 08:02:14 +00:00
|
|
|
|
2024-08-06 12:03:41 +00:00
|
|
|
foreach ($servers as $server) {
|
2025-02-15 12:21:23 +00:00
|
|
|
try {
|
|
|
|
|
$serverTimezone = data_get($server->settings, 'server_timezone', $this->instanceTimezone);
|
|
|
|
|
if (validate_timezone($serverTimezone) === false) {
|
|
|
|
|
$serverTimezone = config('app.timezone');
|
2024-11-25 13:37:44 +00:00
|
|
|
}
|
2024-11-03 08:02:14 +00:00
|
|
|
|
2025-02-15 12:21:23 +00:00
|
|
|
// Sentinel check
|
|
|
|
|
$lastSentinelUpdate = $server->sentinel_updated_at;
|
|
|
|
|
if (Carbon::parse($lastSentinelUpdate)->isBefore(now()->subSeconds($server->waitBeforeDoingSshCheck()))) {
|
|
|
|
|
// Check container status every minute if Sentinel does not activated
|
|
|
|
|
if (isCloud()) {
|
|
|
|
|
$this->scheduleInstance->job(new ServerCheckJob($server))->timezone($serverTimezone)->everyFiveMinutes()->onOneServer();
|
|
|
|
|
} else {
|
|
|
|
|
$this->scheduleInstance->job(new ServerCheckJob($server))->timezone($serverTimezone)->everyMinute()->onOneServer();
|
|
|
|
|
}
|
|
|
|
|
// $this->scheduleInstance->job(new \App\Jobs\ServerCheckNewJob($server))->everyFiveMinutes()->onOneServer();
|
|
|
|
|
|
|
|
|
|
$serverDiskUsageCheckFrequency = data_get($server->settings, 'server_disk_usage_check_frequency', '0 * * * *');
|
|
|
|
|
if (isset(VALID_CRON_STRINGS[$serverDiskUsageCheckFrequency])) {
|
|
|
|
|
$serverDiskUsageCheckFrequency = VALID_CRON_STRINGS[$serverDiskUsageCheckFrequency];
|
|
|
|
|
}
|
|
|
|
|
$this->scheduleInstance->job(new ServerStorageCheckJob($server))->cron($serverDiskUsageCheckFrequency)->timezone($serverTimezone)->onOneServer();
|
|
|
|
|
}
|
2024-11-03 08:10:36 +00:00
|
|
|
|
2025-02-15 12:21:23 +00:00
|
|
|
$dockerCleanupFrequency = data_get($server->settings, 'docker_cleanup_frequency', '0 * * * *');
|
|
|
|
|
if (isset(VALID_CRON_STRINGS[$dockerCleanupFrequency])) {
|
|
|
|
|
$dockerCleanupFrequency = VALID_CRON_STRINGS[$dockerCleanupFrequency];
|
|
|
|
|
}
|
|
|
|
|
$this->scheduleInstance->job(new DockerCleanupJob($server))->cron($dockerCleanupFrequency)->timezone($serverTimezone)->onOneServer();
|
2024-12-17 14:20:06 +00:00
|
|
|
|
feat(server): implement server patch check notifications
- Added a new job, ServerPatchCheckJob, to handle server patch checks and notifications.
- Introduced a new notification class, ServerPatchCheck, for sending updates via email, Discord, Slack, Pushover, and Telegram.
- Updated notification settings models to include server patch notification options for email, Discord, Slack, Pushover, and Telegram.
- Created a migration to add server patch notification fields to the respective settings tables.
- Enhanced the UI to allow users to enable/disable server patch notifications across different channels.
2025-05-26 12:03:59 +00:00
|
|
|
// Server patch check - weekly
|
|
|
|
|
$this->scheduleInstance->job(new ServerPatchCheckJob($server))->weekly()->timezone($serverTimezone)->onOneServer();
|
|
|
|
|
|
2025-02-15 12:21:23 +00:00
|
|
|
// Cleanup multiplexed connections every hour
|
|
|
|
|
// $this->scheduleInstance->job(new ServerCleanupMux($server))->hourly()->onOneServer();
|
2024-10-29 09:28:05 +00:00
|
|
|
|
2025-02-15 12:21:23 +00:00
|
|
|
// Temporary solution until we have better memory management for Sentinel
|
|
|
|
|
if ($server->isSentinelEnabled()) {
|
|
|
|
|
$this->scheduleInstance->job(function () use ($server) {
|
|
|
|
|
$server->restartContainer('coolify-sentinel');
|
|
|
|
|
})->daily()->onOneServer();
|
|
|
|
|
}
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('Error checking resources: '.$e->getMessage());
|
2024-10-29 09:28:05 +00:00
|
|
|
}
|
2023-09-29 12:26:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-11-14 09:56:56 +00:00
|
|
|
private function checkScheduledBackups(): void
|
2023-08-08 15:28:36 +00:00
|
|
|
{
|
2024-11-03 13:18:43 +00:00
|
|
|
$scheduled_backups = ScheduledDatabaseBackup::where('enabled', true)->get();
|
2025-07-11 13:10:55 +00:00
|
|
|
|
2023-08-08 15:28:36 +00:00
|
|
|
if ($scheduled_backups->isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-01-10 10:54:45 +00:00
|
|
|
$finalScheduledBackups = collect();
|
2023-08-08 15:28:36 +00:00
|
|
|
foreach ($scheduled_backups as $scheduled_backup) {
|
2025-01-10 10:54:45 +00:00
|
|
|
if (blank(data_get($scheduled_backup, 'database'))) {
|
2023-09-09 11:18:49 +00:00
|
|
|
$scheduled_backup->delete();
|
2024-08-26 13:26:08 +00:00
|
|
|
|
2023-09-09 11:18:49 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2024-08-16 14:01:41 +00:00
|
|
|
$server = $scheduled_backup->server();
|
2025-01-10 10:54:45 +00:00
|
|
|
if (blank($server)) {
|
|
|
|
|
$scheduled_backup->delete();
|
2024-09-06 14:42:12 +00:00
|
|
|
|
2024-09-06 14:51:15 +00:00
|
|
|
continue;
|
2024-09-06 14:42:12 +00:00
|
|
|
}
|
2025-01-10 10:54:45 +00:00
|
|
|
if ($server->isFunctional() === false) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-01-13 08:17:54 +00:00
|
|
|
if (isCloud() && data_get($server->team->subscription, 'stripe_invoice_paid', false) === false && $server->team->id !== 0) {
|
2025-01-10 10:54:45 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$finalScheduledBackups->push($scheduled_backup);
|
|
|
|
|
}
|
2024-08-16 14:01:41 +00:00
|
|
|
|
2025-01-10 10:54:45 +00:00
|
|
|
foreach ($finalScheduledBackups as $scheduled_backup) {
|
2025-02-15 12:21:23 +00:00
|
|
|
try {
|
|
|
|
|
if (isset(VALID_CRON_STRINGS[$scheduled_backup->frequency])) {
|
|
|
|
|
$scheduled_backup->frequency = VALID_CRON_STRINGS[$scheduled_backup->frequency];
|
|
|
|
|
}
|
|
|
|
|
$server = $scheduled_backup->server();
|
|
|
|
|
$serverTimezone = data_get($server->settings, 'server_timezone', $this->instanceTimezone);
|
2025-01-10 10:54:45 +00:00
|
|
|
|
2025-02-15 12:21:23 +00:00
|
|
|
if (validate_timezone($serverTimezone) === false) {
|
|
|
|
|
$serverTimezone = config('app.timezone');
|
|
|
|
|
}
|
2025-01-10 10:54:45 +00:00
|
|
|
|
2025-02-15 12:21:23 +00:00
|
|
|
if (isset(VALID_CRON_STRINGS[$scheduled_backup->frequency])) {
|
|
|
|
|
$scheduled_backup->frequency = VALID_CRON_STRINGS[$scheduled_backup->frequency];
|
|
|
|
|
}
|
|
|
|
|
$serverTimezone = data_get($server->settings, 'server_timezone', $this->instanceTimezone);
|
2025-07-11 13:10:55 +00:00
|
|
|
|
|
|
|
|
// Check if the backup should run now
|
|
|
|
|
$cron = new CronExpression($scheduled_backup->frequency);
|
|
|
|
|
$now = Carbon::now($serverTimezone);
|
|
|
|
|
|
|
|
|
|
if ($cron->isDue($now)) {
|
|
|
|
|
Log::channel('scheduled')->info('Backup job running now', [
|
|
|
|
|
'backup_id' => $scheduled_backup->id,
|
|
|
|
|
'backup_name' => $scheduled_backup->name ?? 'unnamed',
|
|
|
|
|
'server_name' => $server->name,
|
|
|
|
|
'frequency' => $scheduled_backup->frequency,
|
|
|
|
|
'timezone' => $serverTimezone,
|
|
|
|
|
'current_time' => $now->format('Y-m-d H:i:s T'),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-15 12:21:23 +00:00
|
|
|
$this->scheduleInstance->job(new DatabaseBackupJob(
|
|
|
|
|
backup: $scheduled_backup
|
|
|
|
|
))->cron($scheduled_backup->frequency)->timezone($serverTimezone)->onOneServer();
|
2025-07-11 13:10:55 +00:00
|
|
|
|
2025-02-15 12:21:23 +00:00
|
|
|
} catch (\Exception $e) {
|
2025-07-11 13:10:55 +00:00
|
|
|
Log::channel('scheduled-errors')->error('Error scheduling backup', [
|
|
|
|
|
'backup_id' => $scheduled_backup->id,
|
|
|
|
|
'backup_name' => $scheduled_backup->name ?? 'unnamed',
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'trace' => $e->getTraceAsString(),
|
|
|
|
|
]);
|
2025-02-15 12:21:23 +00:00
|
|
|
Log::error('Error scheduling backup: '.$e->getMessage());
|
|
|
|
|
Log::error($e->getTraceAsString());
|
2023-08-09 15:57:27 +00:00
|
|
|
}
|
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-11-14 09:56:56 +00:00
|
|
|
private function checkScheduledTasks(): void
|
2024-01-16 14:19:14 +00:00
|
|
|
{
|
2024-11-06 11:46:28 +00:00
|
|
|
$scheduled_tasks = ScheduledTask::where('enabled', true)->get();
|
2025-07-11 13:10:55 +00:00
|
|
|
|
2024-01-02 02:23:29 +00:00
|
|
|
if ($scheduled_tasks->isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-01-10 10:54:45 +00:00
|
|
|
$finalScheduledTasks = collect();
|
2024-01-02 02:23:29 +00:00
|
|
|
foreach ($scheduled_tasks as $scheduled_task) {
|
2024-02-14 14:14:06 +00:00
|
|
|
$service = $scheduled_task->service;
|
|
|
|
|
$application = $scheduled_task->application;
|
2024-01-02 02:23:29 +00:00
|
|
|
|
2025-01-10 10:54:45 +00:00
|
|
|
$server = $scheduled_task->server();
|
|
|
|
|
if (blank($server)) {
|
2024-01-02 02:23:29 +00:00
|
|
|
$scheduled_task->delete();
|
2024-08-26 13:26:08 +00:00
|
|
|
|
2024-01-02 02:23:29 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2025-01-10 10:54:45 +00:00
|
|
|
|
|
|
|
|
if ($server->isFunctional() === false) {
|
|
|
|
|
continue;
|
2024-05-14 10:52:01 +00:00
|
|
|
}
|
2025-01-10 10:54:45 +00:00
|
|
|
|
2025-01-13 08:17:54 +00:00
|
|
|
if (isCloud() && data_get($server->team->subscription, 'stripe_invoice_paid', false) === false && $server->team->id !== 0) {
|
2025-01-10 10:54:45 +00:00
|
|
|
continue;
|
2024-05-14 10:52:01 +00:00
|
|
|
}
|
2024-08-16 14:01:41 +00:00
|
|
|
|
2025-01-10 10:54:45 +00:00
|
|
|
if (! $service && ! $application) {
|
|
|
|
|
$scheduled_task->delete();
|
|
|
|
|
|
2024-09-06 14:51:15 +00:00
|
|
|
continue;
|
2024-09-06 14:42:12 +00:00
|
|
|
}
|
2024-08-16 14:01:41 +00:00
|
|
|
|
2025-01-10 10:54:45 +00:00
|
|
|
if ($application && str($application->status)->contains('running') === false) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ($service && str($service->status)->contains('running') === false) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$finalScheduledTasks->push($scheduled_task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($finalScheduledTasks as $scheduled_task) {
|
2025-02-15 12:21:23 +00:00
|
|
|
try {
|
|
|
|
|
$server = $scheduled_task->server();
|
|
|
|
|
if (isset(VALID_CRON_STRINGS[$scheduled_task->frequency])) {
|
|
|
|
|
$scheduled_task->frequency = VALID_CRON_STRINGS[$scheduled_task->frequency];
|
|
|
|
|
}
|
|
|
|
|
$serverTimezone = data_get($server->settings, 'server_timezone', $this->instanceTimezone);
|
2025-01-10 10:54:45 +00:00
|
|
|
|
2025-02-15 12:21:23 +00:00
|
|
|
if (validate_timezone($serverTimezone) === false) {
|
|
|
|
|
$serverTimezone = config('app.timezone');
|
|
|
|
|
}
|
2025-07-11 13:10:55 +00:00
|
|
|
|
|
|
|
|
// Check if the task should run now
|
|
|
|
|
$cron = new CronExpression($scheduled_task->frequency);
|
|
|
|
|
$now = Carbon::now($serverTimezone);
|
|
|
|
|
|
|
|
|
|
if ($cron->isDue($now)) {
|
|
|
|
|
Log::channel('scheduled')->info('Task job running now', [
|
|
|
|
|
'task_id' => $scheduled_task->id,
|
|
|
|
|
'task_name' => $scheduled_task->name ?? 'unnamed',
|
|
|
|
|
'server_name' => $server->name,
|
|
|
|
|
'frequency' => $scheduled_task->frequency,
|
|
|
|
|
'timezone' => $serverTimezone,
|
|
|
|
|
'type' => $scheduled_task->service ? 'service' : 'application',
|
|
|
|
|
'current_time' => $now->format('Y-m-d H:i:s T'),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-15 12:21:23 +00:00
|
|
|
$this->scheduleInstance->job(new ScheduledTaskJob(
|
|
|
|
|
task: $scheduled_task
|
|
|
|
|
))->cron($scheduled_task->frequency)->timezone($serverTimezone)->onOneServer();
|
2025-07-11 13:10:55 +00:00
|
|
|
|
2025-02-15 12:21:23 +00:00
|
|
|
} catch (\Exception $e) {
|
2025-07-11 13:10:55 +00:00
|
|
|
Log::channel('scheduled-errors')->error('Error scheduling task', [
|
|
|
|
|
'task_id' => $scheduled_task->id,
|
|
|
|
|
'task_name' => $scheduled_task->name ?? 'unnamed',
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'trace' => $e->getTraceAsString(),
|
|
|
|
|
]);
|
2025-02-15 12:21:23 +00:00
|
|
|
Log::error('Error scheduling task: '.$e->getMessage());
|
|
|
|
|
Log::error($e->getTraceAsString());
|
2025-01-10 10:54:45 +00:00
|
|
|
}
|
2024-01-02 02:23:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-17 14:33:48 +00:00
|
|
|
protected function commands(): void
|
|
|
|
|
{
|
2024-08-26 13:26:08 +00:00
|
|
|
$this->load(__DIR__.'/Commands');
|
2023-03-17 14:33:48 +00:00
|
|
|
|
|
|
|
|
require base_path('routes/console.php');
|
|
|
|
|
}
|
2024-08-26 13:26:08 +00:00
|
|
|
}
|