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.

Sunday, February 24, 2013

Forgot to add files and made a commit?

It happens to me frequently, I make a commit and just after that I remember that there are some files that I left behind.

The solution is pretty simple: amend

git commit --amend -C HEAD

The detail is that you can use it with -C or -c (I hope you noticed the capital).

The -C uses the last commit message and author stuff and commit again but with your forgotten files.

The -c will let you set a new message.

Easy breezy.

Be happy.

Tuesday, February 19, 2013

Rails errors with Foundation style with simple_form

I just made my error messages use Foundation styles  (http://foundation.zurb.com/docs/forms.php, scroll until Error States)

I edited the simple_form initializer:



Now you have cool error/alert/whatever messages.

Be happy

Friday, February 15, 2013

RSpec or UnitTest

I asked that myself 5 minutes ago and wasted 3 searching and guess what, 50% votes for RSpec and the other half to UnitTest. So, based on this "in depth" research, I came up with the solution.

I doesn't matter at all, choose that one that reads cleaner. It's like asking: "What is the best editor?"

Tuesday, February 12, 2013

Foundation fonts not working

I just made my application work with Foundation Fonts. It is simple but not obvious if you follow the step-by-step.

Acabei de fazer minha aplicação usar as fontes do , é bem simples mas não tão óbvio se você seguir o

They say it is simple like adding a bit of code, of course you need to make some changes.



I won't work.

I made some tests, read some stackoverflow answers and nothing...

Just to make sure the problem wasn't with assets pipeline I printed this in my console:

p Rails.application.config.assets.paths

Guess what. My fonts path was there.

/app/assets/fonts so I was sure there problem was my CSS

That's when I checked the downloaded file from Zurb and the one from the page and notice the difference.



See? There are some classes references to each icon so it's not just adding those i tags and you're done.

In case you need a CSS with all fonts, I have a gist for you

Be happy!

Thursday, January 31, 2013

Github: I've created a small bash script to help with git projects

Hello world!

I have create a small bash script to open the git project page from terminal with some options.

https://github.com/gerep/opengit

And if someday you think "it would be great if...", just tell me and I'll be glad to try that =)

Monday, January 28, 2013

Testing different database connections

Hello world!

I had to test some database connections from inside my application.

The user fill a form with the required information but before saving it to my PostgreSQL instance I had to check if everything is OK.

I was trying to use my model like this:


And guess what, as expected it replaces my application connection.

The solution is pretty simple, use the adapter class like:


The reason for this code be inside a bedin/rescue is that if anything is wrong with the connection data it will return 500 Internal Server Error

Problem solved.

Friday, November 2, 2012

Another way to fix Apache – "Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName"

Hi fellas!

Don't know why but even with the httdp.conf file configured I was still receiving the error: "Could not realiably...", checked the /etc/hosts and everything was OK.

The solution is pretty simple, just rename the http.conf to fqdn and move it to the conf.d folder

Problem solved.

Adios!

Friday, October 26, 2012

How to fix Apache – "Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName"

Hi fellas!

Just made a fresh install of Apache2 on my Ubuntu, made some changes and trying to restart it

$ sudo servie apache2 restart


Returned this:

* Restarting web server apache2 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName


To fix it you need to edit the httpd.conf file.

$ sudo gedit /etc/apache2/httpd.conf


The file will be blank, no problem, this is default. Add this:

ServerName localhost


And now restart Apache

$ sudo service apache2 restart


Problem solved.

Adios!

Monday, October 22, 2012

Ruby - Installing gem eventmachine

Hi fellas!

Trying this:

$ gem install eventmachine


Returned me this:

make
compiling ssl.cpp
make: g++: Command not found
make: *** [ssl.o] Error 127

The solution is installing the build-essential package:

$ sudo apt-get install build-essential


Problem solved!

Friday, October 19, 2012

Ruby - Installing gem debugger

Hi fellas!

Tryin this:


$ gem install debugger


Returned this:


checking for vm_core.h... no
checking for vm_core.h... no
Makefile creation failed
**************************************************************************
No source for ruby-1.9.3-p286 provided with debugger-ruby_core_source gem.
**************************************************************************



Reading the error I tried this:



$ gem install debugger -- --with-ruby-include=/home/$(echo $USER)/.rvm/src/ruby-$(ruby -v | cut -d" " -f2)

Problem solved.

Adios!

Terminal - How to color it like mac?

Hi fellas!

Here you go:

function git_branch_name() {
git branch 2>/dev/null | grep -e '^*' | sed -E 's/^\* (.+)$/(\1) /'
}

function show_colored_git_branch_in_prompt() {
PS1="\[\033[01;32m\]\u@\h:\[\033[01;34m\]\w\[\033[31m\]\$(git_branch_name)\[\033[m\]$ "
}

show_colored_git_branch_in_prompt


Just place it on your .baschrc or something else depending on your distro.

Don't thank me, thank this guy.

Adios!

Wednesday, October 17, 2012

Fedora - Error installing java

Hi fellas!

Yes, I'm gonna start using Fedora instead of Ubuntu, read some good stuff about it and if you are interested, google it =)

My first problem with Fedora, as expected, was with the creepy java.

Made all the RPM installation and set all the alternatives, time to check:

$ java -version

Gave me this:
$ /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory

Oh my God!

The problem is that I'm running a 64-bit system and I don't have the 32-bit lbrary (I've installed java 32-bit)

So just installing this library solves the problem

$ yum install glibc.i686

Adios!

Thursday, October 11, 2012

Rails - Can't convert fixnum to string during rake db:create

Hi fellas!

I'm using MySQL and tried to run rake db:create and this error was returned:

`connect': can't convert Fixnum into String (TypeError)


The problem is that YAML parser is trying to guess what data type value I have in my datase.yml.

My file was like this:

development:
adapter: mysql2
encoding: utf8
reconnect: false
database: walk_benchmark_development
pool: 5
username: root
password: 123
socket: /var/run/mysqld/mysqld.sock


So the parser probably considers my password as a integer and forcing it to interpret my password as a string works.

password: "123"