Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我的区块/产量如何传递变化的变量?_Ruby On Rails_Ruby_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 我的区块/产量如何传递变化的变量?

Ruby on rails 我的区块/产量如何传递变化的变量?,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我正在编写以下模块,以捕获偶尔发送给延迟工作人员的SIGTERM,并设置一个名为term\u now的变量,该变量允许我的工作在完成之前自动终止 如果我将模块中的以下代码内联到我的工作中,那么它就可以完美地工作,但是我需要它来完成一些工作,而当我将它放入模块中时,它就不工作了 我假设它不起作用,因为它现在只传递一次term\u(当它为false时),即使它返回true,它也不会再次传递,因此它永远不会停止作业 module StopJobGracefully def self.execut

我正在编写以下模块,以捕获偶尔发送给延迟工作人员的
SIGTERM
,并设置一个名为
term\u now
的变量,该变量允许我的工作在完成之前自动终止

如果我将模块中的以下代码内联到我的工作中,那么它就可以完美地工作,但是我需要它来完成一些工作,而当我将它放入模块中时,它就不工作了

我假设它不起作用,因为它现在只传递一次
term\u
(当它为false时),即使它返回true,它也不会再次传递,因此它永远不会停止作业

module StopJobGracefully

  def self.execute(&block)
    begin
      term_now = false
      old_term_handler = trap('TERM') do
        term_now = true
        old_term_handler.call
      end

      yield(term_now)
    ensure
      trap('TERM', old_term_handler)
    end
  end

end
以下是正常使用的内联代码(这是我试图转换为模块的代码):


基本上是你自己回答的。在您提供的示例代码中,
term\u now
只有在调用
yield
之前捕捉的陷阱时才会变为真

您需要做的是提供一种定期获取信息的机制,这样您就可以在ie
find_in_batches
的运行中进行检查

因此,您的模块应该有一个
term\u now
方法,该方法可能返回一个实例变量
@term\u now

class SMSRentDueSoonJob

  def perform
    begin
      term_now = false
      old_term_handler = trap('TERM') do 
        term_now = true 
        old_term_handler.call
      end

      User.find_in_batches(batch_size: 1000) do

        if term_now
          raise 'Gracefully terminating job early...'
        end

        # do lots of complicated work here
      end

    ensure
      trap('TERM', old_term_handler)
    end
  end

end