Fix #658 and #974 - Rollback database on_return of task
authorJessica Tallon <jessica@megworld.co.uk>
Mon, 1 Dec 2014 13:39:35 +0000 (13:39 +0000)
committerJessica Tallon <jessica@megworld.co.uk>
Mon, 1 Dec 2014 13:39:35 +0000 (13:39 +0000)
mediagoblin/config_spec.ini
mediagoblin/processing/task.py

index dade8420046d0f865788cde3566b0c539b6dd463..f769e4eb4546e29e94c30df7ebebbda1db56b2ec 100644 (file)
@@ -113,9 +113,7 @@ user_privilege_scheme = string(default="uploader,commenter,reporter")
 
 # Frequency garbage collection will run (setting to 0 or false to disable)
 # Setting units are minutes.
-## NOTE: This is temporarily disabled, but we want to set it back:
-## garbage_collection = integer(default=60)
-garbage_collection = integer(default=0)
+garbage_collection = integer(default=60)
 
 [jinja2]
 # Jinja2 supports more directives than the minimum required by mediagoblin. 
index 1a21c6d2fc8ffcf2461e70256d24a8487dd97383..0c254767a25593aa4c10d0dd4277fa9da132c146 100644 (file)
@@ -155,5 +155,19 @@ class ProcessMedia(celery.Task):
 
         entry = mgg.database.MediaEntry.query.filter_by(id=entry_id).first()
         json_processing_callback(entry)
+        mgg.database.reset_after_request()
+
+    def after_return(self, *args, **kwargs):
+        """
+        This is called after the task has returned, we should clean up.
+
+        We need to rollback the database to prevent ProgrammingError exceptions
+        from being raised.
+        """
+        # In eager mode we get DetachedInstanceError, we do rollback on_failure
+        # to deal with that case though when in eager mode.
+        if not celery.app.default_app.conf['CELERY_ALWAYS_EAGER']:
+            mgg.database.reset_after_request()
+
 
 tasks.register(ProcessMedia)