telegram-bot icon indicating copy to clipboard operation
telegram-bot copied to clipboard

Need advice on configuring error handling in an async job

Open KonstantinReido opened this issue 10 months ago • 1 comments

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?

KonstantinReido avatar Feb 09 '25 15:02 KonstantinReido

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.

printercu avatar Feb 14 '25 04:02 printercu