Are you running JRuby in production? Do you want distributed file storage for your “enterprise” application? Look no further, MogileFS is here.

MogileFS-Client has compatibility issues with JRuby due to it’s use of the low level Socket class. JRuby 1.5-dev does not yet support all the Socket methods, so here is a monkey patch to get the ruby mogilefs client working on JRuby. Yes it blocks, but who cares JRuby has native threads.

This is exactly why I love Ruby; monkey patching.

[code lang=”ruby”]
class Socket
def self.mogilefs_new(host,port,timeout=5.0)
TCPSocket.open(host,port,timeout)
end
end
class TCPSocket
attr_accessor :mogilefs_addr, :mogilefs_connected, :mogilefs_size, :mogilefs_tcp_cork
def self.open(host,port,timeout = 5.0)
super(host,port.to_i)
end
def readable?
true
end
def write_nonblock(data)
write(data)
end
def recv_nonblock(size,arg)
recv(size,arg)
end
def mogilefs_init(host = nil, port = nil)
true
end
end
[/code]

Here is an example test case on how to get it all to work.

[code lang=”ruby”]
require ‘rubygems’
require ‘mogilefs’

# jmogilefs.rb is the monkey patch above
# load it after loading mogilefs client.
require ‘jmogilefs.rb’

mg = MogileFS::MogileFS.new(:domain=>’testserv’,:hosts=>[‘xxx.xxx.xxx.xxx:6001’])

p mg.get_file_data ‘video:100:default.jpg’

p mg.get_paths ‘video:100:default.jpg’,true

mg.list_keys(‘video:100’)[0].each do |f|
p f
end
[/code]