Search code examples
ruby-on-railsrubytcpubuntu-18.04

How to start a rails task as a service


I have a TCP server written in the rails application as a rails task. However it can be started from as:

  rails socketing:start  

and the process will start executing if wanted it to be a task that keep on running in the beckground them change it as:

nohup rails socketing:start &

and will be keep on working in backgroup as the tcp server has multi threading implemented into it if there is an error that will stop only 1 thread.

Now I would like this process to be started by the service at the boottime of the ubuntu server for that created a service and enabled it. But the code break can you please suggest a way out My service is written as:

[Unit]
Description = TCP at 51234
After = network.target

[Service]
Environment="HOME=/home/vidur"
ExecStart = /home/vidur/rails_app/tukaweb/custom_script.sh

[Install]
WantedBy = multi-user.target

My custom_script.sh as:

#!/bin/bash

 cd /home/vidur/rails_app/project_dir
/home/vidur/.rbenv/shims/bundler exec rails socketing:start

Start the service as:

sudo systemctl start socketing.service

It give o/p as:

Jun 10 17:45:31 Vidur-PC systemd[1]: Started TCP at 51234.
Jun 10 17:45:31 Vidur-PC systemd[1]: socketing.service: Succeeded.

But did not start the rails task or even recognise rails environment: I have rails task as:

  task start: :environment do
    require 'socket'
    puts "Started TCP Server at PORT 53492"
    server = TCPServer.new 51324 # Server bound to port 51234
   loop do
      Thread.start(server.accept) do |client|
      client.close
    end
   end

    p "result = #{result}"
    p 'Bye'
  end

This code gives following error:

Jun 10 19:16:40 Vidur-PC systemd[1]: Started TCP at 53492.
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: rake aborted!
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: ArgumentError: couldn't find login name -- expanding `~'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: /home/vidur/rails_app/tukaweb/config/application.rb:8:in `<top (required)>'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: /home/vidur/rails_app/tukaweb/Rakefile:4:in `require_relative'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: /home/vidur/rails_app/tukaweb/Rakefile:4:in `<top (required)>'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: /home/vidur/.rbenv/versions/2.7.2/bin/bundler:23:in `load'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: /home/vidur/.rbenv/versions/2.7.2/bin/bundler:23:in `<main>'
Jun 10 19:16:43 Vidur-PC custom_script.sh[60012]: (See full trace by running task with --trace)
Jun 10 19:16:44 Vidur-PC systemd[1]: socketing.service: Main process exited, code=exited, status=1/FAILURE
Jun 10 19:16:44 Vidur-PC systemd[1]: socketing.service: Failed with result 'exit-code'.

updated application.rb file:

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module TukaWeb
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1
    if Rails.env.development?
        config.active_job.queue_adapter  = :async
      else
        config.active_job.queue_adapter  = :sidekiq
    end

    config.generators.javascript_engine = :js

    config.action_dispatch.default_headers = {
        'Access-Control-Allow-Origin' => 'https://tukadata.tukatech.com',
        'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",")
    }
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
  end
end

It stated the tcp process rake task but for the root user only. enter image description here

DB Error left:

`connect': Access denied for user 'root'@'localhost'

but if run the process normal then it run.


Solution

  • file would be inside folder: /etc/systemd/system:

    [Unit]
    Description=TCP Service
    After=network.target
    
    [Service]
    #Environment="HOME=/home/vidur"
    ExecStart=/home/vidur/rails_apps/jquery_app/custom_script.sh
    Restart=always
    
    [Install]
    WantedBy = multi-user.target
    

    Also use following commands as required:

    sudo systemctl enable tcpstart
    sudo ufw allow 3005
    sudo service tcpstart start
    sudo service tcpstart status