FIX: Don't raise error in update username job if user has been deleted.

FIX: Don’t raise error in update username job if user has been deleted.

diff --git a/app/jobs/regular/update_username.rb b/app/jobs/regular/update_username.rb
index ea4985d..f9660c2 100644
--- a/app/jobs/regular/update_username.rb
+++ b/app/jobs/regular/update_username.rb
@@ -7,6 +7,9 @@ module Jobs
 
     def execute(args)
       @user_id = args[:user_id]
+      user = User.find_by(id: @user_id)
+      return unless user
+
       @old_username = args[:old_username].unicode_normalize
       @new_username = args[:new_username].unicode_normalize
       @avatar_img = PrettyText.avatar_img(args[:avatar_template], "tiny")
@@ -36,7 +39,7 @@ module Jobs
       update_post_custom_fields
 
       DiscourseEvent.trigger(:username_changed, @old_username, @new_username)
-      DiscourseEvent.trigger(:user_updated, User.find_by(id: @user_id))
+      DiscourseEvent.trigger(:user_updated, user)
     end
 
     def update_posts
diff --git a/spec/jobs/update_username_spec.rb b/spec/jobs/update_username_spec.rb
new file mode 100644
index 0000000..1269b6c
--- /dev/null
+++ b/spec/jobs/update_username_spec.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+RSpec.describe Jobs::UpdateUsername do
+  fab!(:user) { Fabricate(:user) }
+
+  it 'does not do anything if user_id is invalid' do
+    events = DiscourseEvent.track_events do
+      described_class.new.execute(
+        user_id: -999,
+        old_username: user.username,
+        new_username: 'somenewusername',
+        avatar_template: user.avatar_template
+      )
+    end
+
+    expect(events).to eq([])
+  end
+end

GitHub sha: 01600492