Booting up Sidekiq with Upstart - ruby-on-rails

My goal is to have sidekiq start when the server boots up (I'm using EC2 with an auto-scaling group). I know there are a few other posts regarding getting sidekiq to start with upstart on boot, but I don't believe mine has been addressed specifically.
I'm using this wiki - https://github.com/mperham/sidekiq/tree/master/examples/upstart/manage-many and have placed the scripts inside /etc/init/sidekiq.conf and /etc/init/sidekiq-manager.conf.
I've made a couple small modifications as directed in /etc/init/sidekiq.conf, changing:
# setuid apps
# setgid apps -> replaced apps with ubuntu in both lines, which is the deployment user.
export HOME=/home/apps to export HOME=/home/ubuntu
I also have a /etc/sidekiq.conf that includes the following line:
/home/ubuntu/app_dir, 2
Otherwise, these scripts are identical to those included in the referenced repo. I'm getting the following errors in my logs (/var/log/upstart)
/bin/bash: line 19: cd: 2: No such file or directory
Could not locate Gemfile
It appears as if it's attempting to change directory somewhere other than /home/ubuntu/app_dir, at which point it's in the wrong directory and cannot find my Gemfile.
Is there somewhere else I need to specify a correct path to my app directory?
Thanks!

You can run sidekiq as an upstart job. Making a sidekiq.conf file in /etc/init/ directory and put the upstart code to run sidekiq.
Here is the complete script and the guide to make sidekiq upstart job.
After making this job, sidekiq start/stop/restart would be easy with sudo service command.

Related

Deploying a Rails Application with Puma and Nginx [without sudo]

All the how-to, tutorial and manuals I can find on nginx and Puma assume the user following them has sudo access on the server.
Is it possible to deploy an application with nginx and puma in an environment in which you do not have sudo?
Presume for the purposes of this question that working tightly with someone who does have sudo to run all the necessary commands when you request them is not an option.
You can build from source without installing it. As long as you aren't trying to use ports under 1024, you should be able to download and compile nginx yourself. I know puma can be installed as a gem, so it wouldn't need to be sudoed in there. I think the syntax once you download the tarball is:
./configure --sbin-path=~/nginx/sbin/nginx --conf-path=~/nginx/etc/nginx.conf --pid-path=~/nginx/pid/nginx.pid
make
Building Nginx from Source
Unless you do some modification to users and groups (which will require sudo) it would be very hard if not impossible to do this.
Assuming Ubuntu and init.d you need sudo to start/stop/reset the services and modify the nginx conf files.
Here is a great resource for automated deployment and easily customize-able. RailsBox.io

Capistrano foreman cannot export upstart scripts because it's trying to run commands as root

I'm using capistrano3-foreman gem to deploy my app into production which is in a centOS server but capistrano is trying to run foreman export command from root. Since I have installed rvm and other stuff from a user which has no password privilege in sudoers file, foreman export cannot be completed.
I'm getting the following error.
sh: /root/.rvm/bin/rvm: No such file or directory
How can I prevent capistrano-foreman from trying to run the command as root and make it set to my user home path.
Thanks in advance
Ok, since RHEL & CentOS 7 migrated to systemd, first mistake was trying to export foreman to upstart.
But When I exported foreman to systemd, systemd did not recognised foreman export scripts as a service so it didn't work either.
After many hours of work & research I decided to take my chance with supervisord on CentOS 7 and now It works like a charm.
http://supervisord.org/installing.html
And please note that Debian & Ubuntu are also getting rid of upstart...

Running Rails Task From Cron

I have a Rails runner task that I want to run from cron, but of course cron runs as root and so the environment is set up improperly to get RVM to work properly. I've tried a number of things and none have worked thus far. The crontab entry is:
* 0 * * * root cd /home/deploy/rails_apps/supercharger/current/ && /usr/local/rvm/wrappers/ruby-1.9.3-p484/ruby bundle exec rails runner -e production "Charger.start"
Apologies for the super long command line. Anyhow, the error I'm getting from this is:
ruby: No such file or directory -- bundle (LoadError)
So ruby is being found in the RVM directory, but again, the environment is wrong.
I tried rvm alias delete [alias_name] and it seemed to do something, but darn if I know where the wrapper it generated went. I looked in /usr/local/rvm/wrappers and didn't see one with the name I had specified.
This seems like a common problem -- common enough that the whenever gem exists. The runner command I'm using is so simple, it seemed like a slam dunk to just put this entry in the crontab and go, but not so much...
Any help with this is appreciated.
It sounds like you could use a third-party tool to tether your Rails app to cron: Whenever. You already know about it, but it seems you never tried it. This gem includes a simple DSL that could be applied in your case like:
every :day # Or specify another period, or something else, see README
runner "Charger.start"
end
Once you've defined your schedule, you'll need to write it into crontab with whenever command line utility. See README file and whenever --help for details.
It should not cause any performance impact at runtime since all it does is conversion into crontab format upon deployment or explicit command. It's not needed, once the server is running, everything is done by cron after that.
If you don't want an extra gem anyway, you might as well check what command does it issue for executing your task. Still, an automated way of adding a cron task is easier to maintain and to deploy. Sure, just tossing a line into the crontab is easier — just for you and just this once. Then it starts to get repetitive and tiring, not to mention confusion for other potential developers who will have to set up something similar on their own machines.
You can run cron as different user than root. Even in your example the task begins with
* 0 * * * root cd
root is the user that runs the command. You can edit it with crontab -e -u username.
If you insist on running cron task as root or running as other user does not work for some reason, you can switch user with su. For example:
su - username -c "bundle exec rails runner -e production "Charger.start"

aws.push command not running .config commands or container_commands

I have a simple setup with a load-balanced application and want to run some commands for cron setups (for instance, using the whenever gem). However, none of my commands seem to get run on the remote server.
# .elasticbeanstalk/Production.config
container_commands:
20_update_crontab:
command: whenever --update-crontab app
leader_only: true
Even tried:
# .elasticbeanstalk/Production.config
commands:
update_crontab:
command: whenever --update-crontab app
Is there something I am missing? These should run with git aws.push correct?
When I check the logs, I don't really get any information saying it was trying to run:
$ eb logs | grep whenever
Using whenever (0.9.2)
The descriptions on this page are pretty good, just can't figure out why it isn't running.
http://www.infoq.com/news/2012/11/elastic-beanstalk-config-files
Elastic Beanstalk configuration files should be placed in .ebextenstions/. It looks like yours are in .elasticbeanstalk/
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html
Create a configuration file with the extension .config (e.g., myapp.config) and place it in an .ebextensions top-level directory of your source bundle.
I had a similar problem where I was trying to install libjpeg for my EC2 instance using the configuration files and it was never installed. I tried everything and was never able to find a "good solution", but I did solve it though.
Solution:
How to solve it? Set up a completely new Elastic Beanstalk Application and deploy the same app again. After I did this it worked from the start with the new EB App.
Check out my other answer here: https://stackoverflow.com/a/23109410/2335675
Check it out, hope it helps for you.

Start ruby rake task on server boot

I have a couple of rails rake tasks that I need to start on server bootup. I have been looking to make them run as traditional services using systemd. The service file is created under /etc/system/systemd but unfortunately it looks like systemd did not find the file:
#systemctl start screens.service
Failed to issue method call: Unit screens.service failed to load: No such file or directory. See system logs and 'systemctl status screens.service' for details.
#systemctl status screens.service
screens.service
Loaded: error
Active: inactive (dead)
I am currently using Fedora 15
How can I make my systemd service work? Is there another way I could make my rake tasks run at system boot.
Update: screens.service file content
[Unit]
Description=Send Reminders
[Service]
Type=simple
User=myuser
WorkingDirectory=/path/to/rails/app
ExecStart=/usr/local/rvm/gems/ruby-1.9.3-p194/bin/rake reminders:send
I know that I need to set dependencies when I need it to work on bootup, but currently I am trying to start my rake using systemctl command and worry about it dependencies later.
First, instead of calling rake directly, I would call /path/to/ruby/bin/bundle exec rake my:raketask.
Second, I think you should really take a look at the foreman gem, as it's designed to handle this situation, though you might have to adapt it work with systemd.
One way would be to call foreman from systemd init file, passing it options to specify the Procfile (foreman config file) in your app's directory.
Foreman can already export its config to several formats, but I don't think yours is among them. But,
you could create a custom exporter that exports the foreman configuration to systemd format. See https://github.com/ddollar/foreman/wiki/Custom-exporters for more information. If you go this route, consider contributing your exporter back to the foreman project.
To make systemd take notice of the newly added services I had to insert the following command:
systemctl daemon-reload
Normally if you need to start something in the application initializer, you could put the .rb file to
config/initializers/ folder
Would you elaborate your requirement, because there might be some other way of doing the same? :)

Resources