Archive > March 2008

Ruby Background Tasks with Starling – Part 2

Dave » 29 March 2008 » In Technology » 23 Comments

EDIT: The latest workling on github has all of the changes described here. There is no need to download my patch anymore. See github.

In my previous post, I briefly described how simple it is to add background processing to your RoR app with Starling and the Workling plugin. Now, let’s discuss the changes I made to the Workling plugin, and then get everything deployed and monitored. As good as the Workling plugin is, it had one limitation that hurt me. At Inquisix, I have 5 workers to handle importing contacts, stats/logging, emails, searching, etc. Most methods are pretty quick, but a large import process could take 5-10 minutes to complete. With the original Workling, that meant all my other background tasks would wait, and I didn’t want that. The easiest solution for me was to modify Workling so each worker polled its queues in a thread. That way, if the contact worker was busy importing 1000 contacts, my emails still got delivered in a reasonable amount of time.

Most of the changes to Workling, are limited to the lib/workling/starling/poller.rb file. Here is a summary of the changes I made:

  • Added threading
  • Updated the way ClassAndMethodRouting builds the routing hash. I needed a routing hash per worker. Note, whatever you do don’t ever try to call routing.build from inside another thread. Really strange things happen that were very difficult to track down. Ruby threads are nice, but I’m used to real threads.
  • Added handling for MemCache exceptions. During development, I went to dinner and left the listener running without Starling. When I got back, my Mac was complaining the disk was full because my log was > 40Gig. Now, I explicitly catch MemCache exceptions and wait 30 seconds before trying again. The log will still grow, but at least not as fast. Besides, I will show you how to make sure Starling stays running.
  • Keep your database connection alive. I found this out after leaving for the night. I thought I had a working system when I left, but nothing worked in the morning. Basically, ActiveRecord will drop your connection if you don’t do anything for a while. Normally, this is not a problem in web apps because the connection is updated every time you access a page, but it doesn’t work that way here. You have to do it yourself. Add this in your loop to be sure:
    
    unless ActiveRecord::Base.connection.active?
    
      unless ActiveRecord::Base.connection.reconnect!
    
        RAILS_DEFAULT_LOGGER.fatal("FAILED - Database not available")
    
        break
    
      end
    
    end
    
    

Here is my workling patch. Now, I need to go back and update the tests, but the threads complicate that. It’s a learning experience…

That’s it for now. Next time, I will walk through the deployment and monitoring process as I have it.

EDIT: The latest workling on github has all of my changes in it. There is no need to download the patch.

Continue reading...

Ruby Background Tasks with Starling

Dave » 25 March 2008 » In Technology » 101 Comments

At Inquisix, we help sales professionals exchange trusted referrals. To do that requires several background tasks, some that could take 10-15 minutes to process. Obviously, I can’t make a client wait for that, so I needed a system that could handle background tasks. At first, I started with backgroundrb, and it worked just fine. Backgroundrb was in production for two months while Inquisix grew. However, there were a few things about backgroundrb that bothered me:

  • It uses a lot of memory. Every worker creates at least one process. Plus, there is a master process to watch everything and deal with communication. It doesn’t take much before you end up with 5-6 processes. I had to upgrade my test server just to deal with the extra memory requirements.
  • It’s not easy to build a queue with control over threads without creating a ton of processes.
  • Too many times, I wanted to do something pretty straight forward, but I had to dig through the backgroundrb code to figure out the backgroundrb way. For example, don’t ever call sleep in a backgroundb thread pool. You need to call next_turn instead.

After a while, I decided to look for a simpler way that would scale better without using so much memory. I decided a more traditional queue system would work better for me. At a former company, I built up an enterprise system based on queues that processes millions of transactions across dozens of servers. Something based on queues would work for me, but I did not want to take on the complexity of JMS, ActiveMQ (or some other queue), ActiveMessaging, etc. As usual with Ruby projects, I looked around on the web. Within a few minutes, I came across Starling and Sparrow. Both a Ruby queue systems using the memcached interface. That means I can use the memcache-client gem that I already use. Starling was developed at Twitter for background processing, so I figure it’s got some testing behind it. Sparrow is newer, but basically the same. However, there isn’t much experience with Sparrow, so I settled on Starling as my queue server.

To install Starling:

sudo gem install starling
sudo gem install memcache-client

Now, I needed a way to use my new queue server for background tasks. Again, a few minutes of looking, and I found Workling. It didn’t have everything I wanted, but it was nice and simple, and it had almost everything I wanted. I use Piston for all my plugins, so here is how to install with that:

piston import http://svn.playtype.net/plugins/workling/ vendor/plugins/workling
svn commit -m "added workling"

Make sure you commit now because we will be making some changes to Workling later. Piston will get confused and toss your changes if you don’t commit first.

Client Code

First, create a worker in app/workers/my_worker.rb

class MyWorker < Workling::Base
  def do_something_big(options = {})
    SomeModel.do_something_big(options[:some_arg])
  end
end

Anything in app/workers that inherits from Workling::Base will get picked up automatically as a worker. Workers are basically listeners on a Starling queue. By default, Workling defines queues based on class and method. There will be a queue for every method in every class that inherits from Workling::Base.

Now, you can call your worker asynchronously anywhere like so:

