From c651c44d34c7d760a0d1303550fdccfdd9e8d2c9 Mon Sep 17 00:00:00 2001 From: Kelvin Zhang Date: Tue, 27 Oct 2015 17:20:43 +0000 Subject: [PATCH] Add EncodeTrackFile job --- app/Jobs/EncodeTrackFile.php | 128 +++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 app/Jobs/EncodeTrackFile.php diff --git a/app/Jobs/EncodeTrackFile.php b/app/Jobs/EncodeTrackFile.php new file mode 100644 index 00000000..60b3953d --- /dev/null +++ b/app/Jobs/EncodeTrackFile.php @@ -0,0 +1,128 @@ +. + */ + +namespace Poniverse\Ponyfm\Jobs; + +use Carbon\Carbon; +use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\Log; +use OAuth2\Exception; +use Poniverse\Ponyfm\Jobs\Job; +use Illuminate\Queue\SerializesModels; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Contracts\Bus\SelfHandling; +use Illuminate\Contracts\Queue\ShouldQueue; +use Poniverse\Ponyfm\Track; +use Poniverse\Ponyfm\TrackFile; +use Symfony\Component\Process\Exception\ProcessFailedException; +use Symfony\Component\Process\Process; + +class EncodeTrackFile extends Job implements SelfHandling, ShouldQueue +{ + use InteractsWithQueue, SerializesModels; + /** + * @var TrackFile + */ + private $trackFile; + /** + * @var + */ + private $isExpirable; + + /** + * Create a new job instance. + * @param TrackFile $trackFile + * @param $isExpirable + */ + public function __construct(TrackFile $trackFile, $isExpirable) + { + $this->trackFile = $trackFile; + $this->isExpirable = $isExpirable; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + // Start the job + $this->trackFile->in_progress = true; + $this->trackFile->update(); + + // Assign the source to the track's master file + $source = TrackFile::where('track_id', $this->trackFile->track_id) + ->where('is_master', true) + ->first() + ->getFile(); + + // Assign the target + $destination = $this->trackFile->track()->getDirectory(); + $this->trackFile->track()->ensureDirectoryExists(); + $target = $destination . '/' . $this->trackFile->getFilename(); + + // 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()); + // Ensure queue fails + throw $e; + } finally { + Log::info($process->getOutput()); + } + + // Update the tags of the track + $this->trackFile->track()->updateTags(); + + // Insert the expiration time for cached tracks + if ($this->isExpirable) { + $this->trackFile->expiration = Carbon::now()->addMinutes(Config::get('ponyfm.cache_duration')); + $this->trackFile->update(); + } + + // Complete the job + $this->trackFile->in_progress = false; + $this->trackFile->update(); + + } + + /** + * Handle a job failure. + * + * @return void + */ + public function failed() + { + $this->trackFile->in_progress = false; + $this->trackFile->expiration = null; + $this->trackFile->update(); + } +}