Skip to content

Instantly share code, notes, and snippets.

@ggoodale
Created April 14, 2010 23:06
Show Gist options
  • Save ggoodale/366453 to your computer and use it in GitHub Desktop.
Save ggoodale/366453 to your computer and use it in GitHub Desktop.
@collection = db.collection('docs')
loop do
# Find a record to work on
new_record = @collection.find_one({:state => "NEW"}, {:sort => "created_at asc"})
# Set the state to my process id, but only if it's still in the "NEW" state
response = @collection.update({:_id => new_record.id, :state => "NEW"},
{'$set' => {:state => Process.pid}},
{:upsert => false, :safe => true})
if response[0][0][:n] == 0 # Don't ask...
# We updated no records, so someone else got to this one while we were trying to lock it. Move on.
puts "Decorator #{@pid}: Failed to lock record #{mention.id}; skipping"
sleep 1
next
end
# do some work, possibly modifying new_record
@collection.update({:_id => new_record.id}, new_record.merge(:state => "DONE"))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment