このコミットが含まれているのは:
2026-07-18 00:01:12 +09:00
コミット ff970f8171
11個のファイルの変更449行の追加173行の削除
+43 -7
ファイルの表示
@@ -1,21 +1,54 @@
class PostBulkCreator
def initialize actor:, posts:, thumbnails:
@actor = actor
@actor_id = actor.id
@posts = posts
@thumbnails = thumbnails
end
def run
results = @posts.each_with_index.map { |attributes, index|
create_row(attributes, index)
}
results = Array.new(@posts.length)
mutex = Mutex.new
next_index = 0
workers = Array.new(2) do
Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
actor = User.find(@actor_id)
loop do
index = nil
begin
index = mutex.synchronize do
current = next_index
next_index += 1
current
end
break if index >= @posts.length
attributes = @posts[index]
results[index] = create_row(actor, attributes, index)
rescue StandardError => e
Rails.logger.error(
"post_bulk_creator_worker_failure #{ { error: e.class.name,
message: e.message,
index: }.to_json }")
results[index] = {
status: 'failed',
recoverable: false,
errors: { base: ['登録中にエラーが発生しました.'] },
base_errors: [] }
end
end
end
end
end
workers.each(&:join)
{ results: }
end
private
def create_row attributes, index
def create_row actor, attributes, index
preflight =
PostCreatePreflight.new(
attributes: attributes,
@@ -27,7 +60,7 @@ class PostBulkCreator
end
post = PostCreator.new(
actor: @actor,
actor: actor,
attributes: normalised_attributes(attributes, preflight, index)).create!
result = {
status: 'created',
@@ -122,7 +155,10 @@ class PostBulkCreator
original_created_from: preflight[:original_created_from],
original_created_before: preflight[:original_created_before],
duration: preflight[:duration],
video_ms: preflight[:video_ms] }
video_ms: preflight[:video_ms],
normalised_tags: preflight[:normalised_tags],
tag_sections: preflight[:tag_sections],
normalised_parent_post_ids: preflight[:normalised_parent_post_ids] }
end
def thumbnail_for index, attributes