Created
April 14, 2010 23:06
-
-
Save ggoodale/366453 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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