|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Jobs; |
| 4 | + |
| 5 | +use App\Models\Series; |
| 6 | +use App\Models\User; |
| 7 | +use App\Settings\GeneralSettings; |
| 8 | +use Filament\Notifications\Notification; |
| 9 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 10 | +use Illuminate\Foundation\Queue\Queueable; |
| 11 | +use Illuminate\Support\Facades\Bus; |
| 12 | +use Illuminate\Support\Facades\Log; |
| 13 | + |
| 14 | +/** |
| 15 | + * Checks if there are more series to process and dispatches the next chain if needed. |
| 16 | + * This runs as the last job in a chain, keeping Redis memory usage low. |
| 17 | + */ |
| 18 | +class CheckSeriesImportProgress implements ShouldQueue |
| 19 | +{ |
| 20 | + use Queueable; |
| 21 | + |
| 22 | + public $tries = 1; |
| 23 | + |
| 24 | + /** |
| 25 | + * How many batch jobs to include in each chain. |
| 26 | + * Lower = less Redis memory, more chains |
| 27 | + * Higher = fewer chains, more Redis memory |
| 28 | + */ |
| 29 | + public const JOBS_PER_CHAIN = 10; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + public int $currentOffset, |
| 33 | + public int $totalSeries, |
| 34 | + public bool $notify = true, |
| 35 | + public bool $all_playlists = false, |
| 36 | + public ?int $playlist_id = null, |
| 37 | + public bool $overwrite_existing = false, |
| 38 | + public ?int $user_id = null, |
| 39 | + public ?bool $sync_stream_files = true, |
| 40 | + public ?float $startedAt = null, |
| 41 | + ) { |
| 42 | + // Track start time on first checker |
| 43 | + if ($this->startedAt === null) { |
| 44 | + $this->startedAt = microtime(true); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public function handle(GeneralSettings $settings): void |
| 49 | + { |
| 50 | + $batchSize = ProcessM3uImportSeriesEpisodes::BATCH_SIZE; |
| 51 | + $seriesProcessed = $this->currentOffset; |
| 52 | + $seriesRemaining = $this->totalSeries - $seriesProcessed; |
| 53 | + |
| 54 | + Log::info('Series Import: Progress check', [ |
| 55 | + 'processed' => $seriesProcessed, |
| 56 | + 'total' => $this->totalSeries, |
| 57 | + 'remaining' => $seriesRemaining, |
| 58 | + 'progress_pct' => round(($seriesProcessed / $this->totalSeries) * 100, 1), |
| 59 | + ]); |
| 60 | + |
| 61 | + if ($seriesRemaining <= 0) { |
| 62 | + // All done! Trigger STRM sync if needed |
| 63 | + Log::info('Series Import: All metadata batches complete', [ |
| 64 | + 'total_series' => $this->totalSeries, |
| 65 | + ]); |
| 66 | + |
| 67 | + if ($this->sync_stream_files && $settings->stream_file_sync_enabled) { |
| 68 | + Log::info('Series Import: Dispatching STRM sync'); |
| 69 | + dispatch(new SyncSeriesStrmFiles( |
| 70 | + series: null, |
| 71 | + notify: true, |
| 72 | + all_playlists: $this->all_playlists, |
| 73 | + playlist_id: $this->playlist_id, |
| 74 | + user_id: $this->user_id, |
| 75 | + )); |
| 76 | + } |
| 77 | + |
| 78 | + // Send completion notification |
| 79 | + if ($this->notify && $this->user_id) { |
| 80 | + $user = User::find($this->user_id); |
| 81 | + if ($user) { |
| 82 | + $duration = round(microtime(true) - $this->startedAt, 2); |
| 83 | + $minutes = floor($duration / 60); |
| 84 | + $seconds = $duration % 60; |
| 85 | + $timeStr = $minutes > 0 |
| 86 | + ? "{$minutes} minute(s) and " . round($seconds, 0) . " second(s)" |
| 87 | + : round($seconds, 1) . " second(s)"; |
| 88 | + |
| 89 | + Notification::make() |
| 90 | + ->success() |
| 91 | + ->title('Series Metadata Sync Complete') |
| 92 | + ->body("Successfully processed {$this->totalSeries} series in {$timeStr}.") |
| 93 | + ->broadcast($user) |
| 94 | + ->sendToDatabase($user); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + // More series to process - dispatch next chain |
| 102 | + $jobs = []; |
| 103 | + $jobsInThisChain = min(self::JOBS_PER_CHAIN, (int) ceil($seriesRemaining / $batchSize)); |
| 104 | + |
| 105 | + for ($i = 0; $i < $jobsInThisChain; $i++) { |
| 106 | + $offset = $seriesProcessed + ($i * $batchSize); |
| 107 | + $batchNumber = (int) floor($offset / $batchSize) + 1; |
| 108 | + $totalBatches = (int) ceil($this->totalSeries / $batchSize); |
| 109 | + |
| 110 | + $jobs[] = new ProcessM3uImportSeriesEpisodes( |
| 111 | + playlistSeries: null, |
| 112 | + notify: false, |
| 113 | + all_playlists: $this->all_playlists, |
| 114 | + playlist_id: $this->playlist_id, |
| 115 | + overwrite_existing: $this->overwrite_existing, |
| 116 | + user_id: $this->user_id, |
| 117 | + sync_stream_files: false, // Don't trigger per-job STRM sync |
| 118 | + batchOffset: $offset, |
| 119 | + totalBatches: $totalBatches, |
| 120 | + currentBatch: $batchNumber, |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + // Add checker as last job in chain |
| 125 | + $nextOffset = $seriesProcessed + ($jobsInThisChain * $batchSize); |
| 126 | + $jobs[] = new self( |
| 127 | + currentOffset: $nextOffset, |
| 128 | + totalSeries: $this->totalSeries, |
| 129 | + notify: $this->notify, |
| 130 | + all_playlists: $this->all_playlists, |
| 131 | + playlist_id: $this->playlist_id, |
| 132 | + overwrite_existing: $this->overwrite_existing, |
| 133 | + user_id: $this->user_id, |
| 134 | + sync_stream_files: $this->sync_stream_files, |
| 135 | + startedAt: $this->startedAt, |
| 136 | + ); |
| 137 | + |
| 138 | + Log::info('Series Import: Dispatching next chain', [ |
| 139 | + 'jobs_in_chain' => $jobsInThisChain, |
| 140 | + 'next_offset' => $nextOffset, |
| 141 | + 'series_processed_after_chain' => $nextOffset, |
| 142 | + ]); |
| 143 | + |
| 144 | + Bus::chain($jobs)->dispatch(); |
| 145 | + } |
| 146 | +} |
0 commit comments