5

I'm having trouble getting delayed_jobs running on Elastic Beanstalk. I'm using the 64bit Amazon Linux 2014.03 v1.0.0 running Ruby 2.1 (Passenger Standalone) container.

This is my config script (delayed_job.config) ...

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh":
    mode: "000755"
    owner: root
    group: root
    encoding: plain
    content: |
      #!/usr/bin/env bash
      . /opt/elasticbeanstalk/support/envvars
      cd $EB_CONFIG_APP_CURRENT
      su -c "RAILS_ENV=production bin/delayed_job --pid-dir=$EB_CONFIG_APP_SUPPORT/pids restart" $EB_CONFIG_APP_USER

The 99_restart_delayed_job.sh script exists and runs ... but then I stumble into this error.

2014-10-02 15:28:32,332 [INFO] (17387 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Script succeeded.
2014-10-02 15:28:32,402 [INFO] (17448 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Executing directory: /opt/elasticbeanstalk/hooks/appdeploy/post/
2014-10-02 15:28:32,402 [INFO] (17448 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Executing script: /opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh
/usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- bundler/setup (LoadError)
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /var/app/current/config/boot.rb:4:in `<top (required)>'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /var/app/current/config/application.rb:1:in `<top (required)>'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /var/app/current/config/environment.rb:2:in `<top (required)>'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from bin/delayed_job:3:in `<main>'

2014-10-02 15:28:32,440 [ERROR] (17448 MainThread) [directoryHooksExecutor.py-33] [root directoryHooksExecutor error] Script /opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh failed with returncode 1

I've already poured through this other thread on SO that showed me how to set up. My problem is that I don't know what's preventing the script from running without error.

If I SSH into the EC2 instance, I'm able to run this without error ...

RAILS_ENV=production bin/delayed_job --pid-dir=$EB_CONFIG_APP_SUPPORT/pids restart

While this asks me for a password ...

su -c "RAILS_ENV=production bin/delayed_job --pid-dir=$EB_CONFIG_APP_SUPPORT/pids restart" $EB_CONFIG_APP_USER

Which I can avoid by doing this ...

sudo su -c "RAILS_ENV=production bin/delayed_job --pid-dir=$EB_CONFIG_APP_SUPPORT/pids restart" $EB_CONFIG_APP_USER

See: 'How to automatically restart delayed_job when deploying a rails project on Amazon Elastic Beanstalk?'

Update 1: 2014-10-15

After applying the -l option with the change of directory passed in, I get this error ...

2014-10-15 06:17:28,673 [INFO] (4417 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Executing script: /opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh
2014-10-15 06:17:30,374 [INFO] (4417 MainThread) [directoryHooksExecutor.py-29] [root directoryHooksExecutor info] Output from script: /opt/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/daemons-1.1.9/lib/daemons/application.rb:393:in `kill': Operation not permitted (Errno::EPERM)
    from /opt/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/daemons-1.1.9/lib/daemons/application.rb:393:in `stop'
    from /opt/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/daemons-1.1.9/lib/daemons/application_group.rb:171:in `block (2 levels) in stop_all'

2014-10-15 06:17:30,374 [ERROR] (4417 MainThread) [directoryHooksExecutor.py-33] [root directoryHooksExecutor error] Script /opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh failed with returncode 1

Update 2: 2014-10-15

Turns out the error above was caused by an existing pid created by root (while debugging I had started delayed_job manually) so the c2-user couldn't restart/kill it hence the error.

Community
  • 1
  • 1
King'ori Maina
  • 4,440
  • 3
  • 26
  • 38

1 Answers1

1

The problem, from what I can tell, is that the environment/path variables aren't being established when switching to the $EB_CONFIG_APP_USER linux user. I made three changes:

  1. Add the -l option to the su command to simulate a full login of $EB_CONFIG_APP_USER.
  2. As a side effect of the -l option, the change directory command must be brought into the -c option.
  3. As a good measure, but maybe not necessary, include bundle exec to ensure that the proper gems are being used.

Here's my functioning content: area:

#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
su -l -c "cd $EB_CONFIG_APP_CURRENT && RAILS_ENV=production bundle exec bin/delayed_job --pid-dir=$EB_CONFIG_APP_SUPPORT/pids restart" $EB_CONFIG_APP_USER
YWCA Hello
  • 2,997
  • 4
  • 29
  • 40
  • I applied the suggested change and got a new error (see update to the question) but yes, your suggestion makes sense. This certainly points me in the right direction so I'll have to SSH into my instance and do this manually until something works. Will update on progress. – King'ori Maina Oct 15 '14 at 06:27
  • 1
    Your error seems to indicate that `$EB_CONFIG_APP_USER` doesn't have permission to stop the already running process. Make sure that `bin/delayed_job` isn't running in the background as root from your previous troubleshooting steps. Also verify the file permissions of `bin/delayed_job`. – YWCA Hello Oct 15 '14 at 15:59
  • 1
    Apparently the environment variables went away with the next Beanstalk (2014.09). Workaround in [this AWS forum thread](https://forums.aws.amazon.com/thread.jspa?messageID=602937). Details on delayed_job with the new approach in [this answer](http://stackoverflow.com/a/28506920/550712) and [this blog post](http://www.dannemanne.com/posts/post-deployment_script_on_elastic_beanstalk_restart_delayed_job). – Mark Berry Feb 19 '15 at 02:03