def handle(self, num_pool, username, **options):
        email = options['mail']

        root_dir = settings.FILEPATH
        git_snapshot = gitinfo.get_project_snapshot(
            root_dir,
            submodules=False,
            log_count=1,
        )
        head = git_snapshot['commits'][0]

        if options['check']:
            exit(0 if get_preindex_complete(head) else 1)

        if get_preindex_complete(head) and email:
            mail_admins('Already preindexed', "Skipping this step")
            return
        else:
            clear_preindex_complete()

        commit_info = "\nCommit Info:\nOn Branch %s, SHA: %s" % (
            git_snapshot['current_branch'], head['sha'])

        pre_message = list()
        pre_message.append("Heads up, %s has started preindexing" % username)
        pre_message.append(commit_info)

        if email:
            mail_admins(" HQAdmin preindex_everything started",
                        '\n'.join(pre_message))

        def couch_preindex():
            call_command('sync_prepare_couchdb_multi', str(num_pool), username,
                         **{'no_mail': True})
            print("Couch preindex done")

        def pillow_preindex():
            call_command('ptop_preindex')
            print("ptop_preindex_done")

        jobs = [gevent.spawn(couch_preindex), gevent.spawn(pillow_preindex)]

        gevent.joinall(jobs)

        try:
            for job in jobs:
                job.get()
        except Exception:
            subject = " HQAdmin preindex_everything failed"
            message = get_traceback_string()
        else:
            subject = " HQAdmin preindex_everything may or may not be complete"
            message = ("We heard a rumor that preindex is complete,\n"
                       "but it's on you to check that all tasks are complete.")
            set_preindex_complete(head)

        if email:
            mail_admins(subject, message)
        else:
            print('{}\n\n{}'.format(subject, message))
Exemple #2
0
    def handle(self, **options):
        runs = []
        all_es_indices = get_all_expected_es_indices()
        es = get_es_new()
        indices_needing_reindex = [
            info for info in all_es_indices
            if not es.indices.exists(info.index)
        ]

        if not indices_needing_reindex:
            print('Nothing needs to be reindexed')
            return

        print("Reindexing:\n\t", end=' ')
        print('\n\t'.join(map(str, indices_needing_reindex)))

        preindex_message = """
        Heads up!

        %s is going to start preindexing the following indices:\n
        %s

        This may take a while, so don't deploy until all these have reported finishing.
            """ % (settings.EMAIL_SUBJECT_PREFIX, '\n\t'.join(
            map(str, indices_needing_reindex)))

        mail_admins("Pillow preindexing starting", preindex_message)
        start = datetime.utcnow()
        for index_info in indices_needing_reindex:
            # loop through pillows once before running greenlets
            # to fail hard on misconfigured pillows
            reindex_command = get_reindex_commands(index_info.alias)
            if not reindex_command:
                raise Exception(
                    "Error, pillow [%s] is not configured "
                    "with its own management command reindex command "
                    "- it needs one" % index_info.alias)

        for index_info in indices_needing_reindex:
            print(index_info.alias)
            g = gevent.spawn(do_reindex, index_info.alias, options['reset'])
            runs.append(g)

        if len(indices_needing_reindex) > 0:
            gevent.joinall(runs)
            try:
                for job in runs:
                    job.get()
            except Exception:
                mail_admins("Pillow preindexing failed",
                            get_traceback_string())
                raise
            else:
                mail_admins(
                    "Pillow preindexing completed",
                    "Reindexing %s took %s seconds" %
                    (', '.join(map(str, indices_needing_reindex)),
                     (datetime.utcnow() - start).seconds))

        print("All pillowtop reindexing jobs completed")
def _fail_gracefully_and_tell_admins():
    mail_admins(
        "IMPORTANT: Preindexing case_search failed because the case_search table hasn't been initialized",
        ("***Run ./manage.py migrate first then run ./manage.py ptop_preindex again***\n\n {}"
         .format(get_traceback_string())))

    class FakeReindexer(object):
        """Used so that the ptop_preindex command completes successfully
        """
        def reindex(self):
            pass

    return FakeReindexer()
Exemple #4
0
def _fail_gracefully_and_tell_admins():
    mail_admins("IMPORTANT: Preindexing case_search failed because the case_search table hasn't been initialized",
                ("***Run ./manage.py migrate first then run ./manage.py ptop_preindex again***\n\n {}"
                 .format(get_traceback_string())))

    class FakeReindexer(object):
        """Used so that the ptop_preindex command completes successfully
        """

        def reindex(self):
            pass

    return FakeReindexer()
