I have a controller RamsController in that I have this method
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
@ram.thread_check_status = thread_status
def self.kill_threads
def excpetion_handle
@ram.thread_check_status = thread_status
@ram
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
@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
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