2020-06-27 00:42:10 +03:00
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
# Licensed under the MIT License.
|
2018-11-21 04:32:42 +03:00
|
|
|
"""Main entrypoint."""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Python Azure Functions Worker')
|
2020-09-25 22:44:50 +03:00
|
|
|
parser.add_argument('--host',
|
|
|
|
help="host address")
|
|
|
|
parser.add_argument('--port', type=int,
|
2022-01-15 01:05:45 +03:00
|
|
|
help='port number')
|
2020-09-25 22:44:50 +03:00
|
|
|
parser.add_argument('--workerId', dest='worker_id',
|
|
|
|
help='id for the worker')
|
|
|
|
parser.add_argument('--requestId', dest='request_id',
|
2022-01-15 01:05:45 +03:00
|
|
|
help='id of the request')
|
2018-11-21 04:32:42 +03:00
|
|
|
parser.add_argument('--log-level', type=str, default='INFO',
|
2020-09-25 22:44:50 +03:00
|
|
|
choices=['TRACE', 'INFO', 'WARNING', 'ERROR'],
|
|
|
|
help="log level: 'TRACE', 'INFO', 'WARNING', "
|
|
|
|
"or 'ERROR'")
|
2018-11-21 04:32:42 +03:00
|
|
|
parser.add_argument('--log-to', type=str, default=None,
|
|
|
|
help='log destination: stdout, stderr, '
|
|
|
|
'syslog, or a file path')
|
|
|
|
parser.add_argument('--grpcMaxMessageLength', type=int,
|
|
|
|
dest='grpc_max_msg_len')
|
2023-09-20 22:53:49 +03:00
|
|
|
parser.add_argument('--functions-uri', dest='functions_uri', type=str,
|
|
|
|
help='URI with IP Address and Port used to'
|
|
|
|
' connect to the Host via gRPC.')
|
|
|
|
parser.add_argument('--functions-request-id', dest='functions_request_id',
|
|
|
|
type=str, help='Request ID used for gRPC communication '
|
|
|
|
'with the Host.')
|
|
|
|
parser.add_argument('--functions-worker-id',
|
|
|
|
dest='functions_worker_id', type=str,
|
|
|
|
help='Worker ID assigned to this language worker.')
|
|
|
|
parser.add_argument('--functions-grpc-max-message-length', type=int,
|
|
|
|
dest='functions_grpc_max_msg_len',
|
|
|
|
help='Max grpc message length for Functions')
|
2018-11-21 04:32:42 +03:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2021-01-13 09:05:56 +03:00
|
|
|
from .utils.dependency import DependencyManager
|
|
|
|
DependencyManager.initialize()
|
|
|
|
DependencyManager.use_worker_dependencies()
|
|
|
|
|
2024-02-13 22:08:40 +03:00
|
|
|
import asyncio
|
2024-07-12 00:29:32 +03:00
|
|
|
|
2021-01-13 09:05:56 +03:00
|
|
|
from . import logging
|
2024-07-12 00:29:32 +03:00
|
|
|
from .logging import error_logger, format_exception, logger
|
2021-01-13 09:05:56 +03:00
|
|
|
|
2018-11-21 04:32:42 +03:00
|
|
|
args = parse_args()
|
|
|
|
logging.setup(log_level=args.log_level, log_destination=args.log_to)
|
|
|
|
|
|
|
|
logger.info('Starting Azure Functions Python Worker.')
|
|
|
|
logger.info('Worker ID: %s, Request ID: %s, Host Address: %s:%s',
|
|
|
|
args.worker_id, args.request_id, args.host, args.port)
|
|
|
|
|
|
|
|
try:
|
2024-02-13 22:08:40 +03:00
|
|
|
return asyncio.run(start_async(
|
2020-03-17 10:25:13 +03:00
|
|
|
args.host, args.port, args.worker_id, args.request_id))
|
2022-06-30 18:32:28 +03:00
|
|
|
except Exception as ex:
|
|
|
|
error_logger.exception(
|
|
|
|
'unhandled error in functions worker: {0}'.format(
|
|
|
|
format_exception(ex)))
|
2018-11-21 04:32:42 +03:00
|
|
|
raise
|
|
|
|
|
|
|
|
|
2020-03-17 10:25:13 +03:00
|
|
|
async def start_async(host, port, worker_id, request_id):
|
2021-01-13 09:05:56 +03:00
|
|
|
from . import dispatcher
|
|
|
|
|
2020-09-25 22:44:50 +03:00
|
|
|
disp = await dispatcher.Dispatcher.connect(host=host, port=port,
|
|
|
|
worker_id=worker_id,
|
|
|
|
request_id=request_id,
|
|
|
|
connect_timeout=5.0)
|
2018-11-21 04:32:42 +03:00
|
|
|
|
|
|
|
await disp.dispatch_forever()
|