简体   繁体   English

如何杀死控制器模型中存在的线程(Ruby 2.1.2 on Rails 3.2.13)

[英]How to Kill threads that are present in Controller in its model (Ruby 2.1.2 on Rails 3.2.13)

I have a controller RamsController in that I have this method我有一个控制器RamsController因为我有这个方法

def check_status
   @ram = Ram.find(params[:id])
   if @ram.workflow == "start"
      thread_status = Thread.new do
         thread.current[:name] = "thread_check_status"
         @ram.check_status!
         ActiveRecord::Base.connection.close
      end
      @ram.thread_check_status = thread_status
   end
  render json: { status: "success" }
end

I have a model code like this我有一个这样的模型代码

class Ram < ActiveRecord::Base

attr_accessor :thread_check_status 

  def self.kill_threads
    self.thread_check_status.kill
  end

  def exception_handler(exception)
   if exception == "exited by user"
     self.kill_threads
   end
  end

Whenever an exception is caught it will go to the exception_handler method in the model.每当捕获到异常时,它将转到模型中的 exception_handler 方法。 And now, I'm trying to kill the thread when the exception is caught so I tried to assign the thread to the variable @ram.thread_check_status = thread_status So I created a method def self.kill_threads in the model to kill the threads and called that method in def excpetion_handle method.现在,我试图在捕获异常时杀死线程,所以我试图将线程分配给变量@ram.thread_check_status = thread_status所以我在模型中创建了一个方法def self.kill_threads来杀死线程并调用def excpetion_handle方法中的那个方法。

But, it is not working I think I assigned the thread to the variable in a wrong way @ram.thread_check_status = thread_status但是,它不起作用我想我以错误的方式将线程分配给变量@ram.thread_check_status = thread_status

Please suggest me how to kill the threads associated with @ram id in the model.请建议我如何杀死模型中与@ram id关联的线程。

And I have two more methods in RamsController with two more threads and I'm trying kill those threads too.我在RamsController 中还有两个方法,还有两个线程,我也在尝试杀死这些线程。

I come up with a solution like this.我想出了一个这样的解决方案。 Firstly, in the controller I set the thread variable with a name and value like this首先,在控制器中,我使用这样的名称和值设置线程变量

def check_status
   @ram = Ram.find(params[:id])
   if @ram.workflow == "start"
      Thread.new do

Thread.current.thread_variable_set(:thread_name, @ram.id) #This will set the thread value to ram_id Thread.current.thread_variable_set(:thread_name, @ram.id) #这会将线程值设置为ram_id

         @ram.check_status!
         ActiveRecord::Base.connection.close
      end
      @ram.thread_check_status = thread_status
   end
  render json: { status: "success" }
end

So in the model I listed all the threads available and killed the thread which is associated with ram_id, thread_name所以在模型中我列出了所有可用的线程并杀死了与 ram_id、thread_name 关联的线程

class Ram < ActiveRecord::Base

attr_accessor :thread_check_status 



  def exception_handler(exception)

Thread.list.each do |thr|

if thr.thread_variable_get(:thread_name) == self.id

thr.kill

end

end

   if exception == "exited by user"
     self.kill_threads
   end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM