Create atomic_update db utility function

In some cases (notably the mark_entry_failed function) it
is useful to have atomic update functionality on the db. On
mongo this requires special syntax.

So created an atomic_update function for mongo and started
to use it in mark_entry_failed.
This commit is contained in:
Elrond 2012-01-29 20:56:51 +01:00
parent b8e635b22f
commit 82cd968347
3 changed files with 22 additions and 10 deletions

View File

@ -290,3 +290,14 @@ class MigrationManager(object):
self.set_current_migration(migration_number)
if post_callback:
post_callback(migration_number, migration_func)
##########################
# Random utility functions
##########################
def atomic_update(table, query_dict, update_values):
table.collection.update(
query_dict,
{"$set": update_values})

View File

@ -22,4 +22,5 @@ except ImportError:
if use_sql:
from mediagoblin.db.sql.fake import ObjectId, InvalidId, DESCENDING
else:
from mediagoblin.db.mongo.util import ObjectId, InvalidId, DESCENDING
from mediagoblin.db.mongo.util import \
ObjectId, InvalidId, DESCENDING, atomic_update

View File

@ -18,7 +18,7 @@ import logging
from celery.task import Task
from mediagoblin.db.util import ObjectId
from mediagoblin.db.util import ObjectId, atomic_update
from mediagoblin import mg_globals as mgg
from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
@ -108,22 +108,22 @@ def mark_entry_failed(entry_id, exc):
if isinstance(exc, BaseProcessingFail):
# Looks like yes, so record information about that failure and any
# metadata the user might have supplied.
mgg.database['media_entries'].update(
atomic_update(mgg.database.MediaEntry,
{'_id': entry_id},
{'$set': {u'state': u'failed',
u'fail_error': exc.exception_path,
u'fail_metadata': exc.metadata}})
{u'state': u'failed',
u'fail_error': exc.exception_path,
u'fail_metadata': exc.metadata})
else:
_log.warn("No idea what happened here, but it failed: %r", exc)
# Looks like no, so just mark it as failed and don't record a
# failure_error (we'll assume it wasn't handled) and don't record
# metadata (in fact overwrite it if somehow it had previous info
# here)
mgg.database['media_entries'].update(
atomic_update(mgg.database.MediaEntry,
{'_id': entry_id},
{'$set': {u'state': u'failed',
u'fail_error': None,
u'fail_metadata': {}}})
{u'state': u'failed',
u'fail_error': None,
u'fail_metadata': {}})
class BaseProcessingFail(Exception):