How can I start a Procfile process, after the rails server has completely started up with foreman?

1.6k views Asked by At

I have a rails app that runs on parallel with a ruby script. This is script is a ruby-mqtt subscriber, basically it listens for messages over the mqtt protocol and then interacts with the rails app.

The problem is that foreman starts this process simultaneously with the rails server and when there are messages being broadcast and the server has not finished it's startup process, the script crashes, and foreman kills the server with it.

This is the example Procfile:

server: rails s -p 3000 -b 0.0.0.0
mqtt_subscriber: ruby ./mqtt/subscribers/mqtt_subscriber.rb

The current workaround is to run the rails server and when it finishes start up, launch the subscriber from another terminal or using screen.

Is there a way to run the subscriber processes after the server has completely started through foreman start?

1

There are 1 answers

0
chris raethke On BEST ANSWER

You can use the wait-for-it bash script to wait for a tcp port to become available, before the mqtt subscriber starts

server: rails s -p 3000 -b 0.0.0.0
mqtt_subscriber: wait-for-it.sh -t 0 localhost:3000 -- ruby ./mqtt/subscribers/mqtt_subscriber.rb