def main(relation_name=None):
    """
    This is the main entry point for the reactive framework.  It calls
    :func:`~bus.discover` to find and load all reactive handlers (e.g.,
    :func:`@when <decorators.when>` decorated blocks), and then
    :func:`~bus.dispatch` to trigger handlers until the queue settles out.
    Finally, :meth:`unitdata.kv().flush <charmhelpers.core.unitdata.Storage.flush>`
    is called to persist the flags and other data.

    :param str relation_name: Optional name of the relation which is being handled.
    """
    hook_name = hookenv.hook_name()
    restricted_mode = hook_name in ['meter-status-changed', 'collect-metrics']

    hookenv.log('Reactive main running for hook %s' % hookenv.hook_name(), level=hookenv.INFO)
    if restricted_mode:
        hookenv.log('Restricted mode.', level=hookenv.INFO)

    # work-around for https://bugs.launchpad.net/juju-core/+bug/1503039
    # ensure that external handlers can tell what hook they're running in
    if 'JUJU_HOOK_NAME' not in os.environ:
        os.environ['JUJU_HOOK_NAME'] = hook_name

    try:
        bus.discover()
        if not restricted_mode:  # limit what gets run in restricted mode
            hookenv._run_atstart()
        bus.dispatch(restricted=restricted_mode)
    except Exception:
        tb = traceback.format_exc()
        hookenv.log('Hook error:\n{}'.format(tb), level=hookenv.ERROR)
        raise
    except SystemExit as x:
        if x.code not in (None, 0):
            raise

    if not restricted_mode:  # limit what gets run in restricted mode
        hookenv._run_atexit()
    unitdata._KV.flush()