简体   繁体   English

使用'RAILS_ENV =#{Rails.env} bundle exec用fork调用脚本。 。 '不执行(Rails 2.1)

[英]invoking a script with fork using 'RAILS_ENV=#{Rails.env} bundle exec . . ' doesn't execute (Rails 2.1)

Fairly esoteric question, but for my own edification, I am curious as to how this isn't working. 相当神秘的问题,但出于我自己的兴趣,我很好奇这是如何起作用的。

I have a script fired by a cron running on a legacy system on Rails 2.1 我有一个在Rails 2.1的旧系统上运行的cron触发的脚本

It fetches a list of tasks to execute. 它获取要执行的任务列表。 Since each task may be long running, it backgrounds the tasks using "fork". 由于每个任务可能会长时间运行,因此会使用“ fork”将任务作为后台。

I fetch the pid of the child process. 我获取子进程的pid。 Invoking the process with "RAILS_ENV=Rails.env bundle exec child_script" doesn't work. 使用“ RAILS_ENV = Rails.env bundle exec child_script”调用该过程不起作用。 Dropping the RAILS_ENV does work (Note: running the child script directly from the command line works just fine.) 删除RAILS_ENV确实可以工作(注意:直接从命令行运行子脚本就可以了。)

job_script_path = /path/to/child_script.rb
cmd = "RAILS_ENV=#{Rails.env} bundle exec #{job_script_path}
puts "Preparing to run #{cmd}"
ppid = fork{ system(cmd) }
puts "Fetched #{ppid} as parent process"

cpmd = "ps --ppid #{ppid}"
f = IO.popen(cpmd).readlines
puts "Output of #{cpmd}:"
puts "#{f}"

Invariably outputs: 始终输出:

Preparing to run RAILS_ENV=development bundle exec /path/to/child_script.rb
Fetched 1234 as parent process
Output of ps --ppid 1234
PID TTY          TIME CMD

No process listed. 没有列出进程。

If I drop the RAILS_ENV=#{Rails.env} It works just fine. 如果我放下RAILS_ENV =#{Rails.env},它就可以正常工作。 What about setting the rails environment forces atomicity? 设置Rails环境强制原子性怎么样?

UPDATE: This is more of a Ruby question. 更新:这更多是一个Ruby问题。 I'm running . 我在跑 。 . . . ruby 1.8.7 红宝石1.8.7

Apparently, you cannot fork a concurrent process that runs in a Rails environment. 显然,您不能派生在Rails环境中运行的并发进程。 You can, however, fork a concurrent process that does not rely on a Rails environment. 但是,您可以派生不依赖Rails环境的并发进程。

#Do not prepend RAILS_ENV to this, it doesn't allow concurrency (for some reason.)
cmd = "bundle exec #{job.script_path}"
logger.info "Preparing to run #{cmd}"
ppid = fork{ system(cmd) }
logger.info "Fetched #{ppid} as parent process"

Stripping out the Rails env works. 剥离Rails的环境。 I didn't find a definitive resource as to why this is. 我没有找到确定为什么的确切资源。 A Rails environment is somehow monolithic. Rails环境是单块的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM