From 0eb96f0e832e7f31263bed67990c408b99aed9e8 Mon Sep 17 00:00:00 2001 From: Peter Deltchev Date: Mon, 4 Jan 2016 14:55:20 -0800 Subject: [PATCH] Fixed track encodes. They work again! Also implemented a track re-encoding command. --- app/Commands/UploadTrackCommand.php | 2 +- app/Console/Commands/RebuildTrack.php | 83 +++++++++++++++++++++++++++ app/Console/Kernel.php | 1 + app/Jobs/EncodeTrackFile.php | 23 ++------ app/Models/Track.php | 4 +- app/Models/TrackFile.php | 2 +- 6 files changed, 93 insertions(+), 22 deletions(-) create mode 100644 app/Console/Commands/RebuildTrack.php diff --git a/app/Commands/UploadTrackCommand.php b/app/Commands/UploadTrackCommand.php index 59b53cbe..1440203e 100644 --- a/app/Commands/UploadTrackCommand.php +++ b/app/Commands/UploadTrackCommand.php @@ -235,7 +235,7 @@ class UploadTrackCommand extends CommandBase $trackFile = new TrackFile(); $trackFile->is_master = $name === 'FLAC' ? true : false; $trackFile->format = $name; - $trackFile->status = TrackFile::STATUS_PROCESSING; + $trackFile->status = TrackFile::STATUS_PROCESSING_PENDING; if (in_array($name, Track::$CacheableFormats) && $trackFile->is_master == false) { $trackFile->is_cacheable = true; diff --git a/app/Console/Commands/RebuildTrack.php b/app/Console/Commands/RebuildTrack.php new file mode 100644 index 00000000..25ce13c6 --- /dev/null +++ b/app/Console/Commands/RebuildTrack.php @@ -0,0 +1,83 @@ +. + */ + +namespace Poniverse\Ponyfm\Console\Commands; + +use Illuminate\Console\Command; +use Illuminate\Foundation\Bus\DispatchesJobs; +use Poniverse\Ponyfm\Jobs\EncodeTrackFile; +use Poniverse\Ponyfm\Models\Track; + +class RebuildTrack extends Command +{ + use DispatchesJobs; + + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'rebuild:track + {trackId : ID of the track to rebuild} + {--upload : Include this option to use the uploaded file as the encode source instead of the master file}'; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Re-encodes a track\'s files'; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + /** @var Track $track */ + $track = Track::with('trackFiles')->find((int) $this->argument('trackId')); + + if($this->option('upload')) { + foreach($track->trackFiles as $trackFile) { + $this->info("Re-encoding this track's {$trackFile->format} file..."); + $this->dispatch(new EncodeTrackFile($trackFile, false, true, false)); + } + + } else { + foreach ($track->trackFiles as $trackFile) { + if (!$trackFile->is_master) { + $this->info("Re-encoding this track's {$trackFile->format} file..."); + $this->dispatch(new EncodeTrackFile($trackFile, true)); + } + } + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index dd8f9de5..9e9771a8 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -43,6 +43,7 @@ class Kernel extends ConsoleKernel \Poniverse\Ponyfm\Console\Commands\PoniverseApiSetup::class, \Poniverse\Ponyfm\Console\Commands\ClearTrackCache::class, \Poniverse\Ponyfm\Console\Commands\RebuildTrackCache::class, + \Poniverse\Ponyfm\Console\Commands\RebuildTrack::class, \Poniverse\Ponyfm\Console\Commands\RebuildFilesizes::class, \Poniverse\Ponyfm\Console\Commands\MergeDuplicateAccounts::class, ]; diff --git a/app/Jobs/EncodeTrackFile.php b/app/Jobs/EncodeTrackFile.php index 24755e86..b56270d0 100644 --- a/app/Jobs/EncodeTrackFile.php +++ b/app/Jobs/EncodeTrackFile.php @@ -42,19 +42,19 @@ class EncodeTrackFile extends Job implements SelfHandling, ShouldQueue /** * @var TrackFile */ - private $trackFile; + protected $trackFile; /** * @var */ - private $isExpirable; + protected $isExpirable; /** * @var bool */ - private $isForUpload; + protected $isForUpload; /** * @var bool */ - private $autoPublishWhenComplete; + protected $autoPublishWhenComplete; /** * Create a new job instance. @@ -72,23 +72,10 @@ class EncodeTrackFile extends Job implements SelfHandling, ShouldQueue throw new InvalidEncodeOptionsException("Master files cannot be encoded unless we're generating a lossless master file during the upload process."); } - // don't start this job if the file is already being processed or if it's still valid - if ( - in_array($trackFile->status, [TrackFile::STATUS_PROCESSING_PENDING, TrackFile::STATUS_PROCESSING]) || - !$trackFile->is_expired - ) { - $this->delete(); - return; - } - $this->trackFile = $trackFile; $this->isExpirable = $isExpirable; $this->isForUpload = $isForUpload; $this->autoPublishWhenComplete = $autoPublish; - - // "lock" this file for processing - $this->trackFile->status = TrackFile::STATUS_PROCESSING_PENDING; - $this->trackFile->save(); } /** @@ -151,7 +138,7 @@ class EncodeTrackFile extends Job implements SelfHandling, ShouldQueue $this->trackFile->track->updateTags($this->trackFile->format); // Insert the expiration time for cached tracks - if ($this->isExpirable) { + if ($this->isExpirable && $this->trackFile->is_cacheable) { $this->trackFile->expires_at = Carbon::now()->addMinutes(Config::get('ponyfm.track_file_cache_duration')); $this->trackFile->save(); } diff --git a/app/Models/Track.php b/app/Models/Track.php index f45172a6..5c7bd285 100644 --- a/app/Models/Track.php +++ b/app/Models/Track.php @@ -698,11 +698,11 @@ class Track extends Model } elseif ( $carry !== static::STATUS_ERROR && - (int) $trackFile->status === TrackFile::STATUS_PROCESSING) { + in_array($trackFile->status, [TrackFile::STATUS_PROCESSING, TrackFile::STATUS_PROCESSING_PENDING])) { return static::STATUS_PROCESSING; } elseif ( - !in_array($carry, [static::STATUS_ERROR, static::STATUS_PROCESSING]) && + !in_array($carry, [static::STATUS_ERROR, static::STATUS_PROCESSING, TrackFile::STATUS_PROCESSING_PENDING]) && (int) $trackFile->status === TrackFile::STATUS_NOT_BEING_PROCESSED ) { return static::STATUS_COMPLETE; diff --git a/app/Models/TrackFile.php b/app/Models/TrackFile.php index 1eac4f80..77521cd0 100644 --- a/app/Models/TrackFile.php +++ b/app/Models/TrackFile.php @@ -106,7 +106,7 @@ class TrackFile extends Model } public function getIsExpiredAttribute() { - return $this->expires_at === null || + return $this->attributes['expires_at'] === null || $this->expires_at->isPast(); }