Schorschi
Posts: 202
Joined: Thu Nov 22, 2012 9:38 pm

Run Ruby Rails Server As Daemon?

Thu Jan 11, 2018 3:39 pm

Run Ruby Rails Server As Daemon? I found literally 10s of examples, but none seem to work. I find it odd that Ruby on Rails is so popular and something so basic is so apparently confusing? Anyone have an example of Ruby on Rails on Raspberry Pi, I.e. Debian 9, where Puma based Ruby Rails application is run as a daemon, i.e. system?

Heater
Posts: 8984
Joined: Tue Jul 17, 2012 3:02 pm

Re: Run Ruby Rails Server As Daemon?

Thu Jan 11, 2018 3:55 pm

The modern way to run a program in the background, as a daemon, from boot up is to use systemd.

See the instructions I wrote a while ago here: viewtopic.php?t=138861#p921354

Schorschi
Posts: 202
Joined: Thu Nov 22, 2012 9:38 pm

Re: Run Ruby Rails Server As Daemon?

Thu Jan 11, 2018 4:46 pm

Thank you for your post, the structure and configuration of the systemd files, for the service, I have figured out, what is missing is that actual ExecuteStart command that works. There are many examples but none seem to work correctly with the specific Ruby/Rails configuration that is current in Debian 9 on Pi.

For example...

# ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [armv7l-linux-eabihf]

# rails -v
Rails 5.1.4

Of course this works...
# cd /home/web/demo
# rails server
=> Booting Puma
=> Rails 5.1.4 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.11.0 (ruby 2.4.1-p111), codename: Love Song
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started GET "/" for 192.168.1.11 at 2018-01-11 16:35:41 +0000
Cannot render console from 192.168.1.11! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Rails::WelcomeController#index as HTML
Rendering /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/templates/rails/welcome/index.html.erb
Rendered /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/templates/rails/welcome/index.html.erb (26.3ms)
Completed 200 OK in 1054ms (Views: 70.1ms)

But getting this concept to systemd unit defintion file? For example...

# systemctl status demo.socket demo.service
● demo.socket - Rails HTTP Server Accept Sockets
Loaded: loaded (/etc/systemd/system/demo.socket; static; vendor preset: enabled)
Active: failed (Result: service-start-limit-hit) since Thu 2018-01-11 16:42:01 UTC; 2s ago
Listen: 0.0.0.0:3000 (Stream)

Jan 11 16:41:57 base systemd[1]: Listening on Rails HTTP Server Accept Sockets.
Jan 11 16:42:01 base systemd[1]: demo.socket: Unit entered failed state.

