#!/usr/bin/env python3
# Copyright 2022 Katteli Inc.
# TestFlows.com Open-Source Software Testing Framework (http://testflows.com)
#
# 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 sys

from testflows._core.parallel.service import process_service
from testflows._core.parallel.executor.process import WorkQueueType
try:
    import testflows.settings as settings

    from testflows._core.exceptions import exception
    from testflows._core.cli.text import danger, warning
    from testflows._core.cli.arg.exit import *
    from testflows._core.cli.arg.parser import ArgumentParser
    from testflows._core.parallel.executor.process import WORKER_READY
    from testflows._core.parallel.service import Address
except KeyboardInterrupt:
    sys.exit(1)

def worker_ready():
    """Signal that worker is ready.
    """
    sys.stdout.write(WORKER_READY)
    sys.stdout.flush()

def worker(args):
    """TestFlows process worker.
    """
    worker_ready()

    process_service()

    try:
        work_queue = WorkQueueType(oid=int(args.oid), address=Address(args.hostname, int(args.port)))
        while True:
            work_item = work_queue.get(block=True)
            try:
                if work_item is not None:
                    work_item.run()
                    del work_item
                    continue
                else:
                    # exit worker
                    break
            finally:
                work_queue.task_done()
    except BaseException as e:
        raise


parser = ArgumentParser(prog="tfs-worker", description="""Worker process that executes remote tests.""")

parser.add_argument("--debug", dest="debug", action="store_true",
                    help="enable debugging mode", default=False)
parser.add_argument("--no-colors", dest="no_colors", action="store_true",
                   help="disable terminal color highlighting", default=False)
parser.add_argument("--hostname", type=str, metavar="hostname", required=True,
                    help="hostname of the remote service that provides work queue")
parser.add_argument("--port", type=int, metavar="port", required=True,
                    help="port of the remote service that provide work queue")
parser.add_argument("--oid", type=str, metavar="id", required=True,
                    help="object id of the work queue on the remote service")

parser.set_defaults(func=worker)

exitcode = 0

try:
    if len(sys.argv) == 1:
        exitcode = 1
        sys.argv.append("-h")

    args, unknown = parser.parse_known_args()

    if args.debug:
        settings.debug = True

    if args.no_colors:
        settings.no_colors = True

    if unknown:
        raise ExitWithError(f"unknown argument {unknown}")

    args.func(args)

except (ExitException, KeyboardInterrupt, Exception) as exc:
    if settings.debug:
        sys.stderr.write(warning(exception(), eol='\n'))
    if not isinstance(exc, (KeyboardInterrupt, BrokenPipeError)):
        sys.stderr.write(danger("error: " + str(exc).strip()))
    else:
        pass

    if isinstance(exc, ExitException):
        exitcode = exc.exitcode
    else:
        exitcode = 1
except SystemExit as exc:
    exitcode = exc.code or exitcode
finally:
    if not sys.stdout.isatty():
        try:
            sys.stdout.close()
        except BrokenPipeError:
            pass
    sys.exit(exitcode)