Exemple #5
0
    def handle(self, app_label, migration_name, **options):
        args = []
        if app_label is not None:
            args.append(app_label)
        if migration_name is not None:
            args.append(migration_name)

        options['verbosity'] = 0

        def migrate_db(db_alias, options=options):
            call_options = copy(options)
            call_options['database'] = db_alias
            call_command(
                'migrate',
                *args,
                **call_options
            )

        dbs_to_migrate = [
            db_alias
            for db_alias in settings.DATABASES.keys()
            if settings.DATABASES[db_alias].get('MIGRATE', True)
        ]
        dbs_to_skip = list(set(settings.DATABASES) - set(dbs_to_migrate))

        print('\nThe following databases will be migrated:\n * {}\n'.format('\n * '.join(dbs_to_migrate)))
        if dbs_to_skip:
            print('\nThe following databases will be skipped:\n * {}\n'.format('\n * '.join(dbs_to_skip)))

        jobs = [
            gevent.spawn(migrate_db, db_alias)
            for db_alias in dbs_to_migrate
        ]

        gevent.joinall(jobs)

        migration_error_occured = False
        for job in jobs:
            try:
                job.get()
            except Exception:
                print('\n======================= Error During Migration =======================')
                print(get_traceback_string())
                migration_error_occured = True

        if migration_error_occured:
            sys.exit(1)
    def handle(self, app_label, migration_name, **options):
        args = []
        if app_label is not None:
            args.append(app_label)
        if migration_name is not None:
            args.append(migration_name)

        options['verbosity'] = 0

        def migrate_db(db_alias, options=options):
            call_options = copy(options)
            call_options['database'] = db_alias
            call_command('migrate', *args, **call_options)

        dbs_to_migrate = [
            db_alias for db_alias in settings.DATABASES.keys()
            if settings.DATABASES[db_alias].get('MIGRATE', True)
        ]
        dbs_to_skip = list(set(settings.DATABASES) - set(dbs_to_migrate))

        print('\nThe following databases will be migrated:\n * {}\n'.format(
            '\n * '.join(dbs_to_migrate)))
        if dbs_to_skip:
            print('\nThe following databases will be skipped:\n * {}\n'.format(
                '\n * '.join(dbs_to_skip)))

        jobs = [
            gevent.spawn(migrate_db, db_alias) for db_alias in dbs_to_migrate
        ]

        gevent.joinall(jobs)

        migration_error_occured = False
        for job in jobs:
            try:
                job.get()
            except Exception:
                print(
                    '\n======================= Error During Migration ======================='
                )
                print(repr(job))
                print(get_traceback_string())
                migration_error_occured = True

        if migration_error_occured:
            sys.exit(1)
Exemple #7
0
    def handle(self, **options):
        runs = []
        all_es_indices = get_all_expected_es_indices()
        es = get_es_new()
        indices_needing_reindex = [info for info in all_es_indices if not es.indices.exists(info.index)]

        if not indices_needing_reindex:
            print('Nothing needs to be reindexed')
            return

        print("Reindexing:\n\t", end=' ')
        print('\n\t'.join(map(six.text_type, indices_needing_reindex)))

        preindex_message = """
        Heads up!

        %s is going to start preindexing the following indices:\n
        %s

        This may take a while, so don't deploy until all these have reported finishing.
            """ % (
                settings.EMAIL_SUBJECT_PREFIX,
                '\n\t'.join(map(six.text_type, indices_needing_reindex))
            )

        mail_admins("Pillow preindexing starting", preindex_message)
        start = datetime.utcnow()
        for index_info in indices_needing_reindex:
            # loop through pillows once before running greenlets
            # to fail hard on misconfigured pillows
            reindex_command = get_reindex_commands(index_info.alias)
            if not reindex_command:
                raise Exception(
                    "Error, pillow [%s] is not configured "
                    "with its own management command reindex command "
                    "- it needs one" % index_info.alias
                )

        for index_info in indices_needing_reindex:
            print(index_info.alias)
            g = gevent.spawn(do_reindex, index_info.alias, options['reset'])
            runs.append(g)

        if len(indices_needing_reindex) > 0:
            gevent.joinall(runs)
            try:
                for job in runs:
                    job.get()
            except Exception:
                mail_admins("Pillow preindexing failed", get_traceback_string())
                raise
            else:
                mail_admins(
                    "Pillow preindexing completed",
                    "Reindexing %s took %s seconds" % (
                        ', '.join(map(six.text_type, indices_needing_reindex)),
                        (datetime.utcnow() - start).seconds
                    )
                )

        print("All pillowtop reindexing jobs completed")
    def handle(self, num_pool, username, **options):
        email = options['mail']

        root_dir = settings.FILEPATH
        git_snapshot = gitinfo.get_project_snapshot(
            root_dir,
            submodules=False,
            log_count=1,
        )
        head = git_snapshot['commits'][0]

        if options['check']:
            exit(0 if get_preindex_complete(head) else 1)

        if get_preindex_complete(head) and email:
            mail_admins('Already preindexed', "Skipping this step")
            return
        else:
            clear_preindex_complete()

        commit_info = "\nCommit Info:\nOn Branch %s, SHA: %s" % (
            git_snapshot['current_branch'], head['sha'])

        pre_message = list()
        pre_message.append("Heads up, %s has started preindexing" % username)
        pre_message.append(commit_info)

        if email:
            mail_admins(
                " HQAdmin preindex_everything started", '\n'.join(pre_message)
            )

        def couch_preindex():
            call_command('sync_prepare_couchdb_multi', str(num_pool), username,
                         **{'no_mail': True})
            print("Couch preindex done")

        def pillow_preindex():
            call_command('ptop_preindex')
            print("ptop_preindex_done")

        jobs = [gevent.spawn(couch_preindex), gevent.spawn(pillow_preindex)]

        gevent.joinall(jobs)

        try:
            for job in jobs:
                job.get()
        except Exception:
            subject = " HQAdmin preindex_everything failed"
            message = get_traceback_string()
        else:
            subject = " HQAdmin preindex_everything may or may not be complete"
            message = (
                "We heard a rumor that preindex is complete,\n"
                "but it's on you to check that all tasks are complete."
            )
            set_preindex_complete(head)

        if email:
            mail_admins(subject, message)
        else:
            print('{}\n\n{}'.format(subject, message))