I am basically running a background process that checks for files, and then updates the rails model based on the data discovered in the file. However, I can't access the model from within the thread because of an error.
Here's my example:
def check_logs
while @start == 1
results = Dir.glob("#{@path}/*.txt")
unless results.empty?
results.each do |result|
file_name = result.split("/")[-1]
data = File.open(result).read
if file_name.include? "get"
data_contents = data.split("\n")
time = data_contents[0]
ExamResult.create(time: time)
end
FileUtils.rm_rf result
end
end
sleep 5
end
end
def start_agent
@start = 1
Thread.start {check_logs}
end
def stop_agent
@start = 0
end
However, while it's in the background, this is the error that I see coming across the console:
terminated with exception (report_on_exception is true): Traceback (most recent call last): 5: from portal/lib/custom_rb/exam_results/exam_custom.rb:69:in
block in start_monitoring_agent' 4: from portal/lib/custom_rb/exam_results/exam_custom.rb:40:incheck_logs' 3: from portal/lib/custom_rb/exam_results/exam_custom.rb:40:ineach' 2: from portal/lib/custom_rb/exam_results/exam_custom.rb:46:inblock in check_logs' 1: from /home/nutella/.rvm/gems/ruby-2.5.1/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:202:inconst_missing' /home/user/.rvm/gems/ruby-2.5.1/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:496:inload_missing_constant': A copy of ExamResult has been removed from the module tree but is still active! (ArgumentError)
My goal here is just to have a backgrounded process to monitor for logs. I've seen some other posts about this same exact error, but perhaps I could be doing this a little better other than the solutions provided for them.
Any thoughts or feedback would be greatly appreciated.