# -*- coding: utf-8 -*-
import sys
from datetime import datetime
import logging

from modularodm import Q

from website.archiver import (
    ARCHIVER_UNCAUGHT_ERROR,
    ARCHIVER_FAILURE,
    ARCHIVER_INITIATED
)
from website.settings import ARCHIVE_TIMEOUT_TIMEDELTA
from website.archiver.utils import handle_archive_fail
from website.archiver.model import ArchiveJob

from website.app import init_app

from scripts import utils as script_utils


logger = logging.getLogger(__name__)

def find_failed_registrations():
    expired_if_before = datetime.utcnow() - ARCHIVE_TIMEOUT_TIMEDELTA
    jobs = ArchiveJob.find(
        Q('sent', 'eq', False) &
        Q('datetime_initiated', 'lt', expired_if_before) &
        Q('status', 'eq', ARCHIVER_INITIATED)
    )
    return {node.root for node in [job.dst_node for job in jobs] if node}

def remove_failed_registrations(dry_run=True):
    init_app(set_backends=True, routes=False)
    count = 0
    failed = find_failed_registrations()
    if not dry_run:
        for f in failed:
            logging.info('Cleaning {}'.format(f))
            if not f.registered_from:
                logging.info('Node {0} had registered_from == None'.format(f._id))
                continue
            if not f.archive_job:  # Be extra sure not to delete legacy registrations
                continue
            f.archive_job.status = ARCHIVER_FAILURE
            f.archive_job.sent = True
            f.archive_job.save()
            handle_archive_fail(
                ARCHIVER_UNCAUGHT_ERROR,
                f.registered_from,
                f,
                f.creator,
                f.archive_job.target_info()
            )
            count += 1
    logging.info('Cleaned {} registrations'.format(count))

def main():
    dry = 'dry' in sys.argv
    if not dry:
        script_utils.add_file_logger(logger, __file__)
    remove_failed_registrations(dry_run=dry)

if __name__ == '__main__':
    main()

@@ -207,16 +207,66 @@
 IATED%0A)%0A
+%0Afrom website import (%0A    mails,%0A    settings%0A)%0A%0A
 from web
@@ -530,16 +530,17 @@
 ame__)%0A%0A
+%0A
 def find
@@ -885,18 +885,30 @@
 e%7D%0A%0A
+%0A
 def 
-remove
+notify_desk_about
 _fai
@@ -1346,12 +1346,15 @@
 o de
-lete
+al with
  leg
@@ -1388,32 +1388,33 @@
        continue%0A
+%0A
             f.ar
@@ -1413,151 +1413,121 @@
     
-f.archive_job.status = ARCHIVER_FAILURE%0A            f.archive_job.sent = True%0A            f.archive_job.save()%0A            handle_archive_fail(
+# Send an email to the OSF Help Desk%0A            mails.send_mail(%0A                to_addr=settings.SUPPORT_EMAIL,
 %0A   
@@ -1535,32 +1535,42 @@
             
+mail=mails.
 ARCHIVE
-R
 _UNCAUGHT_ER
@@ -1568,24 +1568,29 @@
 CAUGHT_ERROR
+_DESK
 ,%0A          
@@ -1599,44 +1599,22 @@
     
+user=
 f.
+c
 re
-gistered_from,%0A                f
+ator
 ,%0A  
@@ -1627,25 +1627,37 @@
         
+src=
 f.
-c
 re
-ator
+gistered_from
 ,%0A      
@@ -1666,16 +1666,24 @@
         
+results=
 f.archiv
@@ -1701,16 +1701,17 @@
 t_info()
+,
 %0A       
@@ -1713,24 +1713,25 @@
           )%0A
+%0A
             
@@ -1763,14 +1763,12 @@
 fo('
-Cleane
+Foun
 d %7B%7D
@@ -1781,16 +1781,44 @@
 trations
+, notified the OSF Help Desk
 '.format
@@ -1827,16 +1827,17 @@
 ount))%0A%0A
+%0A
 def main
@@ -1947,14 +1947,25 @@
     
-remove
+notify_desk_about
 _fai

Remove DB writes from this script, only email