Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails/Puma作为Systemd服务启动时不读取环境变量_Ruby On Rails_Puma - Fatal编程技术网

Ruby on rails Rails/Puma作为Systemd服务启动时不读取环境变量

Ruby on rails Rails/Puma作为Systemd服务启动时不读取环境变量,ruby-on-rails,puma,Ruby On Rails,Puma,我刚刚在Ubuntu 16.04上部署了一个处于生产模式的Rails服务器。当我从下面的命令行启动服务器时,服务器将启动并读取所有环境变量 bundle exec puma -e production -C /home/deploy/shared/config/puma.rb 但是,当我切换到Systemd.service并从那里启动服务器时,没有读取任何环境变量。我还尝试在before_配置挂钩中读取和设置环境变量,但这似乎也没有帮助 设置环境变量 #config/initializers/

我刚刚在Ubuntu 16.04上部署了一个处于生产模式的Rails服务器。当我从下面的命令行启动服务器时,服务器将启动并读取所有环境变量

bundle exec puma -e production -C /home/deploy/shared/config/puma.rb
但是,当我切换到Systemd.service并从那里启动服务器时,没有读取任何环境变量。我还尝试在before_配置挂钩中读取和设置环境变量,但这似乎也没有帮助

设置环境变量

#config/initializers/set_environment_variables.rb
module SetEnvironmentVariables
  class Application < Rails::Application
    config.before_configuration do
      env_file = Rails.root.join("config", 'environment.yml').to_s

      if File.exists?(env_file)
        YAML.load_file(env_file)[Rails.env].each do |key, value|
          ENV[key.to_s] = value
        end # end YAML.load_file
      end # end if File.exists?
    end # end config.before_configuration
  end # end class
end # end module
滚动条初始化器文件-

#/etc/systemd/puma.service
[Unit]
Description=Puma Rails Server
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/current
ExecStart=/bin/bash -lc 'bundle exec puma -C /home/deploy/shared/config/puma.rb'
ExecStop=/bin/bash -lc 'bundle exec pumactl -S /home/deploy/shared/tmp/pids/puma.state stop'
TimeoutSec=15
Restart=always

[Install]
WantedBy=multi-user.target
Rollbar.configure do |config|
  config.access_token = ENV['ROLLBAR_ACCESS_TOKEN']

  unless Rails.env.production?
    config.enabled = false
  end

  config.environment = ENV['ROLLBAR_ENV'] || Rails.env
end
.bashrc

#~/.bashrc
  export ROLLBAR_ACCESS_TOKEN="11111111111"
启动puma.service-日志文件

deploy:~/current/log$ systemctl status puma.service
    ● puma.service - Puma Rails Server
       Loaded: loaded (/etc/systemd/system/puma.service; enabled; vendor preset: enabled)
       Active: active (running) since Tue 2018-04-24 01:34:49 UTC; 3min 23s ago
      Process: 20593 ExecStop=/bin/bash -lc bundle exec pumactl -S /home/deploy/shared/tmp/pids/puma.state stop (code=exited, status=0/SUCCESS)
     Main PID: 20948 (ruby)
        Tasks: 11
       Memory: 133.7M
          CPU: 5.849s
       CGroup: /system.slice/puma.service
               └─20948 puma 3.11.3 (unix:///home/deploy/shared/tmp/sockets/puma.sock) [20180423205334]                                                  
    lines 1-10/10 (END)
滚动条错误日志

[Rollbar] Scheduling item
[Rollbar] Sending item
[Rollbar] Got unexpected status code from Rollbar api: 400
[Rollbar] Response: {
  "err": 1,
  "message": "access token required"
}
[Rollbar] Details: https://rollbar.com/instance/uuid?uuid=xxx-xxx-xxx-xxx-xxexxx (only available if report was successful)  

我相信阿恩瓦尔德是对的。在该链接中,您可以看到如何将变量设置为systemd单位

我在这篇文章中读到,当前的最佳实践是在文件中设置它们


希望有帮助。

有点晚,但在您的服务中,您可以手动或通过文件添加变量

EnvironmentFile=/path/to/file
Environment=MY_VAR=something
注意:
environmentfile
必须以以下模式加载文件:

MY_VAR2=something
MY_VAR3=something_new

我对
puma
没有加载
.env
文件值也有同样的问题。。。我所要做的就是使用
Environment
定义
puma.service
文件中的值,例如:

sudo vi/etc/systemd/system/puma.service

[Unit]
Description=Puma HTTP Server
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/rails_current_server

Environment="RAILS_ENV=production"
Environment="SERVER_TYPE=111"

ExecStart=/bin/bash -lc 'bundle exec puma -C /home/ubuntu/rails_current_server/config/puma.rb'
Restart=always
KillMode=process

[Install]
WantedBy=multi-user.target

另一种选择是在
ExecStart
中对env变量的值进行硬编码,如下所示:
ExecStart=/bin/bash-lc'RAILS\u env=production SERVER\u TYPE=111 bundle exec puma-C/home/ubuntu/RAILS\u current\u SERVER/config/puma.rb'
/p>我相信当您通过systemd启动服务器时,它不会读取您的.bashrc文件。当您尝试从environment.yml文件中读取变量时,您是否可以看到该文件是否存在,以及它产生了什么值?只要在循环中打印一些内容,就可以看到它读取了文件并正确地遍历了其中的值。这是SystemD和Upstart的已知行为。我希望这不会是这个系统的限制,但再次感谢您的确认。我将切换到在yaml/缓存中存储值并在运行时读取。