如何终止控制器模型中存在的线程(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)
我有一个控制器 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
我有这样的模型代码
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
每当捕获异常时,它将转到模型中的 exception_handler 方法。
现在,我试图在捕获到异常时终止线程,所以我尝试将线程分配给变量 @ram.thread_check_status = thread_status
所以我在模型中创建了一个方法 def self.kill_threads
来终止线程并调用def excpetion_handle
方法中的那个方法。
但是,它不起作用我想我以错误的方式将线程分配给了变量@ram.thread_check_status = thread_status
请建议我如何终止模型中与 @ram
id 关联的线程。
我在 RamsController 中还有两个方法和两个线程,我也在尝试终止这些线程。
我想到了这样的解决方案。
首先,在控制器中,我使用这样的名称和值设置线程变量
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
@ram.check_status!
ActiveRecord::Base.connection.close
end
@ram.thread_check_status = thread_status
end
render json: { status: "success" }
end
所以在模型中我列出了所有可用的线程并终止了与 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
我有一个控制器 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
我有这样的模型代码
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
每当捕获异常时,它将转到模型中的 exception_handler 方法。
现在,我试图在捕获到异常时终止线程,所以我尝试将线程分配给变量 @ram.thread_check_status = thread_status
所以我在模型中创建了一个方法 def self.kill_threads
来终止线程并调用def excpetion_handle
方法中的那个方法。
但是,它不起作用我想我以错误的方式将线程分配给了变量@ram.thread_check_status = thread_status
请建议我如何终止模型中与 @ram
id 关联的线程。
我在 RamsController 中还有两个方法和两个线程,我也在尝试终止这些线程。
我想到了这样的解决方案。 首先,在控制器中,我使用这样的名称和值设置线程变量
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
@ram.check_status!
ActiveRecord::Base.connection.close
end
@ram.thread_check_status = thread_status
end
render json: { status: "success" }
end
所以在模型中我列出了所有可用的线程并终止了与 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