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

securing and showing a redis server to the world


Bookmark and Share

redis logo

Redis

Redis is an in memory based key value data structure server. They keywords here are in-memory (RAM) and Key – Value (Hash). RAM being the easily accessible memory location for your CPU, and hash being the most accissable data-structure a combination of both makes it lethal. It was developed by an Italian developer named Salvatore Sanfilippo, in 2009.

Such a system is useful in multiple scenarios. Especially in cases where key is readily available, constant and not changing. Some example use cases being. In a rails projects Redis is used at multiple places for example:

  • Backend of action cable which is used to provide notifications (pub/sub)
  • Queue system used by background workers (Sidekiq, Resque)
  • Web Caching
  • Session Store – sharing user session across all the load balanced servers
  • Fast accessible meta data catalog for your inventory or tool
  • Counting – Redis offers a fast method to increment and decrement value. Being an in-memory storage does add

 

Coming back to the topic of this article, most self hosted rails applications starts off by installing redis in the same server as your rails application. In fact sidekiq, crontab (for scheduling tasks) would all be on the …

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

Materialized Views: Caching database query


Bookmark and Share

As a part of our database optimization series, this article is related to creating materializing views in the database.

Materialzied View

Materialized View Purpose

Before starting with a materialized view, let’s talk about database views.

What is a database view?

A database view is a stored set of queries, which gets executed whenever a view is called or evoked. Unlike the regular tables, the view doesn’t occupy any physical space in your hard disk but its schema and everything is stored in the system memory. It helps abstract away the underlying tables and makes it easier to work with.

They can also be called as pseudo tables.

Quoted from the PostgerSQL documentation.

Making liberal use of views is a key aspect of good SQL database design. Views allow you to encapsulate the details of the structure of your tables, which might change as your application evolves, behind consistent interfaces.

 

Now to access all the managers

 

Making more use of views makes your DB design much cleaner, but here we are talking more about using Materializing views. As that would lead to the more direct performance boost.

So what is a Materialized view?

The materializing view was first introduced in oracle. But …

Read More

Introduction to generating JSON using PostgreSQL


Bookmark and Share

Introduction

One of the major requirements for any online business is to have a backend that either provides or can be extended to provide an API response. Building  websites with static HTML and simple jquery ajax is coming to an end. In this era, Javascript frameworks rules the market. Hence, it is a good decision for the database to support JSON, as JSON is becoming the glue that connects the frontend and backend.

Rails have an inbuilt support for generating JSON, as it’s our swiss army knife of web development, and encourages the REST URL structure . And its a good choice for building API. It is good enough to a particular point of growth. Very soon you will reach bottlenecks, where you have more requests than you can handle and you have to either spawn up more servers or use some concurrent languages like elixir, go, etc. Before we go to that scale and burn down the existing codebase, we can use database to generate JSON responses for us, which is 10 times faster in generating JSON than Rails (though more verbose).

Since PostgreSQL 9.2, the database has taken a major leap in supporting JSON. The …

Read More

New binding.irb introduced in ruby 2.4


Bookmark and Share

Ruby 2.4 will have the feature to introduce a REPL session, using IRB, in between your code execution for better debugging. IRB, which stands for Interactive Ruby, is the standard REPL which is bundled along with ruby. Pry is a popular alternative for IRB, which has many developer-friendly features like tab compilation and syntax highlighting. One of the most heavily used features of pry is the ability to introduce a REPL session in between your code execution for better debugging. Instead of using p or puts to print the result and various variables, this helps us try out various codes and fixes in between the code to find the right solution.

binding.pry being used.

 

To use binding.irb in your code, you need to require the IRB library to your code and call binding.irb where you want to introduce the REPL.

and you will see a REPL like below.

screen-shot-2016-11-22-at-10-47-27-am

 

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

Different types of Index in PostgreSQL


Bookmark and Share

This is part two of our PostgreSQL optimization series. You can read the first article where we discuss when to index here.

PostgreSQL uses a different set of algorithm while indexing tables, each type of algorithm is good for a certain set of data. Here we will be discussing the various algorithms available and when we should be using them. (Note these are the algorithms found in PostgreSQL 9.5)

Algorithms

B-Tree (Balance Tree), is the default algorithm used when we build indexes in Rails. It keeps a sorted copy of our column, which would be our index. So if we want to find the row of the word starting with a then as soon as the words starting with a are over. It will stop searching and return null, as the index has kept everything sorted. It is good in most cases, hence it is the default algorithm used.

Hash is one of the most popular indexing algorithms. But only the equate operator works on it, thus the query planner will only use an index with a hash algorithm if we do an equal operation searching for it. Another point to note is that Hash index …

Read More

How to write maintainable routes in rails


Bookmark and Share

config/routes.rb is the gateway to your ruby on rails application. All request send by your users are directed to the appropriate code by the routes.

Example:

When someone visits your-website.com/profiles then the request is taken to the Index action of the UsersController. Under that action you will get the index.html.erb. So using routes we have configured the UsersController to respond to the users request it is its responsibility to do it now.

We can declare routes in various ways:

Since there are multiple ways to declare routes (as all forms are right), its best to stick with a single method for the code to be more readable. routes.rb is going to be one of the most heavily edited file in your project as when ever you add a new page or create a new form, you need to add a route to access the page or an end-point to accept the request. So it is most likely that your routes.rb file will start to grow ugly:

So here we will share some tips to write proper, maintainable routes:

First important point to note is that, its best to write routes as resources
eg:

 

declaring resources will create the following 7 routes.

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