2

I want to run a ftp listener class only when the server starts and not when console, generators, dbconsole, test, destroy, runner and rake commands run.

I've found some people doing same thing with rails 3 and 4 using checks like defined? Rails::Generators but I can't get it working in rails 5, I do not get any return with the defined check.

Subash
  • 3,128
  • 6
  • 30
  • 44

1 Answers1

4

The config.ru file is only used by web servers and not loaded by the console script, rake tasks or even your test suite. What you put there is only executed when a server instance launches.

Web servers themselves have also ways to do this. When you use Puma for instance, there are hooks like on_worker_boot or after_worker_boot, which may come to help (http://www.rubydoc.info/github/puma/puma/Puma/Configuration/DSL).

However, I'd recommend integrating this into your deployed server environment and out of the Rails app.

Christoph Petschnig
  • 4,047
  • 1
  • 37
  • 46
  • Thank you for you reply @Christoph, would you explain why you think it'd be better to integrate in the server environment instead of in the rails server – Subash Dec 20 '17 at 22:20
  • Separation of concerns, I would say most of all. You already describe the problems you have when the Rails app does both. Depending on your deployment, the app server may run on like 5 workers. This means you have also 5 FTP listeners running. On the longer term you may want to scale your app, move the application server on a different server instance than the FTP listener. – Christoph Petschnig Dec 21 '17 at 13:13
  • This works for starting threads on a server while tests, rakes tasks, capistrano deploys and development do not start the threads. – Kevin Triplett Dec 10 '22 at 00:41