Monit and Phusion Passenger

April 30th, 2011 No Comments »

While there’re multiple tools available for monitoring, ranging from Dan Bernshtein’s daemontools to God, I found that Monit is a pretty balanced solution. On one hand, it’s quite powerful, and on the other – it’s not resource hungry. The only drawback that I found, is that for monitoring a process, the process must have a pid-file saved somewhere. The Monit FAQ states that the programs which don’t have pid-file support should be run with some wrapper which will create it on their behalf.

I wanted to use Monit to monitor Rails’ instances, so if they grow too fat, Monit will take care of that. The problem is that the pid file that the Passenger creates a) doesn’t have a predictable location (there’s something called “generation” or something like that), and b) doesn’t have children, as those processes are detached.

Of course there exists a possibility of patching Passenger for providing such pid files. But, luckily, Passenger provides extension points in form of callbacks which are fired when the application instance is created and when it’s taken down.

It was trivial to use this API to provide pid-file managing. The result of this work was the “passenger_monit” plugin.

So, go ahead, add ‘gem “passenger_monit”‘ to your Gemfile and give it a try!

The source is available from:
https://github.com/romanbsd/passenger_monit

April 5th, 2011 No Comments »

Logging line numbers in Ruby Logger can be done with a simple decorator:

  class LoggerDecorator
    def initialize(logger)
      @logger = logger
    end
 
    %w{debug info warn error fatal}.each do |method|
      eval(<<-eomethod)
        def #{method}(msg)
          @logger.#{method}(position) {msg}
        end
      eomethod
    end
 
    private
    def position
      caller.at(1).sub(%r{.*/},'').sub(%r{:in\s.*},'')
    end
  end

MongoDB DLR support for Kannel

April 3rd, 2011 No Comments »

I wrote some code to make use of MongoDB as a DLR storage for Kannel SMS gateway.

The code is available on github:
https://github.com/romanbsd/kannel-mongodb

ExtJS with Rails 3

March 6th, 2011 3 Comments »

In order to make ExtJS play nicely with Rails, the following tweaks are needed:

On the ExtJS javascript side, the following things are needed:

1. Ask server to server JSON:

Ext.Ajax.defaultHeaders = {'Accept': 'application/json'};

2. RailsJsonStore:

Ext.data.RailsJsonStore = Ext.extend(Ext.data.JsonStore, {
    constructor: function(config) {
        Ext.data.RailsJsonStore.superclass.constructor.call(this, Ext.applyIf(config, {
            messageProperty: 'message', //for store.reader.getMessage()
            restful: true,
            url: '/'+ config['root'] + 's',
            writer: {encode: false}
        }));
    }
});
Ext.reg('railsjsonstore', Ext.data.RailsJsonStore);

3. XSRF protection:

Ext.Ajax.on('beforerequest', function(o) {
        var csrf = Ext.select("meta[name='csrf-token']").first();
        if (csrf) {
                o.defaultHeaders = Ext.apply(o.defaultHeaders || {}, {'X-CSRF-Token': csrf.getAttribute('content')});
        }
});

On Rails side:

config.active_record.include_root_in_json = false

Livejournal ruby gem

November 23rd, 2010 No Comments »

