#!/tmp/agora/.env/bin/python
"""
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  Copyright (C) 2018 Fernando Serena.
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
"""
import logging
import multiprocessing
import os

import gunicorn.app.base
from agora import Agora, setup_logging
from gunicorn.six import iteritems

from agora_graphql.server import build

__author__ = 'Fernando Serena'

REQUEST_TIMEOUT = int(os.environ.get('REQUEST_TIMEOUT', 300))
API_PORT = int(os.environ.get('API_PORT', 5010))
SCHEMA_PATH = os.environ.get('SCHEMA_PATH', None)
LOG_LEVEL = int(os.environ.get('LOG_LEVEL', logging.DEBUG))
GATEWAY_HOST = os.environ.get('GATEWAY_HOST', 'localhost')
GATEWAY_PORT = os.environ.get('GATEWAY_PORT', 8000)
DATA_CACHE_HOST = os.environ.get('DATA_CACHE_HOST', 'localhost')
DATA_CACHE_PORT = int(os.environ.get('DATA_CACHE_PORT', 6379))
DATA_CACHE_ID = os.environ.get('DATA_CACHE_ID', 6)
DATA_CACHE_GRAPH_LIMIT = os.environ.get('DATA_CACHE_GRAPH_LIMIT', 10000)

setup_logging(LOG_LEVEL)


def number_of_workers():
    return (multiprocessing.cpu_count() * 2) + 1


class StandaloneApplication(gunicorn.app.base.BaseApplication):
    def init(self, parser, opts, args):
        pass

    def __init__(self, app, options=None):
        self.options = options or {}
        self.application = app
        super(StandaloneApplication, self).__init__()

    def load_config(self):
        config = dict([(key, value) for key, value in iteritems(self.options)
                       if key in self.cfg.settings and value is not None])
        for key, value in iteritems(config):
            self.cfg.set(key.lower(), value)

    def load(self):
        return self.application


if __name__ == '__main__':
    graphql_config = {
        'gateway': {
            'host': GATEWAY_HOST,
            'port': GATEWAY_PORT,
            'data_cache': {
                'min_cache_time': 300,
                'redis_host': DATA_CACHE_HOST,
                'redis_port': DATA_CACHE_PORT,
                'redis_db': DATA_CACHE_ID,
                'graph_memory_limit': DATA_CACHE_GRAPH_LIMIT
            }
        },
        'schema': {
            'path': SCHEMA_PATH
        },
        'gw_cache': {
            'max_age_seconds': 300,
            'max_len': DATA_CACHE_GRAPH_LIMIT
        }
    }

    try:
        app = build(**graphql_config)
        options = {
            'bind': '%s:%s' % ('0.0.0.0', str(API_PORT)),
            'workers': number_of_workers(),
            'threads': 1,
            'workerconnections': 1000,
            'timeout': 4000,
            'workerclass': 'gthread',
            'errorlog': '-',
            'accesslog': '-'
        }
        StandaloneApplication(app, options).run()
    except (KeyboardInterrupt, SystemExit, SystemError):
        pass
    finally:
        Agora.close()
