From 0a078adab3baa1d8f2512a9a286fc547489cb2b2 Mon Sep 17 00:00:00 2001 From: Peter Deltchev Date: Wed, 6 Jan 2016 05:33:23 -0800 Subject: [PATCH] #20: Genre tags in files are now updated when a genre is renamed. --- app/Commands/RenameGenreCommand.php | 6 ++ app/Jobs/UpdateTagsForRenamedGenre.php | 97 ++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 app/Jobs/UpdateTagsForRenamedGenre.php diff --git a/app/Commands/RenameGenreCommand.php b/app/Commands/RenameGenreCommand.php index 46d2f53a..bb71aa9b 100644 --- a/app/Commands/RenameGenreCommand.php +++ b/app/Commands/RenameGenreCommand.php @@ -21,12 +21,16 @@ namespace Poniverse\Ponyfm\Commands; use Gate; +use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Support\Str; +use Poniverse\Ponyfm\Jobs\UpdateTagsForRenamedGenre; use Poniverse\Ponyfm\Models\Genre; use Validator; class RenameGenreCommand extends CommandBase { + use DispatchesJobs; + /** @var Genre */ private $_genre; private $_newName; @@ -72,6 +76,8 @@ class RenameGenreCommand extends CommandBase $this->_genre->slug = $slug; $this->_genre->save(); + $this->dispatch(new UpdateTagsForRenamedGenre($this->_genre)); + return CommandResponse::succeed(['message' => 'Genre renamed!']); } } diff --git a/app/Jobs/UpdateTagsForRenamedGenre.php b/app/Jobs/UpdateTagsForRenamedGenre.php new file mode 100644 index 00000000..27e5b934 --- /dev/null +++ b/app/Jobs/UpdateTagsForRenamedGenre.php @@ -0,0 +1,97 @@ +. + */ + +namespace Poniverse\Ponyfm\Jobs; + +use Auth; +use Cache; +use Log; +use Poniverse\Ponyfm\Models\Genre; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Contracts\Bus\SelfHandling; +use Illuminate\Contracts\Queue\ShouldQueue; +use Poniverse\Ponyfm\Models\Track; +use SerializesModels; + +/** + * Class RenameGenre + * + * NOTE: It is assumed that the genre passed into this job has already been renamed! + * All this job does is update the tags in that genre's tracks. + * + * @package Poniverse\Ponyfm\Jobs + */ +class UpdateTagsForRenamedGenre extends Job implements SelfHandling, ShouldQueue +{ + use InteractsWithQueue, SerializesModels; + + protected $executingUser; + protected $genreThatWasRenamed; + protected $lockKey; + + /** + * Create a new job instance. + * + * @param Genre $genreThatWasRenamed + */ + public function __construct(Genre $genreThatWasRenamed) + { + $this->executingUser = Auth::user(); + $this->genreThatWasRenamed = $genreThatWasRenamed; + + $this->lockKey = "genre-{$this->genreThatWasRenamed->id}-tag-update-lock"; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + // The user who kicked off this job is used when generating revision log entries. + Auth::login($this->executingUser); + + // "Lock" this genre to prevent race conditions + if (Cache::has($this->lockKey)) { + Log::info("Tag updates for the \"{$this->genreThatWasRenamed->name}\" genre are currently in progress! Will try again in 30 seconds."); + $this->release(30); + return; + + } else { + Cache::forever($this->lockKey, true); + } + + + $this->genreThatWasRenamed->tracks()->chunk(200, function ($tracks) { + foreach ($tracks as $track) { + /** @var Track $track */ + $track->updateTags(); + } + }); + + Cache::forget($this->lockKey); + } + + public function failed() + { + Cache::forget($this->lockKey); + } +}