I forked the livejournal ruby gem (http://neugierig.org/software/livejournal/ruby/) which seems to be abandoned.
It fixes the well known errors like:

livejournal/entry.rb:174:in `load_prop': unknown prop

which are due to new properties.
It also has some new functionality.
The fork can be found here.
I tried to contact the author, but haven’t heard from him yet.

Objective-C library for MusiXmatch API

September 28th, 2010 No Comments »

There’s a new cool startup called MusiXmatch which provides an easy access to lyrics.
They already have tons of lyrics and they recently opened their API to the alpha testers.
I wrote a basic library for accessing this service. While it’s incomplete, it already provides the most often used functionality such as searching for tracks and getting lyrics.
Example usage:

// Get a track
Track *track = [Track trackWithId:123];
NSString *lyrics = [track lyrics];
 
// Or use the service directly
MusiXmatchService *service = [MusiXmatchService sharedInstance];
NSArray *tracks = [service trackSearch:@"dreamer" numResults:10];
for (Track *track in tracks) {
    NSLog(@"TrackID: %u, Artist: %@, Title: %@", track.mxmId, track.artist.name, track.name);
}

Get it here:

http://github.com/romanbsd/musiXmatch

Cross compiling autoconf packages for iPhone

June 17th, 2010 No Comments »

While the MacOS X iPhone Developer package contains the needed cross-compiling tools, I haven’t found anywhere a clear explanation regarding how the environment should be set for compiling some standard autoconf/automake package.
I’m using the following script (works on the XCode 3.2.2 on Snow Leopard):

#!/bin/sh
export CPP="/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-gcc-4.2.1 -E"
export  CC="/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-gcc-4.2.1"
export CXX="/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-g++-4.2.1"
 
export CPPFLAGS='-I/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.sdk/usr/lib/gcc/arm-apple-darwin10/4.2.1/include -I/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.sdk/usr/include'
export LDFLAGS="-L/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.sdk/usr/lib"
export CFLAGS="-Os -pipe $CPPFLAGS $LDFLAGS"
export CXXFLAGS="$CFLAGS"
 
export PATH="/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/bin:/usr/bin"
 
export ARCH=arm-apple-darwin10
 
./configure --build=i386-apple-darwin10 --host=$ARCH --enable-shared=no --prefix=/usr $@

And then either use the resulting library, or add the source files alongside the generated config.h

Using curb with open-uri

February 18th, 2009 12 Comments »

Simple, but nice replacement for open-uri library (which uses rather slow Net::HTTP library) which uses libcurl (via curb).
It’s easy to swap the original one just by replacing ‘require open-uri’ with ‘require curb-openuri’. Should work as the original, even with better defaults (which, of course, can be changed).

http://github.com/romanbsd/curb-openuri/tree

Non-negative Matrix Factorization for Ruby

February 9th, 2009 7 Comments »

Several months back I needed to compute NMF of some relatively larges matrices.
Since the native Ruby code was painfully slow, and for some reason even failed to work for some matrices, I decided to write a C implementation which will leverage the GNU Scientific Library (GSL) and then wrap it for using in Ruby application.
It was a neat add-on to the rb-gsl ruby library. What it does is adding NMF module under the GSL::Matrix, and there you have a method nmf which receives a GSL::Matrix and number of columns as a parameter and then returns two matrices.
Since this is an iterative algorithm, the number of runs is limited to 1000, and the desired difference cost metric is set to 10-6.
I tried to contact the author and even posted my code in the issue tracker, but haven’t received any response at the time of writing.
So I decided to create a git-svn mirror on Github and add my changes there.
http://github.com/romanbsd/rb-gsl

You can install the gem using this command:

  $ gem sources -a http://gems.github.com/ # (you only have to do this once)
  $ gem install romanbsd-gsl

I18n support for validates_date_time

February 8th, 2009 2 Comments »

This simple fix uses the fact, that if the second argument to the ‘add’ method is a symbol, then a generate_message is called, which does all the voodoo of Rails 2.2 I18n.
I tried to contact the original author to no avail, therefore I’ll post it here, hoping that if anyone needs it, it’ll surface on a Google search.
The fix itself is quite simple:

Index: lib/validates_date_time.rb
===================================================================
--- lib/validates_date_time.rb  (revision 403)
+++ lib/validates_date_time.rb  (working copy)
@@ -114,7 +114,7 @@
       if options[:before]
         options[:before].each do |r|
           if r.value(record) and value >= r.last_value
-            record.errors.add(attr_name, options[:before_message] % r)
+            record.errors.add(attr_name, :before, :default => options[:before_message] % r)
             break
           end
         end
@@ -123,7 +123,7 @@
       if options[:after]
         options[:after].each do |r|
           if r.value(record) and value <= r.last_value
-            record.errors.add(attr_name, options[:after_message] % r)
+            record.errors.add(attr_name, :after, :default => options[:after_message] % r)
             break
           end
         end

I’ve a fork of the git-svn mirror here:
http://github.com/romanbsd/validates_date_time/tree/master

Then, for using it, you’ll just need to have the following path in your locale yml files (for example):

  activerecord:
    errors:
      models:
        user:
          attributes:
            birth_date:
              before: 'is wrong'
              after: 'is wrong'

Pages

Adsense