Concurrent-Ruby


Bookmark and Share

Concurrent Ruby

Concurrent-ruby is a gem that was brought my attention during Anil Wadghule’s talk in RubyConf India 2017.

Concurrency is the ability of a program to make progress on a task that is spread out over different slices of time. It allows us to run multiple “threads” at the same time. As one thread may be sleeping or waiting on I/O, another thread may take priority and start working, thus making optimal use of available CPU time. When you think of concurrency, think of “threads”.

Modern concurrency tools include agents, futures, promises, thread pools actors, supervisors etc.Concurrent Ruby makes the strongest thread safety guarantees of any Ruby concurrency library. Every abstraction in this library is thread safe. Similarly, all are deadlock free and many are fully lock free.

Concurrency Abstractions

  • Async (Concurrent::Async): A mixin module that provides simple asynchronous behavior to a class, turning it into a simple actor.
  • Future (Concurrent::Future): An asynchronous operation that produces a value. It represents a promise to complete an action at some time in the future.
    • Dataflow: Built on Futures, Dataflow allows you to create a …
Read More

Integrating Elm with Rails


Bookmark and Share

 

Front-end languages and frameworks are changing significantly over years. The trend is towards light-weight, modular architecture. Functional programming has influenced JavaScript and its frameworks a lot. For beautiful single page web applications, Elm is a framework that can be chosen. It gets compiled to efficient JavaScript code. But when to use Elm instead of JavaScript?  If you are building complicated single page applications Elm can do better.

Elm is a functional programming language created by Evan Czaplicki in 2012 for building reliable Web Applications. Elm is simple to use and offers much quality. Its architecture is a simple pattern for building web apps, that help you to add features quickly. Also, we can use Elm in existing projects as it can be used along with already written JavaScript code.

Why Elm?

Switching to functional programming languages makes it a better environment for multi-threaded applications. For example, immutability is a powerful functional concept that JavaScript lacks. But in Elm, once created value cannot be changed, thus making a thread-safe environment. Each thread need not worry about other threads when they act on data since these data are represented by immutable objects in Elm.

While in other languages it’s hard …

Read More

The future of Spree OSS and whats new in Spree 3.3


Bookmark and Share

The story so far

Ever since its advent in 2007, Spree – originally developed by Sean Schofield, is the most popular open source Ruby On Rails E-commerce Software spanning over 17,703 commits, 725 contributes and 208 releases. Spree was acquired by First Data in the late 2015 and since then, there has been fair amount of speculation around Spree remaining as an opensource project and it’s long term future as outlined here in this blog post.

As it turned out Spree didn’t die off, but revived under a new core team. After the acquisition announcement developers from Spark Solutions and Vinsol volunteered to keep spree alive and has been doing a fantastic job, putting shoulders to the wheel by taking Spree from version 3.0.4 to the current Spree 3.3

Whats new in Spree 3.3

On August 22, 2017, Spree released version 3.3 and is packed with the following major features.

Rails 5.1 support

Spree 3.3 is Rails 5.1  compatible whereas Spree 3.2  runs on Rails version 5.0. Which means managing JavaScript dependencies from NPM via Yarn, ability to use direct & resolved routes, parameterized mailers and many more goodies Rails 5.1  has …

Read More

Getting Started with Vue.js


Bookmark and Share

 

Vue.js

What is Vue.js?

Vue.js is yet another JavaScript framework getting popular considering its simplicity praised a lot these days. There are a lot of JavaScript frameworks among which React and Angular are popular among web developers. Vue.js pronounced as “view.js”  is a much flexible and less opinionated framework than Angular. It’s similar to React but much simpler.

Vue.js gives you more freedom in designing your app, unlike Angular. So that you are not forced to do everything in their way. It can be adapted very easily to your existing application. You don’t need to know JSX, unlike React. All you have to do is to drop the link into your HTML page header and you are up and ready.

Overview

This is the basic folder structure.

– index.html

This is the main HTML template for your application. You can link your static assets inside the head tag while your processed assets will be auto injected in the body.

– src/router/index.js

This is the file which initiates vue-router with the given …

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

Custom Loggers


Bookmark and Share

Custom loggers are useful if you wish to have a separate log file for different moving parts of your Rails application. This would make your logging more efficient and readable. (Eg: a file to record all the API request you received). Let’s have a look on them!

Note: This could cause an issue of log files growing and consuming the entire space in the hard disk can be managed by configuring Rails application. It is described in our previous article Enable log rotation within the rails application.

Rails make use of ActiveSupport::Logger class to write log information. It is used to output message and has associated levels. You can then give the Logger a level, and only messages at that level or higher will be printed. They are:

UNKNOWN
An unknown message that should always be logged.

FATAL
An un-handleable error that results in a program crash.

ERROR
A handleable error condition.

WARN
A warning.

INFO
Generic (useful) information about system operation.

DEBUG
Low-level information for developers.

CREATE YOUR CUSTOM LOGGER

It creates a custom_logger.rb file. Here defines the format for log messages. The arguments are:

  • Severity of the log message
  • Time instance representing when the message was logged
  • progname configured or passed to the logger method
  • Object the user passed to the …
Read More

Managing threads with Queue and SizedQueue


Bookmark and Share

Threads in Ruby

To make our applications do multiple things faster at the same time, we can use Ruby threads. For a long-running application, re-using a pool of such threads can provide great performance benefits. Thread pool is a way to have a fixed set of threads doing the work, instead of creating new ones each time.

Considering a simple program as below, we realize the importance of threads.

You will get the sum of each array as output but the sleep(4) instruction will pause execution for 4 seconds and then continue. Thus, group1 will get the sum after 4 seconds, group2 after 8 seconds and so on, which is not feasible. In such cases, it is more economical to have threads, since we can have the sum of each array calculated independently.

Threads allow us to execute different parts of our program independently. For implementing threads, after initializing each array,

The add_element method definition is same but we wrapped method call in a Thread.new block.

Now, instead of getting the sum of each array after 4 seconds, 8 seconds and 12 seconds respectively, you will get the sum of all arrays after 4 seconds. This indicates better performance and efficiency …

Read More

Make unit tests great again – Integrate Jasmine into Rails


Bookmark and Share

Jasmine is a framework to write tests for the Javascript code in the Behaviour Driven Development (BDD) style. In this article, you will learn how to integrate Jasmine into your existing rails application and how to write clean and beautiful unit tests. Let us make tests great again!

 

Install Jasmine

To make Jasmine available to your Rails app, you just have to place the jasmine-gem (link) in your Gemfile. That will do the magic. Just make sure you have it under Development and Test group in the Gemfile as follows:

Then run this to install the gem:

After all the gems are installed, run this code to generate necessary files for Jasmine to run:

This will create the jasmine helper file and the yml file where you configure how it should run the tests.

Run tests

You can use Jasmine right after it’s installed. It can be run in several ways, the most important ones being,

  • In your browser
  • Continuous Integration Mode (CI)
  • The CI mode is usually used when you have to integrate it into your build system.

    Browser Mode

    You have to start the Jasmine server to run it in a browser. This server runs all the tests and serves the results to a webpage. …

    Read More