/** * Execute the job. * * @return void */ public function handle() { // Sanity-check: was this file just generated, or is it already being processed? if ($this->trackFile->status === TrackFile::STATUS_PROCESSING) { Log::warning('Track file #' . $this->trackFile->id . ' (track #' . $this->trackFile->track_id . ') is already being processed!'); return; } elseif (!$this->trackFile->is_expired) { Log::warning('Track file #' . $this->trackFile->id . ' (track #' . $this->trackFile->track_id . ') is still valid! No need to re-encode it.'); return; } // Start the job $this->trackFile->status = TrackFile::STATUS_PROCESSING; $this->trackFile->save(); // Use the track's master file as the source if ($this->isForUpload) { $source = $this->trackFile->track->getTemporarySourceFile(); } else { $source = TrackFile::where('track_id', $this->trackFile->track_id)->where('is_master', true)->first()->getFile(); } // Assign the target $this->trackFile->track->ensureDirectoryExists(); $target = $this->trackFile->getFile(); // Prepare the command $format = Track::$Formats[$this->trackFile->format]; $command = $format['command']; $command = str_replace('{$source}', '"' . $source . '"', $command); $command = str_replace('{$target}', '"' . $target . '"', $command); Log::info('Encoding track file ' . $this->trackFile->id . ' into ' . $target); // Start a synchronous process to encode the file $process = new Process($command); try { $process->mustRun(); } catch (ProcessFailedException $e) { Log::error('An exception occured in the encoding process for track file ' . $this->trackFile->id . ' - ' . $e->getMessage()); Log::info($process->getOutput()); // Ensure queue fails throw $e; } // Update the tags of the track $this->trackFile->track->updateTags($this->trackFile->format); // Insert the expiration time for cached tracks if ($this->isExpirable && $this->trackFile->is_cacheable) { $this->trackFile->expires_at = Carbon::now()->addMinutes(Config::get('ponyfm.track_file_cache_duration')); $this->trackFile->save(); } // Update file size $this->trackFile->updateFilesize(); // Complete the job $this->trackFile->status = TrackFile::STATUS_NOT_BEING_PROCESSED; $this->trackFile->save(); if ($this->isForUpload) { if (!$this->trackFile->is_master && $this->trackFile->is_cacheable) { File::delete($this->trackFile->getFile()); } // This was the final TrackFile for this track! if ($this->trackFile->track->status === Track::STATUS_COMPLETE) { if ($this->autoPublishWhenComplete) { $this->trackFile->track->published_at = Carbon::now(); DB::table('tracks')->whereUserId($this->trackFile->track->user_id)->update(['is_latest' => false]); $this->trackFile->track->is_latest = true; $this->trackFile->track->save(); } File::delete($this->trackFile->track->getTemporarySourceFile()); } } }