Skip to content

Instantly share code, notes, and snippets.

@ssaunier
Created September 24, 2015 09:30
Show Gist options
  • Save ssaunier/44bbebb9c0fa01953860 to your computer and use it in GitHub Desktop.
Save ssaunier/44bbebb9c0fa01953860 to your computer and use it in GitHub Desktop.
Running Sidekiq on AWS Elastic Beanstalk (Put that file in `.ebextensions` folder)
# Sidekiq interaction and startup script
commands:
create_post_dir:
command: "mkdir -p /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/50_restart_sidekiq.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
EB_APP_DEPLOY_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
EB_APP_PID_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_pid_dir)
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)
. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
SIDEKIQ_PID=$EB_APP_PID_DIR/sidekiq.pid
SIDEKIQ_CONFIG=$EB_APP_DEPLOY_DIR/config/sidekiq.yml
SIDEKIQ_LOG=$EB_APP_DEPLOY_DIR/log/sidekiq.log
cd $EB_APP_DEPLOY_DIR
if [ -f $SIDEKIQ_PID ]
then
su -s /bin/bash -c "kill -TERM `cat $SIDEKIQ_PID`" $EB_APP_USER
su -s /bin/bash -c "rm -rf $SIDEKIQ_PID" $EB_APP_USER
fi
. /opt/elasticbeanstalk/support/envvars.d/sysenv
sleep 10
su -s /bin/bash -c "bundle exec sidekiq \
-e $RACK_ENV \
-P $SIDEKIQ_PID \
-C $SIDEKIQ_CONFIG \
-L $SIDEKIQ_LOG \
-d" $EB_APP_USER
"/opt/elasticbeanstalk/hooks/appdeploy/pre/03_mute_sidekiq.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
. /opt/elasticbeanstalk/support/envvars
EB_APP_USER=$(/opt/elasticbeanstalk/bin/get-config container -k app_user)
EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
EB_SUPPORT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_dir)
. $EB_SUPPORT_DIR/envvars
. $EB_SCRIPT_DIR/use-app-ruby.sh
SIDEKIQ_PID=$EB_APP_PID_DIR/sidekiq.pid
if [ -f $SIDEKIQ_PID ]
then
su -s /bin/bash -c "kill -USR1 `cat $SIDEKIQ_PID`" $EB_APP_USER
fi
@niinyarko
Copy link

Work perfectly. Thanks.

@andey
Copy link

andey commented Dec 28, 2016

can confirm this works out of the box, no modifications necessary for a default sidekiq installation.

64bit Amazon Linux 2016.09 v2.3.0 running Ruby 2.3 (Passenger Standalone) & sidekiq (4.2.2)

@maxdbn
Copy link

maxdbn commented Jan 22, 2017

Thank you for this wonderful gist, it helped me a lot but it's not perfect.
While using this script as it is it can make some trouble because it's not waiting for the already busy jobs to finish, therefore not killing the sidekiq process "gracefully".

I would suggest using the gist I've made:
https://gist.github.com/MaxDBN/b80d6e5fac7f1a05f2727474ff172ea6

@josephecombs
Copy link

Confirmed working on Passenger with Ruby 2.3 running on 64bit Amazon Linux/2.7.1 on 02/22/18

@joshtab
Copy link

joshtab commented Mar 23, 2018

This script is working well for me. Some minor modifications for sidekiq 5.0 here

@waissbluth
Copy link

is this meant to be run in a web or a worker environment?

@mst209
Copy link

mst209 commented Aug 21, 2018

Same question as @waissbluth. is this meant to be run in a web or a worker environment?

@john-verys
Copy link

john-verys commented Aug 31, 2018

@waissbluth @mst209 I'm assuming a web server environment because Sidekiq polls Redis, whereas worker environments poll SQS and then makes HTTP requests to itself on localhost:80.

@jasonswett
Copy link

Can confirm that this file worked for me without modification. FWIW, I'm running mine in a web environment.

@rokumatsumoto
Copy link

rokumatsumoto commented Oct 28, 2019

It is working on Sidekiq 5.2.7 but not working on Sidekiq 6

/var/log/eb-activity.log (Sidekiq 5.2.7)

WARNING: PID file creation will be removed in Sidekiq 6.0, see #4045. Please use a proper process supervisor to start and manage your services
  WARNING: Logfile redirection will be removed in Sidekiq 6.0, see #4045. Sidekiq will only log to STDOUT
  WARNING: Daemonization mode will be removed in Sidekiq 6.0, see #4045. Please use a proper process supervisor to start and manage your services

See
sidekiq/sidekiq#4045
sidekiq/sidekiq#4307

@tannakartikey
Copy link

On platform, "Ruby 2.7 running on 64bit Amazon Linux 2", initctl command from upstart does not work. Seems we need to use systemctl to manage the processes.

@ali-ehmed
Copy link

These scripts won't work for Sidekiq >6. Does any one come by chance with updated scripts?

@austinwalter
Copy link

austinwalter commented Oct 7, 2021

It seems the new solution for Amazon Linux 2 is to use a Procfile to manage Sidekiq.

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ruby-platform-procfile.html
https://github.com/mperham/sidekiq/wiki/Heroku

If you don't want to use a Procfile then I think the Sidekiq project also provided an example of how to perform the setup using systemd

https://github.com/mperham/sidekiq/blob/main/examples/systemd/sidekiq.service

@x1wins
Copy link

x1wins commented May 9, 2023

this gist was not working in elastic beanstalk for sidekiq.

@x1wins
Copy link

x1wins commented May 9, 2023

It seems the new solution for Amazon Linux 2 is to use a Procfile to manage Sidekiq.

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ruby-platform-procfile.html https://github.com/mperham/sidekiq/wiki/Heroku

If you don't want to use a Procfile then I think the Sidekiq project also provided an example of how to perform the setup using systemd

https://github.com/mperham/sidekiq/blob/main/examples/systemd/sidekiq.service

Amazon Linux 2 and don't support the Procfile feature.

We can't use Procfile.
Did you use systemd for sidekiq?

@ssaunier
Copy link
Author

ssaunier commented May 9, 2023

Sorry, @lewagon has migrated away from Beanstalk 2 years ago, to Scalingo, I can't provide much help on that one :/

@x1wins
Copy link

x1wins commented May 10, 2023

@ssaunier Thanks for update.

And I comment for someone who want to how to setup sidekiq on EB.
i found solution with predeploy, postdeploy
sidekiq/sidekiq#4307 (comment)
https://www.barot.us/running-sidekiq-on-amazon-linux-2/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment