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

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.

diff --git a/plugins/discourse-narrative-bot/plugin.rb b/plugins/discourse-narrative-bot/plugin.rb
index 54e97a05e7..efa6ae8fea 100644
--- a/plugins/discourse-narrative-bot/plugin.rb
+++ b/plugins/discourse-narrative-bot/plugin.rb
@@ -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
diff --git a/plugins/discourse-narrative-bot/public/public b/plugins/discourse-narrative-bot/public/public
new file mode 120000
index 0000000000..65954e2f81
--- /dev/null
+++ b/plugins/discourse-narrative-bot/public/public
@@ -0,0 +1 @@
+/home/regis/Poetry/discourse/plugins/discourse-narrative-bot/public
\ No newline at end of file

GitHub sha: 38074847