I have a library with a class that, when created, will automatically scrape data from a series of FTP servers periodically. It is threaded and you can simply call a ‘results’ method to see the results thus far. I am fairly new to rails but I wanted to create a web interface for this object that allowed one to see the results, start it, stop it, and basically just interact with the object in general. I want it to be created first thing and remain intact regardless of if anyone is using the web interface so that it can run in the background and collect data. I also want it to be accessible from a controller so that I can have start/stop/info pages. Any ideas on the best way to achieve this?
P.S. I am running Rails 3.2.3 and Ruby 1.9.3
There’s better ways to run a background process and access it from a rails app, but to directly answer your question:
Just put it in an initializer. There’s a /config/initializers directory. Put a .rb file in there and it will automatically run when your rails app is started.
/config/initializers/hello.rb:
Start your rails server and you’ll see “Hello World”
To make the instance of your class globally available just create a global var: