Rails Plugins

Here are some of my plugins and code snippets for Ruby on Rails.

If you like any of them and wish to share the joy, send me a message. If you have something to comment on or share any bugs spotted, send me a message.

Singleton Unix Worker

The plug-in adds a singleton worker process to your Rails application, which is useful for any background tasks. It does no real job by itself, but makes it possible to have a singleton process which is started when you need it, runs as long as you need and terminates safely.

The distinctive feature is that it works in multi-worker Mongrel configurations by using a global variable to store and share the ID a worker process. Another good news is that it has its own pristine connection to the database and doesn’t interfere with the ActiveModel of the parent process, so you are safe to do whatever you need with the data model.

Please note that you can use it only on Unix (Linux, Mac) systems. There’s no Ruby way (to my best knowledge) to learn if the process by PID is still running and the fork() command is not implemented on Windows.

It’s licensed under the MIT License Terms and is free for free, open-source and proprietary software.

Your worker typically looks like this:

    class MyWorker < SingletonUnixWorker
      # You override this method and do whatever you need to do
      # and safely terminate.
      def self.run
        # Do some real job here
      end
    end

Here’s how it’s called from the controller:

    MyWorker.start_if_necessary

If the worker process is inactive at the moment, it will be started. If it is, nothing will happen, and the code will quickly return.

Installation

    script/plugin install http://svn.noizeramp.com/rails/singleton_unix_worker/