Pages - Menu

Wednesday, March 26, 2014

Making Riak execute a function on a specific node

Hello good reader, I hope you're doing awesome!

First things first, this is the code for the problem I had, if you have the time and interest, keep reading to understand what happened =)



That's it, the code is not the best but solves my problem.

Explanation:

The function validate_license is receiving a list [License|Host] with the License and the Host where it needs to compare the licenses.

It was necessary to make an rpc:call instead of mapreduce to be sure the function was going to run on the specified host(node).

I need to send a Node, Module, Function, and the Args to rpc:call.

I had to use string:join because the Host was a list and string:concat to concatenate the 'riak@' with the Host that was passed so I could get something like this: riak@127.0.0.1.

list_to_atom because rpc:call expects an atom as the node, you can't pass a string, to finish, walk(module), check_license(function) and [0,0, [License]](check_license parameters).

With that code I'm able to run a specific function on a specific node.

Details, details

The scenario: I have two nodes running with a library we have at work.

The problem: We had to run a function that generates a serial number based on the machine id and when the function was called Riak was the responsible to decide on which node to run it and with that we had a problem. How could we make sure that our function that checks the serial number would check for the serial number in the correct node? We couldn't.

We have a bucket where we store the encrypted license. The function validate_license had to check if the encrypted license sent to it by another application was the same on the bucket. So my application send the license to the node A but some times Riak would send that request to the node B and the function would return an error.

The solution: It was necessary to guarantee that the function was going to be executed in the correct node so it could check the licenses correctly.

That's all.

I'd really appreciate any feedback on that since my experience with Riak and Erlang are 0.1%.

Today, be nice with someone you're not fond of, this will make your day better.

Power to you!

Saturday, October 12, 2013

Friday, October 11, 2013

Setting up a Rails environment with RVM

Hi, happy? I hope so :)

So here I'll show you how to setup a Rails environment using RVM, if you are a Windows user, I'm sorry, RVM is not for you but you can do it using a VM.

We are going to install RVM that stands for Ruby Version Manager and create some Gemsets with different Rails, Ruby and Gem versions.

Install RVM

$ curl -L https://get.rvm.io | bash -s stable

You can also append some options like --rails, --ruby or specify the version like --ruby=1.9.3.

After the installation there will be a folder .rvm on your home directory, that's where all the magic will happen. If something goes wrong in the future you can remove that folder and install again but all your Gemsets, Gems and Rubies will be gone as well, be careful.

When the installtion is over it will automatically update your .bash_profile adding this:

source $HOME/.rvm/scripts/rvm

Which will make your RVM recognizable on your terminal as a function, otherwise it won't work. If for some reason it doesn't happen, you can add it by yourself like:

echo "source $HOME/.rvm/scripts/rvm" >> ~/.bash_profile

When that's done you must reload your dotfile so the new line take effect:

. ~/.bash_profile

Or you can close and reopen your terminal :)

OK, now you have RVM working as a function and the next step is to check your OS requirements for it to work.

rvm requirements


In my case, using Manjaro, it has no requirements but when it does it will start downloading and in some cases it must be run as sudo.

To check what is available for you to use like ruby, jruby, macruby, etc:

rvm list known


All those options are available for you to install like this:

rvm install your_option_goes_here

So let's install Ruby 1.9.3. As you can see it has 1.9.3[-p448]

rvm install 1.9.3-p448

This step will download, extract, configure and compile so it may take some time depending on your CPU and connection and if something goes wrong during one of these steps, be calm and read carefully the messages given, they are really helpful.

When Ruby is installed you can check it:

ruby -v

Which will return, in case you installed 1.9.3-p488, something like this:

ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]

You can install any Ruby version you wish following the above step and to use it you do this:

rvm use 1.9.3

Let's suppose you also have Ruby 2.0.0 installed, to use it:

rvm use 2.0.0

You can also reinstall it. That will happen in case your OS doesn't have a required library, e.g.: libyaml and you have already installed Ruby

rvm reinstall RUBY_VERSION

And voilà

That's it about installing Ruby.

Now let's work with Gemsets. This is how you'll organize all your Gems so you don't make a mess.

First you need to check all the available Gemsets

rvm gemset list

It will list a (default) and global Gemsets. If you do not specify the Gemset where you want to install a gem, it will be installed in the default Gemset.

