Use ProcessPool rather than ThreadPool Executor
[ccsdk/cds.git] / ms / command-executor / src / main / python / server.py
1 # !/usr/bin/python
2
3 #
4 # Copyright (C) 2019 Bell Canada.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 from builtins import KeyboardInterrupt
19 from concurrent import futures
20 import logging
21 import time
22 import sys
23
24 import grpc
25
26 import proto.CommandExecutor_pb2_grpc as CommandExecutor_pb2_grpc
27
28 from request_header_validator_interceptor import RequestHeaderValidatorInterceptor
29 from command_executor_server import CommandExecutorServer
30
31 logger = logging.getLogger("Server")
32
33 _ONE_DAY_IN_SECONDS = 60 * 60 * 24
34
35
36 def serve():
37     port = sys.argv[1]
38     basic_auth = sys.argv[2] + ' ' + sys.argv[3]
39
40     header_validator = RequestHeaderValidatorInterceptor(
41         'authorization', basic_auth, grpc.StatusCode.UNAUTHENTICATED,
42         'Access denied!')
43
44     server = grpc.server(
45         futures.ProcessPoolExecutor(),
46         interceptors=(header_validator,))
47
48     CommandExecutor_pb2_grpc.add_CommandExecutorServiceServicer_to_server(
49         CommandExecutorServer(), server)
50
51     server.add_insecure_port('[::]:' + port)
52     server.start()
53
54     logger.info("Command Executor Server started on %s" % port)
55
56     try:
57         while True:
58             time.sleep(_ONE_DAY_IN_SECONDS)
59     except KeyboardInterrupt:
60         server.stop(0)
61
62
63 if __name__ == '__main__':
64     logging_formater = '%(asctime)s - %(name)s - %(threadName)s - %(levelname)s - %(message)s'
65     logging.basicConfig(filename='/opt/app/onap/logs/application.log', level=logging.DEBUG,
66                         format=logging_formater)
67     console = logging.StreamHandler()
68     console.setLevel(logging.INFO)
69     formatter = logging.Formatter(logging_formater)
70     console.setFormatter(formatter)
71     logging.getLogger('').addHandler(console)
72     serve()