FIX: use distributed mutex to prevent errors when uploading emojis in batches

This commit is contained in:
Régis Hanol 2015-02-09 18:54:57 +01:00
parent f82e8ce5f1
commit 1e6f886886
2 changed files with 33 additions and 15 deletions

View File

@ -13,8 +13,7 @@ class Admin::EmojisController < Admin::AdminController
.gsub(/_{2,}/, '_') .gsub(/_{2,}/, '_')
.downcase .downcase
# check the name doesn't already exist if Emoji.exists?(name)
if Emoji.custom.detect { |e| e.name == name }
render json: failed_json.merge(message: I18n.t("emoji.errors.name_already_exists", name: name)), status: 422 render json: failed_json.merge(message: I18n.t("emoji.errors.name_already_exists", name: name)), status: 422
else else
if emoji = Emoji.create_for(file, name) if emoji = Emoji.create_for(file, name)
@ -28,7 +27,7 @@ class Admin::EmojisController < Admin::AdminController
def destroy def destroy
name = params.require(:id) name = params.require(:id)
Emoji.custom.detect { |e| e.name == name }.try(:remove) Emoji[name].try(:remove)
render nothing: true render nothing: true
end end

View File

@ -1,6 +1,8 @@
class Emoji class Emoji
include ActiveModel::SerializerSupport include ActiveModel::SerializerSupport
EMOJIS_CUSTOM_LOCK ||= "_emojis_custom_lock_".freeze
attr_reader :path attr_reader :path
attr_accessor :name, :url attr_accessor :name, :url
@ -13,11 +15,14 @@ class Emoji
def remove def remove
return if path.blank? return if path.blank?
DistributedMutex.new(EMOJIS_CUSTOM_LOCK).synchronize do
if File.exists?(path) if File.exists?(path)
File.delete(path) rescue nil File.delete(path) rescue nil
Emoji.clear_cache Emoji.clear_cache
end end
end end
end
def self.all def self.all
Discourse.cache.fetch("all", family: "emoji") { standard | custom } Discourse.cache.fetch("all", family: "emoji") { standard | custom }
@ -31,6 +36,14 @@ class Emoji
Discourse.cache.fetch("custom", family: "emoji") { load_custom } Discourse.cache.fetch("custom", family: "emoji") { load_custom }
end end
def self.exists?(name)
Emoji[name].present?
end
def self.[](name)
Emoji.custom.detect { |e| e.name == name }
end
def self.create_from_path(path) def self.create_from_path(path)
extension = File.extname(path) extension = File.extname(path)
Emoji.new(path).tap do |e| Emoji.new(path).tap do |e|
@ -51,15 +64,19 @@ class Emoji
def self.create_for(file, name) def self.create_for(file, name)
extension = File.extname(file.original_filename) extension = File.extname(file.original_filename)
path = "#{Emoji.base_directory}/#{name}#{extension}" path = "#{Emoji.base_directory}/#{name}#{extension}"
DistributedMutex.new(EMOJIS_CUSTOM_LOCK).synchronize do
# store the emoji # store the emoji
FileUtils.mkdir_p(Pathname.new(path).dirname) FileUtils.mkdir_p(Pathname.new(path).dirname)
File.open(path, "wb") { |f| f << file.tempfile.read } File.open(path, "wb") { |f| f << file.tempfile.read }
# clear the cache # clear the cache
Emoji.clear_cache Emoji.clear_cache
end
# launch resize job # launch resize job
Jobs.enqueue(:resize_emoji, path: path) Jobs.enqueue(:resize_emoji, path: path)
# return created emoji # return created emoji
Emoji.custom.detect { |e| e.name == name } Emoji[name]
end end
def self.clear_cache def self.clear_cache
@ -76,10 +93,12 @@ class Emoji
end end
def self.load_custom def self.load_custom
DistributedMutex.new(EMOJIS_CUSTOM_LOCK).synchronize do
Dir.glob(File.join(Emoji.base_directory, "*.{png,gif}")) Dir.glob(File.join(Emoji.base_directory, "*.{png,gif}"))
.sort .sort
.map { |emoji| Emoji.create_from_path(emoji) } .map { |emoji| Emoji.create_from_path(emoji) }
end end
end
def self.base_directory def self.base_directory
"public/#{base_url}" "public/#{base_url}"