I want to be able to load the gem needed to run when I run an external Ruby script using Open3 or Daemons.

Asked 1 years ago, Updated 1 years ago, 72 views

I'm stuck, so please tell me.
Rais, Ruby is writing for the first time, so you may be misguided.

I wanted to run the capture3 method of Open3 from Rails to launch the daemon, but it turned out to be "cannot load Such file".

#top_controller.rb (call processing from Rails)

def daemon_start
    cmd = "/bin/app start;"
    out, err, status = Open3.capture3 (cmd)
    pout               
    perr              
    p status.exitstatus

    redirect_to root_path
end


#app (app called)

#!/usr/bin/envruby
require 'bundler/setup'
require 'daemons'
require 'pathname'

base_dir=Pathname('../../').expand_path(__FILE__)
Daemons.run(
  base_dir.join('bin/bot'),
  app_name: 'bot',
  dir_mode: —Normal,
  dir —base_dir.join('tmp'),
  log_dir —base_dir.join('log'),
  log_output —True
)


#Display contents (err)

"bin/app:3:in `require':cannot load search file --daemons(LoadError)\n\tfrom bin/app:3:i"


Here's what I've tried.

  • Run app from terminal
    → Boot without any problems

  • Add Gem Path in ruby-I
    US>cmd
    cmd = "ruby-I/Users/owner/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.3/lib/bin/app start;"
    If you change it to , the error does not appear.
    (If you pass /Users/owner/.rvm/gems/ruby-2.2.0/gems/ in -I, it's the same as the first error.)

  • Change required path to absolute path
    Similarly, no errors appear.

  • Do $LOAD_PATH.unshift in the app
    $LOAD_PATH.unshift('/Users/owner/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.3/lib')
    Similarly, no errors appear.
    However, as with the above two, I cannot find the required gem in the bot called by the app.

Run app from terminal
→ Boot without any problems

Add Gem Path in ruby-I
US>cmd
cmd = "ruby-I/Users/owner/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.3/lib/bin/app start;"
If you change it to , the error does not appear.
(-I pass /Users/owner/.rvm/gems/ruby-2.2.0/gems/ to the first error)

Change required path to absolute path
Similarly, no errors appear.

$LOAD_PATH.unshift in app
$LOAD_PATH.unshift('/Users/owner/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.3/lib')
Similarly, no errors appear.
However, as with the above two, I cannot find the required gem in the bot called by the app.


#What should the error display at that time?
(I can't find the required file in the retryable where bot is required)

/Users/owner/.rvm/gems/ruby-2.2.0/gems/retryable-2.0.2/lib/retryable.rb:1:in `require':cannot load Such file --retryable/version (LoadError)

[Question]
If you run the app from the terminal, it will work normally, so I think the problem is that the path is not passing.
It may be possible to do the same thing with all the gems as I tried above, but I would like to avoid it because there are a certain number.
Is there a better way to pass?
Please let me know anything.
I will add any missing information.
Thank you for your cooperation.

[Environment]
Rails:4.1
Ruby: 2.2.0 (used by Rails)
Ruby: 2.2.0 (used in app)

ruby-on-rails ruby rubygems bundler

2022-09-30 20:24

2 Answers

After the Rails side bundle installation of the gem needed to run the app, it was able to start for now.
However, there may be a better way, so I look forward to hearing from you.


2022-09-30 20:24

## in the /bin/app
require 'bundler/setup'

What the does is it climbs up from the current directory and sets the gem to LOAD_PATH using the first Gemfile information it finds.Furthermore, since the load_path information of the globally installed gem should not be covered by the bundleer, clean_load_path may not be able to read all of the bundle correctly.

Perhaps daemon and app are ruby applications that are developed independently. If rails are unique, prepare Gemfile for them and set them up as one project.Then, make sure that Gemfile in the app is used when you run Open3.

For example:

in
#top_controller.rb
cmd = 'cd/path/to/app/dir&bundle exec bin/app'
Open3.capture 3cmd
# ...

Or explicitly specify the Gemfile you want to use

#app (app called)
ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile',__dir__)
# =>(Specify Gemfile as the relative path from the directory where the app file is located)

Also, it's one of the ways to manage the rails project, including daemon scripts.


2022-09-30 20:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.