ruby 如何将Rails任务作为服务启动

cmssoen2  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(115)

我在rails应用程序中编写了一个TCP服务器作为rails任务。但是它可以从以下位置启动:

rails socketing:start

字符串
并且进程将开始执行,如果希望它是在beckground中继续运行的任务,则将其更改为:

nohup rails socketing:start &


并且如果存在将仅停止1个线程的错误,则将继续在后台组中工作,因为TCP服务器已经在其中实现了多线程。
现在,我想这个过程将由服务在Ubuntu服务器的启动时间启动,创建了一个服务,并启用它.但代码中断,你可以请建议一种方法出来我的服务被写为:

[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


我的custom_script.sh为:

#!/bin/bash

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


将服务启动为:

sudo systemctl start socketing.service


它给予o/p为:

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.


但没有启动rails任务,甚至没有识别rails环境:我有rails任务:

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


此代码给出以下错误:

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'.


更新的应用程序.rb文件:

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


它声明了tcp进程rake任务,但仅针对root用户。x1c 0d1x
数据库错误:

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


但是如果正常运行进程,则它运行。

disho6za

disho6za1#

[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

字符串
还可以根据需要使用以下命令:

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

相关问题