Background Workers using Crontab


Bookmark and Share

Scheduling background jobs is a common task in rails application development. Eventually what we want is a cron job which runs the schedule jobs.

cron is the system process which will automatically perform tasks for you according to a set schedule. The schedule is called the crontab, which is also the name of the program used to edit that schedule.

For example, let’s say you have a rake task which you want to run every hour.

To edit the crontab, use this command:

Now let’s add our job to the crontab. Each job you add should take up a single line.

The format is very simple: six pieces of information, each separated by a space; the first five pieces of information tell cron when to run the job, and the last piece of information tells cron what the job is.

m, representing the minute of the hour, h, representing the hour of the day, dom, representing the day of the month, mon, representing the month of the year, dow, representing the day of the week and  command, which is the command to be run. For example in our case

The asterisks (“*“) will tell cron that for that unit of time, the job should run ‘every’. …

Read More

Using mina to deploy a particular commit


Bookmark and Share

Mina is our tool of choice when it comes to automating our deployment. It might not be the best tool out there, but it is enough to get us started. Single server or multi server, we love Mina.

In most of uses cases if something fail in production we just need to rollback to one or two version before to it. But recently we had a case in which we had to revert to version order than the previous 5 version (5 is default no of versions stored). The quick thing that we did at that point is copy of master called temp-master branch and changed master to the version that we wanted and had the new master deployed.

We didn’t like that solution much, so we decided to look for a cleaner solution. Looking at the source code of the git task inside mina, we found that mina had an option to deploy a particular git commit. The config was set :commit, HASH (Thee cheers to the contributors to mina for having that).

Now that made our life easier for the future. We just added the following line to our deploy.rb

set :commit, ENV[‘COMMIT’]

so now we can …

Read More

Deploying Rails application on AWS, using Nginx, Puma and Mina


Bookmark and Share

In this tutorial we are going to setup an AWS EC2 instance for deployment of rails application.

Server Setup

Following is the setup instruction for ubuntu 16.04 using ruby 2.4, Postgres 9.6 and rails 5.

Installing Ruby

Setting up dependencies for ruby.

Next step is to install ruby using RVM.

Last step is to install bundler.

Installing Rails

Installing NodeJS, this lets you use Coffee script and the Asset Pipeline in Rails which combines and minifies your javascript to provide a faster production environment.

Next is to install rails.

You can verify the installation of rails version.

Installing PostgreSQL

The postgres installation doesn’t setup a user for you, so you’ll need to follow these steps to create a user with permission to create databases.

Install Nginx

Following are some basic commands to manage Nginx:

Nginx Configuration

First disable default site by removing the symlink.

Now create a new virtual host config file.

edit the my_app.conf file:

Puma Configuration

Change config/puma.config file according to following example:

Create puma upstart script

Let’s create an Upstart init script so we can easily start and stop Puma.

Now open the provided puma.conf file, so we can configure the Puma deployment user.

Look for the two lines that specify setuid and setgid, and replace “apps” with the name of your deployment user and …

Read More

[Tip] Mina: Find last git commit released


Bookmark and Share

A project that we were handling hadn’t had a deployment in some time. So we were confused on what all commits were going to go to production ( bad karma for us for not keeping a release or change log). We use mina for all our deployment as we found it to be faster that Capistrano. So we were sure that there would be something on our server to help us settle this dilemma. We finally solved this by going through the various folders and files.

Edit the file inside the file with your branch name located at /path/to/project/scm/refs/heads. Like for example if you have your project in /var/www folder and you are deploying master then you should edit the file called master found at the following location./var/www/project/scm/refs/heads/master that file would have only one line and that’s the last git commit released.

 

Read More

Deploying Sidekiq to Ubuntu 16.04


Bookmark and Share

Sidekiq is a popular background processing tool available in Ruby. It’s fast, robust and reliable compared to other solutions out there. Sidekiq run as a process outside of rails (but including the rails environment), which means it doesn’t start when you start your rails application. During development, we start sidekiq in another terminal (or tab) using the command

to run it as a daemon we use the -d option

To kill a sidekiq daemon, you need to do  the PID of the sidekiq process. When a sidekiq process starts it enters its pid to file which can be found at

So the command to stop it would be

But making it a daemon is not a good idea, as there is no code from sidekiq to restart the process when it fails or exits on its own. So in ubuntu, which is our favorite OS for the production server, we make sidekiq a systemd process.

Before we make it into a service and if you are using rvm you need to create a wrapper for systemd so that ruby with all the gems are available for it.

Once that is done you need to create a sidekiq.service file under your ‘/etc/systemd/system/‘. You can find …

Read More

Mina: Faster deployment and remote server automation


Bookmark and Share

Even though we use CodeShip/Circle CI/Jenkins for continuous integration, we still need to write scripts to automate our deployment. We also need to write small commands to clear cache, restart queues, etc. It’s always a good practice to not have to enter the server directly but to have it done through scripts. There are many tools available for this purpose (in multiple languages) like Capistrano, Vlad, etc.

Mina is a similar tool, but faster. The reason why it runs faster is because it generates a bash script, uploads it to the server and run there, rather than creating a new ssh session and run every command one by one. Mina is one of our default tools at Red Panthers.

To use mina in your project, add mina to your Gemfile.

and to get started do

 

http://nadarei.co/mina/setting_up_a_project.html
https://www.digitalocean.com/community/tutorials/how-to-deploy-with-mina-getting-started

 

Having a one step deployment is an important requirement for any project so have one ready using mina or capistrano.

 

Note:

Assets pre-compile

 

Run another rake task

Run shell commands

The syntax queue is referring to the fact that, the commands are all made into a single shell script and then later executed together.

 

Read More

Enable log rotation within the rails application


Bookmark and Share

Any person who has worked with a rails application for a good amount of time would have faced the issue of log files growing and consuming the entire space in the hard disk. Although this sounds funny, it does happen if you do not place a log management for your rails app (in production). You can manage your logs using log rotation. Do note that log rotation is not required if you host your app in heroku, as heroku won’t let you store files for more than 15 minutes in their server. You also do not need it if you are using some third party services like loggly or papertrail to manage your logs .

In the unix world, you can use log rotate service, which would be installed by default in all linux servers. But we at Red Panthers feel that everything that touches or involves our rails application should be placed along with our rails application as much as possible. If you feel the same way, it can be achieved by placing the code below in the production.rb file.

The Logger.new accepts three parameters:

  • The file name, which would be production.log
  • The number of files you want to keep
  • The size of …
  • Read More