【发布时间】:2021-08-02 09:36:01
【问题描述】:
我的目标是查找当前堆栈中是否有 ActionJob::Base 以查看它是否来自后台作业。
我想办法是在 ruby 中为我当前的位置调用类,这样我就可以在调用堆栈中搜索并找到 ActionJob::Base 祖先类。
在 ruby 中有 a few ways 从 the current location 获取堆栈跟踪/回溯。他们涉及Thread.current.backtrace。这会导致字符串数组作为文件名而不是被调用的类。即
/Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:537:in `eval_input'
/Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:472:in `block in run'
/Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:471:in `catch'
/Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:471:in `run'
/Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:400:in `start'
/Users/justin/dev/testarea/lamby_discovery/vendor/bundle-dev/ruby/2.7.0/gems/railties-6.1.3.2/lib/rails/commands/console/console_command.rb:70:in `start'
/Users/justin/dev/testarea/lamby_discovery/vendor/bundle-dev/ruby/2.7.0/gems/railties-6.1.3.2/lib/rails/commands/console/console_command.rb:19:in `start'
/Users/justin/dev/testarea/lamby_discovery/vendor/bundle-dev/ruby/2.7.0/gems/railties-6.1.3.2/lib/rails/commands/console/console_command.rb:102:in `perform'
/Users/justin/dev/testarea/lamby_discovery/vendor/bundle-dev/ruby/2.7.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
/Users/justin/dev/testarea/lamby_discovery/vendor/bundle-dev/ruby/2.7.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
/Users/justin/dev/testarea/lamby_discovery/vendor/bundle-dev/ruby/2.7.0/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch'
/Users/justin/dev/testarea/lamby_discovery/vendor/bundle-dev/ruby/2.7.0/gems/railties-6.1.3.2/lib/rails/command/base.rb:69:in `perform'
byebug 的backtrace 包含我正在寻找的信息(即IRB::Context),但似乎不可搜索,并且backtrace 是一个c 方法。
--> #0 IRB::Context.set_last_value(value#NilClass) at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb/context.rb:363
#1 IRB::Context.evaluate(line#String, line_no#Integer, exception#NilClass) at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb/context.rb:459
#2 block (2 levels) in IRB::Irb.block (2 levels) in eval_input at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:541
#3 IRB::Irb.signal_status(status#Symbol) at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:704
#4 block in IRB::Irb.block in eval_input at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:538
#5 block (2 levels) in RubyLex.block (2 levels) in each_top_level_statement at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb/ruby-lex.rb:166
ͱ-- #6 Kernel.loop at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb/ruby-lex.rb:151
#7 block in RubyLex.block in each_top_level_statement at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb/ruby-lex.rb:151
ͱ-- #8 Kernel.catch(*args) at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb/ruby-lex.rb:150
#9 RubyLex.each_top_level_statement at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb/ruby-lex.rb:150
#10 IRB::Irb.eval_input at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:537
#11 block in IRB::Irb.block in run(conf#Hash) at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:472
ͱ-- #12 Kernel.catch(*args) at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:471
#13 IRB::Irb.run(conf#Hash) at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:471
#14 #<Class:IRB>.start(ap_path#NilClass) at /Users/justin/.rbenv/versions/2.7.3/lib/ruby/2.7.0/irb.rb:400
有什么建议吗?
【问题讨论】:
-
另一种选择是在 ActionJob 的
before_perform中设置一个Thread.current变量,然后您可以检查该变量是否存在以了解它是否在后台作业中 -
@maxpleaner 这是个好主意。考虑到任何涉及读取类堆栈的解决方案似乎都不现实,我可能最终会使用它。
标签: ruby