Skip to content

Instantly share code, notes, and snippets.

Created May 2, 2014 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b5e7c80cd110096c3884 to your computer and use it in GitHub Desktop.
Save anonymous/b5e7c80cd110096c3884 to your computer and use it in GitHub Desktop.
create queue with upsert
def create(self, name, project=None):
# NOTE(flaper87): If the connection fails after it was called
# and we retry to insert the queue, we could end up returning
# `False` because of the `DuplicatedKeyError` although the
# queue was indeed created by this API call.
#
# TODO(kgriffs): Commented out `retries_on_autoreconnect` for
# now due to the above issue, since creating a queue is less
# important to make super HA.
try:
# NOTE(kgriffs): Start counting at 1, and assume the first
# message ever posted will succeed and set t to a UNIX
# "modified at" timestamp.
counter = {'v': 1, 't': 0}
scoped_name = utils.scope_queue_name(name, project)
self._collection.update({'p_q': scoped_name},
{'$set':{'p_q': scoped_name, 'm': {},
'c': counter}}, upsert=True)
except pymongo.errors.DuplicateKeyError:
return False
else:
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment