How to Daemonize Rails Rake Task on Elastic Beanstalk Start Up

how to daemonize Rails Rake task on Elastic Beanstalk start up

I believe I have found a suitable solution. I installed the ruby gem: "daemons" and switched the script to:

commands:
create_post_dir:
command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
webapp_pids:
command: "mkdir /home/webapp/pids"
ignoreErrors: true
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
chown webapp:webapp /home/webapp/pids
su -l -c "$EB_CONFIG_APP_CURRENT/bin/delayed_job --pid-dir=/home/webapp/pids restart" $EB_CONFIG_APP_USER
echo "worker starting" >> /var/log/directory-hooks-executor.log

So far seems to be working.

How to run a long running job on Amazon EC2 instance in Rails?

For this you can run rake task in background. There are multiple ways to accomplish this. Two of them are here:

  1. Using daemon you can run a rake task in background. Here is link for what is daemon. Here is link, How to achieve this for rails rake tasks. Appending & to rake task is my preferred way.

  2. Implement a cron to run rake task individually.

Are rake tasks suitable for long running processes in production?

You can try delayed job with this extension.

class MyJob

include Delayed::ScheduledJob

run_every 1.day

def display_name
"MyJob"
end

def perform
# code to run ...
end
end

Or manually enqueue another job with Time.now + 5.minutes for example after current job is finished inside perform method.

Too many rake processes spawning on AWS Beanstalk rails application

Looks like every time sneakers rake task was killed, it left behind orphan processes. To rectify I added this as a pre deploy hook:

files:
"/opt/elasticbeanstalk/hooks/appdeploy/pre/04_mute_sneakers.sh":
mode: "000755"
content: |
#!/bin/bash
initctl stop sneakers 2>/dev/null
kill $(ps aux | grep '[/]opt/rubies/ruby-2.0.0-p648/bin/rake' | awk '{print $2}') 2>/dev/null
echo "Killed Sneakers Process"

ElasticBeanstalk Ruby PostDeploy Script Mission Impossible

I am also using elasticbeanstalk on Amazon Linux 2

I am using resque which needs a restart postdeploy. Following is my postdeploy hook which restarts resque workers
.platform/hooks/postdeploy/0020_restart_resque_workers.sh

#!/usr/bin/env bash
. /opt/elasticbeanstalk/deployment/env
cd /var/app/current/
su -c "RAILS_ENV=production bundle exec rake resque:restart_workers" webapp ||
echo "resque workers restarted."
true

Notice the environment variable setup. It simply executes /opt/elasticbeanstalk/deployment/env which will give you the environment.

Hope you are able to use above script by simply replcaing command to restart delayed job instead of resque workers.



Related Topics



Leave a reply



Submit