def reprocess_archive_stubs(): # Check for archive stubs from corehq.form_processor.interfaces.dbaccessors import FormAccessors from couchforms.models import UnfinishedArchiveStub stubs = UnfinishedArchiveStub.objects.filter(attempts__lt=3) datadog_gauge('commcare.unfinished_archive_stubs', len(stubs)) start = time.time() cutoff = start + timedelta(minutes=4).total_seconds() for stub in stubs: # Exit this task after 4 minutes so that tasks remain short if time.time() > cutoff: return try: xform = FormAccessors(stub.domain).get_form(form_id=stub.xform_id) # If the history wasn't updated the first time around, run the whole thing again. if not stub.history_updated: FormAccessors.do_archive(xform, stub.archive, stub.user_id, trigger_signals=True) # If the history was updated the first time around, just send the update to kafka else: FormAccessors.publish_archive_action_to_kafka( xform, stub.user_id, stub.archive) except Exception: # Errors should not prevent processing other stubs notify_exception(None, "Error processing UnfinishedArchiveStub")
def unarchive(self, user_id=None, trigger_signals=True): if self.is_archived: FormAccessors.do_archive(self, False, user_id, trigger_signals)
def archive(self, user_id=None, trigger_signals=True): if not self.is_archived: FormAccessors.do_archive(self, True, user_id, trigger_signals)