config = dict(
    # Set this to dev if you're hacking on fedmsg or an app locally.
    # Set to stg or prod if running in the Fedora Infrastructure.
    environment="{{ fedmsg_env }}",

    # Most hosts will be "false" here indicating that if they publish messages,
    # they will passively bind to ports and have other consuming services
    # actively connect to them.  If this flag is set to True, then the service
    # will actively connect out to a fedmsg-relay to have its messages forwarded
    # on.
    active={{fedmsg_active}},

    {% if fedmsg_cert_prefix is defined %}
    # Most fedmsg enabled services dynamically guess their cert_prefix by
    # looking at the namespace of the python code that they're running in.  For
    # instance, bodhi's code is in the 'bodhi' python module, so fedmsg grabs
    # that and uses it for its cert prefix.  Some code, however, runs in an
    # oddly-namespaced module, and so we allow the option here to override that
    # at the host level.
    cert_prefix='{{fedmsg_cert_prefix}}',
    {% endif %}

    {% if not (ansible_hostname.startswith('busgateway') or ansible_hostname.startswith('bodhi-backend')) %}
    # These options provide a place for hub processes to write out their last
    # processed message.  This let's them read it in at startup and figure out
    # what kind of backlog they have to deal with.
    {% if env == 'staging' %}
    # But we have it turned off in staging.
    #status_directory="/var/run/fedmsg/status",
    {% else %}
    status_directory="/var/run/fedmsg/status",
    {% endif %}

    # This is the URL of a datagrepper instance that we can query for backlog.
    {% if env == 'staging' %}
    # But we have it turned off in staging.
    #datagrepper_url="https://apps.stg.fedoraproject.org/datagrepper/raw",
    {% else %}
    datagrepper_url="https://apps.fedoraproject.org/datagrepper/raw",
    {% endif %}
    {% endif %}

    # This used to be set to 1 for safety, but it turns out it was
    # excessive.  It is the number of seconds that fedmsg should sleep
    # after it has initialized, but before it begins to try and send any
    # messages.  If set to a non-zero value, this will slow down one-off
    # fedmsg scripts like the git post-receive hook and pkgdb2branch.
    # If we are experiencing message-loss problems, one of the first things
    # to try should be to turn this number up to a non-zero value.  '1' should
    # be more than sufficient.
    post_init_sleep={{fedmsg_post_init_sleep}},

    # This is the number of milliseconds to wait before timing out on
    # connections.. notably to the fedmsg-relay in the event that it has
    # crashed.
    zmq_linger=2000,

    # Default is 0
    high_water_mark=0,
    io_threads=1,

    # We almost always want the fedmsg-hub to be sending messages with zmq as
    # opposed to amqp or stomp.  The only exception will be the bugzilla
    # amqp<->zmq bridge service.
    zmq_enabled=True,

    # When subscribing to messages, we want to allow splats ('*') so we tell the
    # hub to not be strict when comparing messages topics to subscription
    # topics.
    zmq_strict=False,

    # See the following
    #   - http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
    #   - http://api.zeromq.org/3-2:zmq-setsockopt
    zmq_tcp_keepalive=1,
    zmq_tcp_keepalive_cnt=3,
    zmq_tcp_keepalive_idle=60,
    zmq_tcp_keepalive_intvl=5,

    # See the following
    #   - https://github.com/fedora-infra/fedmsg/issues/305
    #   - http://api.zeromq.org/3-2:zmq-setsockopt
    zmq_reconnect_ivl=100,
    zmq_reconnect_ivl_max=500,
)

# This option adds an IPC socket by which we can monitor hub health.
try:
    import os
    import psutil

    pid = os.getpid()
    proc = [p for p in psutil.process_iter() if p.pid == pid][0]

    # proc.name is a method on modern versions of psutil.
    name = proc.name
    if callable(name):
        name = name()

    config['moksha.monitoring.socket'] = \
        'ipc:///var/run/fedmsg/monitoring-%s.socket' % name
    config['moksha.monitoring.socket.mode'] = '770'
except (OSError, ImportError):
    # We run into issues when trying to import psutil from mod_wsgi on rhel7
    # but this feature is of no concern in that context, so just fail quietly.
    # https://github.com/jmflinuxtx/kerneltest-harness/pull/17#issuecomment-48007837
    pass
