def run_update(user, tries=1):
    if tries == 0:
        pmig = phabdb.phdb(db=config.rtmigrate_db,
                       user=config.rtmigrate_user,
                       passwd=config.rtmigrate_passwd)
        current = phabdb.get_user_migration_history(user['user'],
                                                    pmig)
        if current:
           failed = ipriority['update_failed_comments']
           log(phabdb.set_user_relations_priority(failed,
                                                  user['user'],
                                                  pmig))
        else:
            log('%s user does not exist to update' % (user['user']))
        pmig.close()
        elog('final fail to update %s' % (user['user'],))
        return False
    try:
        return update(user)
    except Exception as e:
        import traceback
        tries -= 1
        time.sleep(5)
        traceback.print_exc(file=sys.stdout)
        elog('failed to update %s' % (user,))
        return run_update(user, tries=tries)
def update(user):

    phab = Phabricator(config.phab_user,
                       config.phab_cert,
                       config.phab_host)

    pmig = phabdb.phdb(host=config.dbhost,
                       db=config.bzmigrate_db,
                       user=config.bzmigrate_user,
                       passwd=config.bzmigrate_passwd)

    phabm = phabmacros('', '', '')
    phabm.con = phab

    if phabdb.is_bot(user['userphid']):
        log("%s is a bot no action" % (user['user']))
        return True

    epriority = phabdb.get_user_relations_priority(user['user'], pmig)
    if epriority and len(epriority[0]) > 0:
        if epriority[0][0] == ipriority['update_success']:
            log('Skipping %s as already updated' % (user['user']))
            return True

    # 'author': [409, 410, 411, 404, 412],
    # 'cc': [221, 69, 203, 268, 261, 8],
    # 'created': 1410276037L,
    # 'modified': 1410276060L,
    # 'assigned': [97, 64, 150, 59, 6],
    # 'userphid': 'PHID-USER-4hsexplytovmqmcb7tq2',
    # 'user': u'*****@*****.**'}

    if user['assigned']:
        for ag in user['assigned']:
             vlog(phabm.sync_assigned(user['userphid'], ag, bzlib.prepend))

    if user['author']:
        for a in user['author']:
            vlog(phabm.synced_authored(user['userphid'], a, bzlib.prepend))

    if user['cc']:
        for ccd in user['cc']:
            vlog(phabdb.add_task_cc_by_ref(user['userphid'], ccd, bzlib.prepend))

    current = phabdb.get_user_migration_history(user['user'], pmig)
    if current:
        log(phabdb.set_user_relations_priority(ipriority['update_success'], user['user'], pmig))
    else:
        elog('%s user does not exist to update' % (user['user']))
        return False
    pmig.close()
    return True
def run_update(user, tries=1):
    if tries == 0:
        pmig = phabdb.phdb(db=config.fabmigrate_db,
                       user=config.fabmigrate_user,
                       passwd=config.fabmigrate_passwd)
        current = phabdb.get_user_migration_history(user['user'], pmig)
        if current:
           log(phabdb.set_user_relations_priority(ipriority['update_failed'], user['user'], pmig))
        else:
            log('%s user does not exist to update' % (user['user']))
        pmig.close()
        log('final fail to update %s' % (user['user'],))
        return False
    try:
        if update(user):
            log('%s done with %s' % (str(int(time.time())), user,))
            return True
    except Exception as e:
        import traceback
        tries -= 1
        time.sleep(5)
        traceback.print_exc(file=sys.stdout)
        log('failed to update %s (%s)' % (user, e))
        return run_update(user, tries=tries)