FIX: delay the 'send_advanced_tutorial_message' job to prevent race conditions

When a user accepts an invite with an email address that matches a group
which automatically awards its members Trust Level 2, a race condition might happen
when the 'send_advanced_tutorial_message' job runs.

That job is enqueued inside the 'user_promoted' event which is triggered inside a
transaction on the user record. If the job runs before the transaction is done,
the user record is invisible and this generates an exception.
This commit is contained in:
Régis Hanol 2019-11-29 22:14:41 +01:00
parent 06c2e28bbb
commit 3807484757
2 changed files with 5 additions and 1 deletions

View File

@ -257,7 +257,10 @@ after_initialize do
args[:old_trust_level] == TrustLevel[1]
if SiteSetting.discourse_narrative_bot_enabled && promoted_from_tl1
Jobs.enqueue(:send_advanced_tutorial_message, user_id: args[:user_id])
# NOTE: since the `user_promoted` event is triggered from inside a transaction
# we have to delay the job otherwise it might run before the transaction
# is commited and the user will be invisible to the job
Jobs.enqueue_in(1.minute, :send_advanced_tutorial_message, user_id: args[:user_id])
end
end
end

View File

@ -0,0 +1 @@
/home/regis/Poetry/discourse/plugins/discourse-narrative-bot/public