The global Gemset has some Gems that are used frequently like bundler, json, rake, rdoc, etc, to have a complete list use gem list.

Now you'll create a Gemset called my_project and we'll install rails 3.2.14

rvm gemset create my_project

You can check it using rvm gemset list

IMPORTANT: For each and every Ruby version there will be a different Gemset list. To test it, change your Ruby version (rvm use 1.9.3 or whatever) and check it's Gemset list.

To use that Gemset

rvm use 1.9.3@my_project

Now install your gems

 gem install rails -v 3.2.14  

When the process is over, you can check it using

rails -v

And that Rails version will be available only when using that Gemset.

Obviously you can create lots and lots of Gemsets and each with lots and lots of Gems that will not conflict with each other.

Now let's suppose you'll create a project using Rails 4.0.0

You'll create a new Gemset for that project

rvm gemset create my_new_project

rvm use 1.9.3@my_new_project

gem install rails

Done, you have now two Gemsets (my_project and my_new_project) each with a different Rails version and you can install all other Gems required for your project.

If you need to remove a Gemset, you must be using it (rvm RUBY_VERSION@GEMSET_NAME) and do this:

rvm gemset delete

You can rename your Gemset:

rvm gemset rename current_name new_name

To make my life easier I have created some aliases on my .bash_profile so I don't have to type rvm RUBY_VERSION@GEMSET_NAME.



That's it, if you know a way I can improve it, please let me know it.

I wish you the best.

Today, be calm when you would be nervous

Sunday, September 8, 2013

Making spree 2.0.3 show your gateway error messages

Hi there, happy?

I have developed an active merchant gateway, called 2Pay and I configured my Spree to use it.

It was working great until I got warned that when something goes wrong with the client's credit card, spree is always showing a default error message.

Well, that is terrible because the client will have no idea of what is going on among all possibilities.

PAUSE: I have this on StackOverflow if you feel more comfortable there :)

I had to do one thing, check the spree_core source code using

bundle open spree_core
So I noticed how the checkout_controller was calling a default message when something goes wrong with the Gateway object so I had to update it but it didn't work 100%.

Searching a bit more I found that the order.rb model rescue block wasn't adding the error to the errors hash.

Anyway, these are the two files you need to override and to do that you need to create checkout_controller.rb as checkout_controller_decorator.rb on /controller and the order.rb as order_decorator.rb on /model.

I know the paths are obvious but some user on #spree channel told me to save them on different folder and that was making me more confused.



I really hope it helps you my friend.

Now, be a nice person and say something nice to who's with you :)

Tuesday, June 11, 2013

Kaprekar number

Hi there reader, happy?

Today I was introduced to the Kaprekar number and decided to check if a given number is a kaprekar one.

Here is what I got:




To make it run simply:
ruby kaprekar.rb n

Now, be a good person a hug the person next to you =)

Wednesday, April 24, 2013

Linux user not in the sudoers list anymore or forgot your password?

Crap, that happened to me too =/

I've installed VirtualBox and to use my USB devices I had to add my user to the virtualbox group so I did this:

usermod -G virtualbox gerep

My mistake? I forgot the -a or --append option that according to the manual:

Add the user to the supplementary group(s). Use only with the -G option.

The solution is simple, you need to boot on recovery mode (hold down the Shift key during bootup).

In the list that will show, choose the option revory mode.

In the next list, choose root - Drop to root shell prompt. You need to use your root password and doing this will open command line.

The filesystem is mounted as read-only, so you need to enter the following command to get it to remount as read-write, so the changes will be allowed:

mount -o rw, remount

Now I can change my user group with the same command usermod setting my user back to the sudo group but now with the -a option:

usermod -a -G sudo gerep
Now you can exit the command line, and select the option resume - Resume normal boot.

Note:
In case you forgot your user password do this and you'll be able to set a new password:

passwrd your_username

Monday, March 4, 2013

Removing untracked files from Git working copy

Today I had to remove some untracked files from my git working copy and it turns out to be really simple.

Days ago I was deleting those files by hand, what is really stupid unless you want to keep some of them (not my case).

The solution is: clean

git clean -d -f

The -f option will force git to clean the untracked files.
The -d option will remove the directories too.