Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need advice on configuring error handling in an async job #256

Open
KonstantinReido opened this issue Feb 9, 2025 · 1 comment
Open

Comments

@KonstantinReido
Copy link
Contributor

I want to add error handling for an async job. I found a recommendation in the repository to use Telegram::Bot::Client.default_async_job.class_eval in an initializer. However, the following code in my initializer:

Telegram::Bot::Client.default_async_job.class_eval do
  rescue_from Telegram::Bot::Forbidden do |error|
    Rails.logger.error("Telegram::Bot::Forbidden error: #{error.message}")
  end
end

causes the application to crash on startup with the following error:

~/.rbenv/versions/3.4.1/lib/ruby/gems/3.4.0/gems/telegram-bot-0.16.7/lib/telegram/bot/async.rb:61:in 'Telegram::Bot::Async::ClassMethods#default_async_job': Define ApplicationJob class or setup #async= with custom job class (RuntimeError)
	from ~/project/config/initializers/telegram.rb

In my Rails app, the ApplicationJob class is defined.

Rails 8.0.1
Ruby 3.4.1


Currently, I have added rescue_from to my ApplicationJob, but I would like to move it to a separate file, ideally in the initializer.

What is the recommended way to configure error handling in an async job?

@printercu
Copy link
Member

Hey, I think rails run initializers before loading classes in ./app. I'm not familiar with Rails 8 bootstrap process, but in older versions it was possible to register finalizers with Rails.application.config.to_prepare. Finalizer should have access to all app classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants