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

Painless Cron jobs in Crystal using Schedule


Bookmark and Share

Many Ruby developers use the awesome whenever gem for scheduling tasks in their projects, so do we and with ‘whenever’  scheduling tasks become so effortless that we absolutely loved it. Recently we have been deploying Crytal apps to production and Kemal is our framework of choice. During the process, we felt the urge to have something similar to whenever in Crystal for cron jobs and that’s when we discovered Schedule – a Crystal shard that provides a clear DSL to write periodic or scheduled tasks and there was no turning back.

Getting started

Add Schedule to your shard.yml file

you are all set to schedule your tasks, all you gotta do is require the schedule module as require “schedule”

Examples

Schedul’s API defines 2 important methods, .every and .after for periodic and scheduled execution of tasks respectively.

Periodic execution

For running a task periodically we have to pass in a valid interval as well as a block to the .every method. For example,

will print the message on every 3 seconds.

Similarly

will call the class method hourly_job every 1 hour

Scheduled execution

For scheduling tasks to run sometime in the future we can make use of the after method.

Stop and Retry jobs

Call Schedule.retry to retry a task and …

Read More

Deploying Crystal app to production using Heroku


Bookmark and Share

This article assumes that you have Crystal and Heroku CLI installed.

Create a sample application

Create a new application using

You should see the following on your terminal

now cd to the root of the app, $ cd sample-app  and add Kemal to shrad.yml file

and run $ crystal deps for installing dependencies.

Add a “/” route

Open sample-app.cr file and add replace the contents with the following code

now run $ crystal src/kemal_sample.cr  and go to http://localhost:3000on your browser. You should see ur app running and the message ‘Hello World’.

Deploy to Heroku

So far so good 🙂 Now let us deploy our app to Heroku. Run

$ heroku create your-custom-name --buildpack https://github.com/crystal-lang/heroku-buildpack-crystal.git

You should see something like the following on your terminal.

You are all set. Commit and push to heroku

To see your app running go to https://your-custom-name.herokuapp.com/ . Congratulations you just deployed your crystal app to production

Happy Coding

Read More

How we made our rspec test suite to run 2x faster


Bookmark and Share

 

Why on earth is my test suite taking so long to run?

If you are a developer, you might have asked this question at least once to yourself. So did we, our rails project’s test suite was taking a good 1 hour 30 minutes to run and we wanted to improve that time so badly that we eventually did exactly that, reducing nearly 1 and half hours to just minutes, and this is how we did it.

1. Database independent tests

DB operations are often time-consuming and most of the time we can do away with saving objects to the database to run our tests.

Use test doubles, stubs and mocks instead of creating the real instance of a class and invoking methods on the created instance.

Our test case

This test can be made faster by replacing

with

2) Use gem group

Rails preload your gems before running tests. Using gem groups allow rails to load only the environment specific dependencies.

#Gemfile

3) Use before(:each) and before(:all) hooks carefully

Since before(:each) runs for every test, be careful what we include inside before(:each) hook. If the code inside before(:each) is slow every single test in your file is going to be slow.

A workaround would be …

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