def METHOD_NAME(self, masterid, name):
    log.msg(f"doing housekeeping for master {masterid} {name}")
    # common code for deactivating a master
    yield self.master.data.rtypes.worker._masterDeactivated(
        masterid=masterid)
    yield self.master.data.rtypes.builder._masterDeactivated(
        masterid=masterid)
    yield self.master.data.rtypes.scheduler._masterDeactivated(
        masterid=masterid)
    yield self.master.data.rtypes.changesource._masterDeactivated(
        masterid=masterid)
    # for each build running on that instance..
    builds = yield self.master.data.get(('builds',),
                                        filters=[resultspec.Filter('masterid', 'eq',
                                                                   [masterid]),
                                                 resultspec.Filter('complete', 'eq', [False])])
    for build in builds:
        # stop any running steps..
        steps = yield self.master.data.get(
            ('builds', build['buildid'], 'steps'),
            filters=[resultspec.Filter('results', 'eq', [None])])
        for step in steps:
            # finish remaining logs for those steps..
            logs = yield self.master.data.get(
                ('steps', step['stepid'], 'logs'),
                filters=[resultspec.Filter('complete', 'eq',
                                           [False])])
            for _log in logs:
                yield self.master.data.updates.finishLog(
                    logid=_log['logid'])
            yield self.master.data.updates.finishStep(
                stepid=step['stepid'], results=RETRY, hidden=False)
        # then stop the build itself
        yield self.master.data.updates.finishBuild(
            buildid=build['buildid'], results=RETRY)
    # unclaim all of the build requests owned by the deactivated instance
    buildrequests = yield self.master.db.buildrequests.getBuildRequests(
        complete=False, claimed=masterid)
    yield self.master.db.buildrequests.unclaimBuildRequests(
        brids=[br['buildrequestid'] for br in buildrequests])