● demo.service
Loaded: loaded (/etc/systemd/system/demo.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2018-01-11 16:42:01 UTC; 2s ago
Process: 32341 ExecStart=/home/web/demo/bin/rails server (code=exited, status=1/FAILURE)
Main PID: 32341 (code=exited, status=1/FAILURE)

Jan 11 16:42:01 base systemd[1]: demo.service: Failed with result 'exit-code'.
Jan 11 16:42:01 base systemd[1]: demo.service: Service hold-off time over, scheduling restart.
Jan 11 16:42:01 base systemd[1]: Stopped demo.service.
Jan 11 16:42:01 base systemd[1]: demo.service: Start request repeated too quickly.
Jan 11 16:42:01 base systemd[1]: Failed to start demo.service.
Jan 11 16:42:01 base systemd[1]: demo.service: Unit entered failed state.
Jan 11 16:42:01 base systemd[1]: demo.service: Failed with result 'exit-code'.

Where... the socket and service files are...

# cat /etc/systemd/system/demo.service
[Service]
ExecStart=/home/web/demo/bin/rails server
User=web
Restart=always

[Install]
WantedBy=multi-user.target

# cat /etc/systemd/system/demo.socket
[Unit]
Description=Rails HTTP Server Accept Sockets

[Socket]
ListenStream=0.0.0.0:3000
NoDelay=true
ReusePort=true
Backlog=1024
Restart=always

[Install]
WantedBy=sockets.target

Heater
Posts: 8984
Joined: Tue Jul 17, 2012 3:02 pm

Re: Run Ruby Rails Server As Daemon?

Fri Jan 12, 2018 4:34 am

Your service files don't seem to have everything specified as per the example I linked to above. Which was:

[Service]
WorkingDirectory=/home/pi/propanel
ExecStart=node /home/pi/propanel/propanel.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=propanel
User=root
Group=root
Environment=NODE_ENV=production
[Install]

I don't see any "WorkingDirectory" specified. That should be the location that you normal run the thing from.

You should also include the StandardOutput, StandardError and SyslogIdentifier items. Then you will be able to anu output this thing has in /var/log/syslog

You have two services there demo.service and demo.socket. We were originally asked about a single Rails server. What is that about?

Schorschi
Posts: 202
Joined: Thu Nov 22, 2012 9:38 pm

Re: Run Ruby Rails Server As Daemon?

Fri Jan 12, 2018 4:03 pm

Thanks for the reply. The service versus socket scenario was something I found in one of the various examples, that looking interesting, but it complicated the issue. What I wanted to do was to have each rails application use a different port, this is all development level work, but would let me isolate traffic per port with ease, for analysis.

You are correct, the working directory should be qualified, and I agree adding Standard*/Syslog is a good idea. Last night, after some serious isolation differences and comparison of examples, I got a simple variant working. The sample below works, and it is the minimum, I still need to incorporate some of the suggestions you noted, I will do that and post and update version soon. But for those that may find this interesting, here is what is working for now now.

# cat /etc/systemd/system/demo.service
[Unit]
Description=Puma HTTP Server
After=network.target

[Service]
Type=simple
WorkingDirectory=/home/rails/demo
ExecStart=/bin/bash -lc 'bundle exec puma -C /home/rails/demo/config/puma.rb'
User=rails
Restart=always

[Install]
WantedBy=multi-user.target

I change the default port in the config/puma.rb script, per application, so demo is 3000, hello is 3001, etc. for example. Long term, I will set the rails applications to a production comparable environment, where I plan to use environment variables to control the port assignment, but I am still learning rails, so this is sufficient for now.

# cat /home/rails/demo/config/puma.rb | grep PORT
port ENV.fetch("PORT") { 3000 }

# cat /home/rails/hello/config/puma.rb | grep PORT
port ENV.fetch("PORT") { 3001 }

What is interesting is that their are so many variants to invoking the rails server to execute the application, the examples vary even within the same OS, distribution, etc. This has always been what I consider good and bad about Linux, to many ways to do the same thing. Having also been a engineer/designer/programmer on Macintosh and Windows, where the variance is less, ah, diverse? But I digress.

I will post a more detailed unit file with the suggestions you noted soon as I noted above.

Heater
Posts: 8984
Joined: Tue Jul 17, 2012 3:02 pm

Re: Run Ruby Rails Server As Daemon?

Sat Jan 13, 2018 2:02 am

You should be fine.

Despite the fact that a lot of people complain about systemd I have found it makes setting up such servers very easy. It has been reliable for us on many cloud servers for a long time now.

I have also have multiple HTTP servers running on one machine, listening on different ports. I use node.js because it's, well, more cool. All those services are accessed through nginx on the regular port 80 or more usually 443. It's very easy to configure nginx to proxy pass different URLs to the different services on other ports.

Yes, there are many ways to skin cats with Linux. Can be annoying because it means you end up reading lot of advice on the net that is out of date or does not apply to your Linux distro or is not suitable for you use case or mostly just wrong!

Schorschi
Posts: 202
Joined: Thu Nov 22, 2012 9:38 pm

Re: Run Ruby Rails Server As Daemon?

Sun Jan 14, 2018 11:10 pm

Funny, that you note using nginx? This is one of the ideas I had for when I setup a test/production environment. Good to know! Thanks for your help, appreciate it.

Heater
Posts: 8984
Joined: Tue Jul 17, 2012 3:02 pm

Re: Run Ruby Rails Server As Daemon?

Mon Jan 15, 2018 4:12 am

Seems to be quite a common practice to use nginx as the public facing web server and have it proxy pass requests to back end services on different machines or different ports on the same machine.

I have used it a lot. It takes care of HTTPS for me and passes requests on as HTTP.

It's very easy to set up and performs well.

Return to “Other languages”

Who is online

Users browsing this forum: No registered users and 1 guest