You are currently browsing the tag archive for the 'Ruby' tag.

Again I read an article about background processing that generates many different jobs which all do the same: call method a on b with parameter z. At the moment we are using one GenericJob to handle all those cases.

It serializes ActiveRecord objects to a string representation, so that they do not get submittet as instance(often too large or deserialisation problems), if you do not need this feature, :send_later could be a good option for starting with generic jobs.

Usage

GenericJob.publish UserMailer, :deliver_notification,
  :args=>[user, comment], :priority=>2

Install

class GenericJob < DelayedJobBase
  DEFAULT_JOB_PRIORITY = 5

  # GenericJob.publish( Product, :find, :args=>[:all, {:conditions=>"1 = 2"}], :priority=>3 )
  def self.publish(klass, method, options={})
    args = options[:args] || []
    args = GenericJob.serialize_ar(args)
    priority = options[:priority] || DEFAULT_JOB_PRIORITY
    Delayed::Job.enqueue self.new(:class_name => klass.to_s, :method_name => method, :args => args), priority
  end

  def perform
    klass = message[:class_name].constantize
    args = GenericJob.deserialize_ar(message[:args]||[])
    klass.send(message[:method_name], *args)
  end

  private

  def self.serialize_ar(args)
    args.map do |arg|
      if arg.is_a?(ActiveRecord::Base)
        "ActiveRecord:#{arg.class}:#{arg.id}"
      else
        arg
      end
    end
  end

  def self.deserialize_ar(args)
    args.map do |arg|
      if arg.to_s =~ /^ActiveRecord:(\w+):(\d+)$/
        $1.constantize.find($2)
      else
        arg
      end
    end
  end
end

Since my old Ubuntu lived a long live and saw numerous hacks, I chose to reinstall from scratch, here are the steps I took:

  1. install dotfiles
  2. Skype
  3. Multi-clipboard: sudo apt-get install glipper
  4. Application laucher: sudo apt-get install gnome-do + enable skype plugin
  5. Ruby enterprise
  6. Rubymine + Desktop icon+ Meta key for Rubymine
  7. Mysql: sudo apt-get install mysql-server mysql-client libmysql-ruby
  8. Apache:  sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev
  9. Passenger
  10. SSL for apache/passenger
  11. gem sources -a http://gems.github.com
  12. gems…
  13. Java: sudo apt-get install sun-java6-jre
  14. Git: sudo apt-get install git-core

No more hacks, everything works, very fast startup (~10s), faster graphics (for intel chips) :D

My new desktop

My new desktop

Making a certificate

sudo mkdir /etc/apache2/ssl
sudo /usr/sbin/make-ssl-cert /usr/share/ssl-cert/ssleay.cnf /etc/apache2/ssl/apache.pem

sudo nano /etc/apache2/sites-available/default-ssl
  #deactivete other crt/key files (snakeoil)
  SSLCertificateFile /etc/apache2/ssl/apache.pem

sudo a2ensite default-ssl
sudo a2enmod ssl
sudo /etc/init.d/apache2 restart

Configure passenger

<VirtualHost *:80>
 ServerAlias *.something.com
 RailsEnv development
 DocumentRoot /apps/something/public
</VirtualHost>
<VirtualHost *:443>
 SSLEngine on
 SSLCertificateFile /etc/apache2/ssl/apache.pem
 ServerAlias *.something.com
 RailsEnv development
 DocumentRoot /apps/something/public
</VirtualHost>

host

# descriptive raise
# normale: raise 1 == TypeError: exception class/object expected
# now: raise 1 == RuntimeError: 1
class Object
  def raise_with_helpfulness(*args)
    raise_without_helpfulness(*args)
  rescue TypeError => e
    raise_without_helpfulness args.first.inspect if e.to_s == 'exception class/object expected'
    raise e
  end
  alias_method_chain :raise, :helpfulness

I recently did the switch and it was fun, and now I finally got everything working, here are the steps I took:

  • Install rubymine and make a shortcut
  • Use Textmate style shortcuts (so I can work with all the evil textmaters out there :> )
  • Correct Meta key behaviour:
    1. change LWIN in /usr/share/X11/xkb/symbols/pc
    key <LWIN> {        [ Meta_L, Super_L       ]       };
    2. System > administration > keyboard > layout options
    choose meta is mapped to left win
  • fuzzy autocomplete / Textmate Esc autocomplete is called “cyclic completion”, find it in keymap and replace it with Esc
  • add ctrl+c/v/x/z/… shortcuts so rubymine behaves like all the other apps

Benefits:

  • great fuzzy file search e.g. “ba/us/in” -> app/backend/users/index.erb
  • Fast autocompletion
  • Nice refactoring with preview
  • Warnings / hints on local / global / unused variables and sane spellchecking
  • Tons of options for whitespace/editor layout
  • Integrated git with visible history
  • “click something and got to its definition” works great

Drawbacks

  • Startup takes ~30s for large app (initial indexing took ~ 4min)
  • To many menu entries, but they can be customized out…