I’m writing a sinatra app in ruby that gathers information about my network into two different files. The first, a .csv, gathers information on the IP Addresses and given names of all workstations in the network. The second, a .txt, reads into an Asterisk server and gathers information on active SIP channels linking to specified IP Addresses.
My app is merely compiling the information from these two files and creating tables on a webpage matching up users currently logged into specific stations. However, I want the app to feign realtime-use. I have the two files being automatically updated every 5 minutes, but as the files being read by the app are being overwritten, the app’s output doesn’t change. Is there a way to rig it so the app is reading the “new” files after they are written?
I’ve dug around on stack overflow, and I’ve seen things mentioning Kqueue for python users to simply watch for changes in those files before rewriting them, which would be really nice. Is there a ruby version for that? Additionally, I want the app to be accessible at all times, and maybe only “refresh” when it needs to update something, so that would make sense.
I also came to a funny little epiphany when I realized I’m never closing my files after I read them. I won’t post the whole app, but here’s where I read the files:
#Pulls active SIP channels from Asterisk
$sip = {}
File.open('sip.txt').each do |line|
userid,ip = line.split(" ")
$sip[ip] = userid[0..3]
end
#Prepares hash of all stations, ip addresses
$machines = {}
CSV.foreach('/Volumes/Scripts/report-51.csv') do |row|
name = row[1]
address = row[0]
$machines[name] = address
end
Is it possible that because I never close the files, the program never opens up the “new” documents? Just trying to brainstorm here.
It’s hard to tell the overall structure of what’s going on, but yeah, whatever code is being called every 5 minutes and which in turn does the file reading, I would make sure that the files are closed and reopened in that code with every call. That should solve your problem.
You may also consider switching to a database approach for more robust results.