MyWorker.asynch_do_something_big(:some_arg => 5)

Starling Runner

To use the Workling’s Starling runner, you need to setup your environment like so:

Workling::Remote.dispatcher = Workling::Remote::Runners::StarlingRunner.new

I add this line to all my environment files (development.rb, etc.). Workling is nice in that if you comment out the above line, all the MyWorker.asynch_* calls will become synchronous calls — nice for debugging!

The Starling runner takes care of several things:

  1. Mapping of queue names to worker code. this is done with Workling::ClassAndMethodRouting, but you can change the queue routing pretty easily.
  2. There’s a client daemon that waits for messages and dispatches these to the responsible workers. if you intend to run this on a remote machine, then just check out your rails project there and start up the Starling client.

Now, fire up Starling, your app, and the workling runner, and your are processing background tasks. Don’t forget to edit config/starling.yml first to tell Workling where Starling is running.

sudo starling -d
script/server
script/workling_starling_client start

What I ended up with was much better for what I was doing. This combination processed my background tasks faster and more reliably. It is much easier to add new workers and call them. Finally, it uses a whole lot less memory, so my end user application performs better. Basically, it wins on all fronts for me.

Next time, I will share the changes I made to Workling to support threads and provide the necessary configuration to ensure that everything stays running in production.

Continue reading...

F-117 stealth fighters to make final flight and Agile Development

Dave » 12 March 2008 » In Technology » 4 Comments

F-117 stealth fighters to make final flight no one will know about – Engadget

Skunk Works It’s the final flight of the F-117. It’s not exactly the prettiest aircraft in the world, but it sure broke new ground. At first, I was surprised, but I didn’t realize there we so few of them left. Then it makes sense. Although, being replaced by the F-22 is an expensive proposition.

If you are interested in how engineering works when you have very smart people and not much oversight, check out Skunk Works. It still boggles my mind that they were able to build the SR-71 from scratch in two years. Especially, when you think about the fact that almost nothing on that aircraft had every been done before. They had to invent new tires, new fuel, new oil, engines that could cruise on afterburner, titanium fabrication techniques, and the list goes on. My favorite story involves paying vendors with suitcases full of cash so no one could follow the money — these were very secret projects. Crazy stuff…

I also look back and see the parallels with Agile Software development. The principles are the same. You take a small team of smart people, empower them to solve a very difficult problem, and you can do a lot in a very short period of time. Kelly Johnson and his team had the luxury of super-secret projects that afforded them the freedom to do whatever is necessary to build their product. The same can be said for Agile Development teams. Give the team a goal and let them figure out the best way to reach that goal. With strong leadership, I’ll bet that small team will destroy a large team’s productivity any day of the week and twice on Sunday. Kelly Johnson said it best:

“Be Quick, Be Quiet, And Be On Time”

I might just have to add that to my own “Go big, or stay home.”

Kelly’s rules of management aren’t too bad either:

  1. The Skunk Works manager must be delegated practically complete control of his program in all aspects. He should report to a division president or higher.
  2. Strong but small project offices must be provided both by the military and industry.
  3. The number of people having any connection with the project must be restricted in an almost vicious manner. Use a small number of good people (10% to 25% compared to the so-called normal systems).
  4. A very simple drawing and drawing release system with great flexibility for making changes must be provided.
  5. There must be a minimum number of reports required, but important work must be recorded thoroughly.
  6. There must be a monthly cost review covering not only what has been spent and committed but also projected costs to the conclusion of the program. Don’t have the books ninety days late and don’t surprise the customer with sudden overruns.
  7. The contractor must be delegated and must assume more than normal responsibility to get good vendor bids for subcontract on the project. Commercial bid procedures are very often better than military ones.
  8. The inspection system as currently used by the Skunk Works, which has been approved by both the Air Force and Navy, meets the intent of existing military requirements and should be used on new projects. Push more basic inspection responsibility back to subcontractors and vendors. Don’t duplicate so much inspection.
  9. The contractor must be delegated the authority to test his final product in flight. He can and must test it in the initial stages. If he doesn’t, he rapidly loses his competency to design other vehicles.
  10. The specifications applying to the hardware must be agreed to well in advance of contracting. The Skunk Works practice of having a specification section stating clearly which important military specification items will not knowingly be complied with and reasons therefore is highly recommended.
  11. Funding a program must be timely so that the contractor doesn’t have to keep running to the bank to support government projects.
  12. There must be mutual trust between the military project organization and the contractor with very close cooperation and liaison on a day-to-day basis. This cuts down misunderstanding and correspondence to an absolute minimum.
  13. Access by outsiders to the project and its personnel must be strictly controlled by appropriate security measures.
  14. Because only a few people will be used in engineering and most other areas, ways must be provided to reward good performance by pay not based on the number of personnel supervised.

In a follow-up post, I will map these rules to an Agile Software project.

Continue reading...

Vodafone demos “Otello” visual search

Dave » 04 March 2008 » In Technology » No Comments

CeBIT 2008: Vodafone demos “Otello” visual search

Now this is interesting, and it sure would save a lot of typing. Now, the engineer in me marvels at how you could actually pull this off. The data requirements to do this kind of pattern matching is staggering. I suspect it will be a while before it gets out of any kind of trial.

Continue reading...