Last active
April 18, 2023 13:13
-
-
Save webuilder240/0ceec0b1b3f429e3a2ec8aa9c08d4788 to your computer and use it in GitHub Desktop.
1は条件分岐をキューイングする際、Jobクラス内部のどちらででも行うようにしています。2はJobクラスでのみ処理条件を書くようにしています。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# user.rb | |
class User < ApplicationRecord | |
after_create_commit do | |
if is_admin? | |
AdminUserCreateLoggingJob.perform_later(self.id) | |
end | |
end | |
end | |
# ConditionをJobクラスに含めない | |
class AdminUserCreateLoggingJob < ApplicationJob | |
queue_as :default | |
def perform(user_id) | |
user = User.find_by!(id: user_id, is_admin: true) | |
Rails.logger.info "Admin user #{user.name} logging..." | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# user.rb | |
# キューイング時に処理条件は書かない。 | |
class User < ApplicationRecord | |
after_create_commit do | |
AdminUserCreateLoggingJob.perform_later(self.id) | |
end | |
end | |
# ConditionをJobクラスに含める | |
class AdminUserCreateLoggingJob < ApplicationJob | |
queue_as :default | |
def perform(user_id) | |
user = User.find(user_id) | |
if user.is_admin? | |
Rails.logger.info "Admin user #{user.name} logging..." | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment