Ejemplo n.º 1
0
async def main(aio_loop):
    conf = pkg.constants.CONFIG

    def get_dsn(secure=False):
        user = conf['rabbit']['user']
        pswd = '*****' if secure else conf['rabbit']['pass']
        host = conf['rabbit']['host']
        port = conf['rabbit']['port']
        return f'amqp://{user}:{pswd}@{host}:{port}/'

    logger.info(f'Connecting to {get_dsn(secure=True)}')
    i, dsn = 0, get_dsn()
    max_attempts = conf['rabbit'][
        'max_conn_attempts'] if 'max_conn_attempts' in conf['rabbit'] else 20
    while i < max_attempts:
        i += 1
        try:
            connection = await connect(dsn, loop=aio_loop)
            channel = await connection.channel()
            rpc = await RPC.create(channel)
            logger.info('Connection with RabbitMQ established')
            break
        except:
            if i >= max_attempts:
                logger.error(get_raised_error())
                logger.critical('Can\'t connect to RabbitMQ, goodbye honey!\n')
                panic()
            else:
                logger.error(
                    f'Can\'t connect to RabbitMQ, do another (#{i + 1}) attempt...'
                )
                await asyncio.sleep(1)

    await init_handlers(rpc)
Ejemplo n.º 2
0
import os
import yaml
from pkg.arg_parser import create_argparse
from pkg.utils import get_project_root
from pkg.utils.console import panic
from pkg.utils.files import read_file

os.chdir(get_project_root())
args = create_argparse()
try:
    if args.config_file:
        CFG_FILE = args.config_file
    else:
        CFG_FILE = os.environ.get('SHYM_PLAZA_CONFIG', None) or 'config.yml'
    CONFIG = yaml.load(read_file(CFG_FILE), Loader=yaml.BaseLoader)
except FileNotFoundError:
    panic()
Ejemplo n.º 3
0
import copy
import pid
import sys
import tempfile
import yaml
import uvicorn
from pkg.config import CONFIG, CFG_FILE
from pkg.constants.version import SOFTWARE_VERSION
from pkg.utils.console import panic
from pkg.utils.logger import DEFAULT_LOGGER, LOG_CONFIG
from setproctitle import setproctitle

if __name__ == '__main__':
    if sys.version_info < (3, 8):
        panic(
            'We need minimum Python version 3.8 to run. Current version: %s.%s.%s'
            % sys.version_info[:3])

    host = CONFIG['rest']['listen_host']
    port = int(CONFIG['rest']['listen_port'])

    proc_name = f'arms-rest-{port}'
    setproctitle(proc_name)

    pid_dir = tempfile.gettempdir()
    pid_file = f'{pid_dir}/{proc_name}.pid'
    pid_ok = False
    try:
        with pid.PidFile(proc_name, piddir=pid_dir) as p:
            pid_ok = True
Ejemplo n.º 4
0
                ftp.quit()


def send_errors_notify():
    with SmtpServer() as smtp:
        if smtp.is_initialized():
            smtp.send_emails(
                f'{APP_NAME} notification',
                'Some errors occurred while uploading data. See server logs for details'
            )


if __name__ == '__main__':
    if sys.version_info < (3, 8):
        panic(
            'We need minimum Python version 3.8 to run. Current version: %s.%s.%s'
            % sys.version_info[:3])

    args = create_argparse()

    if (not args.get_sales) and (not args.upload_to_ftp):
        panic('Specify -g or/and -u option. See --help for details')

    DEFAULT_LOGGER.info(f'{SOFTWARE_VERSION} started')
    DEFAULT_LOGGER.info(f'Config loaded from {CFG_FILE}\n')

    if args.get_sales:
        if args.yesterday:
            yesterday_str = datetime.strftime(
                datetime.now() - timedelta(days=1), DATE_FORMAT)
            beg_str = f'{yesterday_str} 00:00:00'
Ejemplo n.º 5
0
                logger.error(get_raised_error())
                logger.critical('Can\'t connect to RabbitMQ, goodbye honey!\n')
                panic()
            else:
                logger.error(
                    f'Can\'t connect to RabbitMQ, do another (#{i + 1}) attempt...'
                )
                await asyncio.sleep(1)

    await init_handlers(rpc)


if __name__ == '__main__':
    if sys.version_info < (3, 6):
        panic(
            'We need mininum Python verion 3.6 to run. Current version: %s.%s.%s'
            % sys.version_info[:3])

    env_config_path = os.environ['ALPINEBOOK_REPORT_CONFIG_PATH'] \
        if 'ALPINEBOOK_REPORT_CONFIG_PATH' in os.environ else None
    config_path = env_config_path
    if not config_path:
        server_dir, _ = os.path.split(os.path.abspath(__file__))
        config_path = os.path.join(server_dir, 'config.py')

    try:
        cfg_module = load_config(config_path)
        pkg.constants.CONFIG = cfg_module.CONFIG
        pkg.constants.DEBUG = cfg_module.CONFIG[
            'debug'] if 'debug' in cfg_module.CONFIG else False
        host = cfg_module.CONFIG['http']['host']
Ejemplo n.º 6
0
async def setup_postgres(app, loop):
    def get_dsn(secure=False):
        user = CONFIG['postgres']['user']
        pswd = '*****' if secure else CONFIG['postgres']['pass']
        host = CONFIG['postgres']['host']
        port = CONFIG['postgres']['port']
        db = CONFIG['postgres']['db']
        return f'postgres://{user}:{pswd}@{host}:{port}/{db}'

    logger = logging.getLogger('postgres')
    logger.info(f'Connecting to {get_dsn(secure=True)}')

    try:
        min_s = CONFIG['postgres']['pool']['min-size']
    except:
        min_s = 1
    try:
        max_s = CONFIG['postgres']['pool']['max-size']
    except:
        max_s = 10
    try:
        micl = CONFIG['postgres']['pool']['max-inactive-connection-lifetime']
    except:
        micl = 0
    try:
        command_timeout = CONFIG['postgres']['pool']['command-timeout']
    except:
        command_timeout = 60
    try:
        use_ssl = CONFIG['postgres']['ssl']
    except:
        use_ssl = False
    try:
        max_attempts = CONFIG['postgres']['max-conn-attempts']
    except:
        max_attempts = 120

    pool_info = f'pool.min-size: {min_s}, pool.max-size: {max_s}, pool.max-inactive-connection-lifetime: {micl}'
    logger.info(f'Connection settings: {pool_info}, command-timeout: {command_timeout}, ssl: {use_ssl}')

    if use_ssl:
        ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
        ctx.verify_mode = ssl.CERT_NONE
    else:
        ctx = False

    i, dsn = 0, get_dsn()
    while i < max_attempts:
        i += 1
        try:
            app.pool = await asyncpg.create_pool(dsn=dsn,
                                                 min_size=min_s,
                                                 max_size=max_s,
                                                 max_inactive_connection_lifetime=micl,
                                                 command_timeout=command_timeout,
                                                 ssl=ctx)
            app.pool_max_size = max_s
            async with PoolProxy(app.pool).acquire() as conn:
                info = conn.get_server_version()
            logger.info(f'Connection with PostgreSQL established: {info}')
            break
        except Exception as e:
            if i >= max_attempts:
                logger.exception(str(e))
                logging.getLogger('alpinebook').critical('Can\'t connect to PostgreSQL, goodbye honey!\n')
                panic()
            else:
                logger.error(f'Can\'t connect to PostgreSQL, do another (#{i + 1}) attempt...')
                await asyncio.sleep(1)

    for query in filter(lambda q: 'SQL_' == q[:4], dir(pkg.postgresql.queries)):
        app.db_queries[query.lower()[4:]] = getattr(pkg.postgresql.queries, query)
Ejemplo n.º 7
0
                        bar.message = f'{curr_op} of {ops_count}'
                        bar.update()
                    bar.next()
            i += 1

    bar.message = 'Done'
    bar.update()

    out_csv_file.close()
    in_csv_file.close()


if __name__ == '__main__':
    if sys.version_info < (3, 8):
        panic(
            'We need minimum Python version 3.8 to run. Current version: %s.%s.%s'
            % sys.version_info[:3])

    args = create_argparse()
    if args.config:
        CONFIG = yaml.safe_load(read_file(args.config))
        print(f'Config loaded from {args.config}')

        if args.output:
            if args.short:
                if args.input:
                    process_short(args.output, args.input, args.algorithm)
                else:
                    panic('No input file specified. Use -i option')
            else:
                process_default(args.output, args.algorithm)
Ejemplo n.º 8
0
import csv
import pandas as pd
import matplotlib.pyplot as plt
import sys
import yaml
from pkg.arg_parser.tester import create_argparse
from pkg.data import get_barcode_daily_sales, create_engine
from pkg.utils.console import panic
from pkg.utils.files import read_file
from pkg.utils.series import get_forecast_accuracy_errors, get_forecast_standard_deviation
from progress.bar import ChargingBar


if __name__ == '__main__':
    if sys.version_info < (3, 8):
        panic('We need minimum Python version 3.8 to run. Current version: %s.%s.%s' % sys.version_info[:3])

    args = create_argparse()
    if args.generate:
        if args.config:
            if args.output:
                csv_file = open(args.output, 'w', newline='')
                csv_writer = csv.writer(csv_file, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

                config = yaml.safe_load(read_file(args.config))
                print(f'Config loaded from {args.config}')

                engine = create_engine(config['mysql'])

                print(f'Processing...')
                stores = config['stores']