Archive for the ‘Code’ Category

to_json and associations

The problem was that I wanted to get to a address that was associated with the store. I was using .to_json but this did not include the address information.

First I had var store = @store.to_json but that only gave me the address_id not the address information. After a little digging I got the syntax correct. Here is my code syntax…

This is my head tag:

<script type="text/javascript">var stores=<%=@stores.to_json(:include => {:store_address => {}})%>;</script>

Than I can use the stores variable in my google maps javascript like so:

var storePoint = new GLatLng(stores[i].store.lat, stores[i].store.lng);
var storeText = '<div id="gmap-content">'+
  '<h4>' + stores[i].store.name + '</h4>'+
  '<em>' + stores[i].store.store_address.address + '</em><br />' +
  '<em>' + stores[i].store.store_address.city + '</em><br />' +
  '<em>' + stores[i].store.store_address.phone + '</em>' +
  '</div>';
var storeMarker = new createMarker(storePoint,storeText)

PS. These are just snips and need more code to work..

Advertisements

Ruby’n Rails AASM or state machine

I wanted to use a state machine plugin for my Order model and found that ttilley-aasm was a good fit for me.

Here is how I installed it:

RAILS_ROOT/config/environmet.rb
config.gem 'ttilley-aasm', :source => 'http://gems.github.com', :lib => 'aasm'
# rake gems:install
# ./script/generate migration add_aasm_state_to_Order aasm_state:string
# rake db:migrate

And now how I test that it worked:

>> require 'aasm'
=> true
>> class Order
>> include AASM
>> aasm_initial_state :new
>> aasm_state :new
>> aasm_state :shipped
>> aasm_event :ship do
?> transitions :to => :shipped, :from => [:new]
>> end
>> end
=> #
>> Order.new.ship!
=> true

That worked now my Order class…

class Order < ActiveRecord::Base

include AASM
aasm_initial_state :in_progress

aasm_state :in_progress, :exit => :generate_order_number
aasm_state :shipped

aasm_event :ship do

transitions :to => :shipped, :from => [:in_progress], :if => :paid?

end

def generate_order_number; …; end
def paid?; …; end

end

That looks great!

Using JSVC with Tomcat on a 64-bits machine with a 32-bits JVM.

The Problem

After a good amount of research and issues that  looked like memory leaks I’m moving my jvms to 32-bits JVMs for the servers that do not need a heap larger than 2GB. This was ok and a fast switch of the /usr/local/java symlink, BUT it was not as easy with JSVC that I use to run Tomcat under the user tomcat. The problem is that when I compile jsvc on a 64-bits machine it will auto-magically find the box as x86_64 and then expect that your jvm is also 64 bits.

The Fix

What I found that needs to be done is to force the compiler to use 32bit flags, so first lets run the configure string

[root ~]# cd /usr/local/tomcat/bin && tar -zxf jsvc.tar.gz
[root ~]# cd jsvc-src && chmod +x configure
[root ~]# CFLAGS=-m32 CPPFLAGS=-m32 CCASFLAGS=-m32 LDFLAGS="-L/usr/lib -L/lib" ./configure --build=i686-pc-linux-gnu

now the next step is to run make

[root ~]# make

What you will now see is that the last step of the make will fail horribly with the following error:

gcc -L/usr/lib -L/lib -ldl -lpthread jsvc-unix.o libservice.a -o ../jsvc
/usr/bin/ld: skipping incompatible /usr/lib/libdl.so when searching for -ldl
/usr/bin/ld: skipping incompatible /usr/lib/libdl.a when searching for -ldl
/usr/bin/ld: skipping incompatible /usr/lib/libpthread.so when searching for -lpthread
/usr/bin/ld: skipping incompatible /usr/lib/libpthread.a when searching for -lpthread
/usr/bin/ld: skipping incompatible /usr/lib/libc.so when searching for -lc
/usr/bin/ld: skipping incompatible /usr/lib/libc.a when searching for -lc
/usr/bin/ld: warning: i386 architecture of input file `jsvc-unix.o' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386 architecture of input file `libservice.a(arguments.o)' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386 architecture of input file `libservice.a(debug.o)' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386 architecture of input file `libservice.a(help.o)' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386 architecture of input file `libservice.a(home.o)' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386 architecture of input file `libservice.a(java.o)' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386 architecture of input file `libservice.a(location.o)' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386 architecture of input file `libservice.a(replace.o)' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386 architecture of input file `libservice.a(dso-dlfcn.o)' is incompatible with i386:x86-64 output
make[1]: Leaving directory `/usr/local/tomcat/bin/jsvc-src/native'

Looks like the last gcc left out the -m32 and therefor fails.. Lets do the last task manually

[root ~]# cd native && gcc -m32 -L/usr/local/java/lib -L/usr/lib -L/lib -ldl -lpthread jsvc-unix.o libservice.a -o ../jsvc && cd ..

now if you do the following you should have a correctly compiled 32-bits jsvc executable that points to your 32 bits jvm

[root ~]# file jsvc
jsvc: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped

Nice!

Resources

http://www.mail-archive.com/users@tomcat.apache.org/msg36176.html

Journey with Thinking Sphinx and Crond

This is my journey through time 🙂

Environment:
Ruby => 1.8.7-72
Rails => 2.3.2
Thinking-sphinx => 1.1.3

I had a server running thinking-sphinx through crontab but noticed that this was not working so I started doing some debugging to see what was causing the non running thinking-sphinx:index task…

First I Changed crontab to have full path like so: (crontab -e)

*/10 * * * * cd /rails/current && RAILS_ENV=production /usr/local/bin/rake thinking_sphinx:index >> /dev/null 2>&1

This did nothing… Then I changed the output to go to a file to see if this would give me something

*/10 * * * * cd /rails/current && RAILS_ENV=production /usr/local/bin/rake thinking_sphinx:index >> /tmp/sphinx.output 2>&1

This gave me a timestamp ??? Running that exact command from the shell gives me a full reindex without any issues

Then I figured maybe I needed a trace so I moved the cron task to /etc/cron.d/thinking-sphinx.ct and put this in the file

*/10 * * * * cd /rails/current && RAILS_ENV=production /usr/local/bin/rake thinking_sphinx:index –trace >> /tmp/sphinx.output 2>&1

AND THERE IT WAS !!

!!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql.

Damn I should have know. So its a PATH issue when running rake under cron… Here is the latest /etc/cron.d/thinking-sphinx.ct I have that is now WORKING!!

# ————- minute (0 – 59)
# | ———– hour (0 – 23)
# | | ——— day of month (1 – 31)
# | | | ——- month (1 – 12)
# | | | | —– day of week (0 – 6) (Sunday=0)
# | | | | |
# * * * * * command to be executed

PATH=/usr/local/mysql/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
SHELL=/bin/bash
RAILS_ENV=”production”
LD_LIBRARY_PATH=/usr/local/mysql/lib:${LD_LIBRARY_PATH}

# re-index production sphinx every 15 minutes
*/10 * * * * root cd /rails/current && /usr/local/bin/rake thinking_sphinx:index >> /dev/null 2>&1

There are two important settings there setting the PATH with ruby/rakes install path and MySQLs install path. But also the LD_LIBRARY_PATH so that rails can find the MySQL library. I have that set in /etc/profile.d/mysql.sh but cron does not load the profile.