ActiveJob Fails with 'Uninitialized Constant GuestsCleanupJob' Error After OAuth2 Authentication

54 Views Asked by At

I'm currently using ActionCable to broadcast a JWT token once a user has been authenticated. I've scheduled the broadcast to occur 4 seconds after authentication because I'm redirecting the client to the login page first and then want to broadcast the JWT token.

Application job Class

class ApplicationJob < ActiveJob::Base
  queue_as :mailers

  def perform(devise_mailer, method,user_id, *args)
    user = User.find(user_id)
    devise_maier.send(method,user,*args).deliver_now
  end

end

In my google_oauth2 action within the OmniauthCallbacksController, I have the following code:

  def google_oauth2
  # You need to implement the method below in your model (e.g. app/models/user.rb)
  @user = User.from_omniauth(request.env['omniauth.auth'])
  if @user.persisted?
    flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'        
    sign_in @user, event: :authentication #this will throw if @user is not activated
    token = Warden::JWTAuth::UserEncoder.new.call(@user, :user, nil)[0].to_s                          
    binding.pry
    GuestsCleanupJob.set(wait: 2.second).perform_now(token)
    redirect_to "http://127.0.0.1:3002/", allow_other_host: true  
  else
    session['devise.google_data'] = request.env['omniauth.auth'].except('extra') # Removing extra as it can overflow some session stores
    redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
  end

end

I'm scheduling the broadcast by passing it to an ActiveJob queue that runs in Sidekiq. Here's the relevant code:

   class GuestsCleanupJob < ApplicationJob
  queue_as :default

  def perform(token)
    ActionCable.server.broadcast "ChatChannel", {"type":"token","token":"Bearer #{token}"}       
  end
end

However, the ActiveJob fails with the following error logs:

2023-11-03T05:13:31.936Z pid=87046 tid=1fjq WARN: 
2023-11-03T05:13:52.419Z pid=87046 tid=1fl6 class=GuestsCleanupJob jid=5b449cba56dbda04714ea023 INFO: start
2023-11-03T05:13:52.471Z pid=87046 tid=1fl6 class=GuestsCleanupJob jid=5b449cba56dbda04714ea023 elapsed=0.053 INFO: fail
2023-11-03T05:13:52.471Z pid=87046 tid=1fl6 WARN: {"context":"Job raised exception","job":{"retry":true,"queue":"default","class":"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper","wrapped":"GuestsCleanupJob","args":[{"job_class":"GuestsCleanupJob","job_id":"f3e429cb-2ad3-4259-92e1-4816b8b674af","provider_job_id":null,"queue_name":"default","priority":null,"arguments":["eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJmYjA1NDQ0Zi03ZmQ0LTRhZTItYTMwOS0xOTMwZTc3ZWFjYWQiLCJmb28iOiJiYXIiLCJzdWIiOiIxIiwic2NwIjoidXNlciIsImF1ZCI6bnVsbCwiaWF0IjoxNjk4OTg4MzYxLCJleHAiOjE2OTg5OTAxNjF9.0yEhVrnFuYo8ZYE3G3cpbMe34dGJDU8jeTX3g8plqWU"],"executions":0,"exception_executions":{},"locale":"en","timezone":"UTC","enqueued_at":"2023-11-03T05:13:07Z"}],"jid":"5b449cba56dbda04714ea023","created_at":1698988387.1106932,"enqueued_at":1698988432.414398,"error_message":"uninitialized constant GuestsCleanupJob\n\n      Object.const_get(camel_cased_word)\n            ^^^^^^^^^^","error_class":"NameError","failed_at":1698988389.918184,"retry_count":1,"retried_at":1698988411.9304},"_config":{"labels":"#<Set: {}>","require":".","environment":"development","concurrency":1,"timeout":25,"poll_interval_average":null,"average_scheduled_poll_interval":5,"on_complex_arguments":"raise","error_handlers":["#<Proc:0x000000010fe27560 /Users/moinahmed/moin_blog/backendapp/.bundle/ruby/3.1.0/gems/sidekiq-7.1.2/lib/sidekiq/config.rb:37 (lambda)>"],"death_handlers":[],"lifecycle_events":{"startup":[],"quiet":[],"shutdown":[],"heartbeat":[],"beat":["#<Proc:0x00000001105365b8 /Users/moinahmed/moin_blog/backendapp/.bundle/ruby/3.1.0/gems/sidekiq-7.1.2/lib/sidekiq/metrics/tracking.rb:133>"]},"dead_max_jobs":10000,"dead_timeout_in_seconds":15552000,"reloader":"#<Sidekiq::Rails::Reloader @app=Backend::Application>","backtrace_cleaner":"#<Proc:0x00000001128ebe40 /Users/moinahmed/moin_blog/backendapp/.bundle/ruby/3.1.0/gems/sidekiq-7.1.2/lib/sidekiq/rails.rb:59 (lambda)>","queues":["default","mailers"],"config_file":"./config/sidekiq.yml","tag":"backendapp","identity":"Moins-MacBook-Air.local:87046:f520db71521f"}}
2023-11-03T05:13:52.473Z pid=87046 tid=1fl6 WARN: NameError: uninitialized constant GuestsCleanupJob

      Object.const_get(camel_cased_word)

I'd appreciate any help or insights into why the ActiveJob is failing with the "uninitialized constant GuestsCleanupJob" error.

I tried add namespace around my ActiveJob class

    module Jobs
class GuestsCleanupJob < ApplicationJob
  queue_as :default

  def perform(token)
    ActionCable.server.broadcast "ChatChannel", {"type":"token","token":"Bearer #{token}"}       
  end
end
end

But the above changes throws same errors

NameError: uninitialized constant Jobs

FYI the application job that sends devise mailer works fine without any issue

RUBY VERSION: 3.. RAILS. 7

0

There are 0 best solutions below