2017-02-06 12:45:23 -07:00
|
|
|
# (C) Copyright 2014-2017 Hewlett Packard Enterprise Development LP
|
2017-07-19 13:53:04 +02:00
|
|
|
# Copyright 2017 FUJITSU LIMITED
|
2014-02-27 16:55:07 -07:00
|
|
|
#
|
2014-05-01 12:27:06 -06:00
|
|
|
# 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.
|
|
|
|
|
2014-02-27 16:55:07 -07:00
|
|
|
""" Notification Engine
|
2018-04-11 14:15:08 +02:00
|
|
|
This engine reads alarms from Kafka and then notifies the customer using their configured
|
|
|
|
notification method.
|
2014-02-27 16:55:07 -07:00
|
|
|
"""
|
|
|
|
|
2014-03-17 16:29:09 -06:00
|
|
|
import multiprocessing
|
2014-03-19 09:22:55 -06:00
|
|
|
import os
|
2014-02-27 16:55:07 -07:00
|
|
|
import signal
|
|
|
|
import sys
|
2014-03-19 09:22:55 -06:00
|
|
|
import time
|
2017-05-16 00:14:56 +02:00
|
|
|
import warnings
|
2014-02-27 16:55:07 -07:00
|
|
|
|
2017-05-16 00:14:56 +02:00
|
|
|
from oslo_log import log
|
|
|
|
|
|
|
|
from monasca_notification import config
|
|
|
|
from monasca_notification import notification_engine
|
|
|
|
from monasca_notification import periodic_engine
|
|
|
|
from monasca_notification import retry_engine
|
|
|
|
|
|
|
|
LOG = log.getLogger(__name__)
|
|
|
|
CONF = config.CONF
|
2014-02-27 16:55:07 -07:00
|
|
|
|
|
|
|
processors = [] # global list to facilitate clean signal handling
|
2014-03-19 09:22:55 -06:00
|
|
|
exiting = False
|
2014-02-27 16:55:07 -07:00
|
|
|
|
|
|
|
|
2014-03-06 17:24:29 -07:00
|
|
|
def clean_exit(signum, frame=None):
|
2016-12-16 02:44:21 +00:00
|
|
|
"""Exit all processes attempting to finish uncommitted active work before exit.
|
2014-03-17 16:29:09 -06:00
|
|
|
Can be called on an os signal or no zookeeper losing connection.
|
2014-02-27 16:55:07 -07:00
|
|
|
"""
|
2014-03-19 09:22:55 -06:00
|
|
|
global exiting
|
|
|
|
if exiting:
|
2015-01-28 15:34:52 -07:00
|
|
|
# Since this is set up as a handler for SIGCHLD when this kills one
|
|
|
|
# child it gets another signal, the global exiting avoids this running
|
|
|
|
# multiple times.
|
2017-05-16 00:14:56 +02:00
|
|
|
LOG.debug('Exit in progress clean_exit received additional signal %s' % signum)
|
2014-03-19 09:22:55 -06:00
|
|
|
return
|
|
|
|
|
2017-05-16 00:14:56 +02:00
|
|
|
LOG.info('Received signal %s, beginning graceful shutdown.' % signum)
|
2014-03-19 09:22:55 -06:00
|
|
|
exiting = True
|
2016-02-05 13:19:45 -07:00
|
|
|
wait_for_exit = False
|
2014-03-19 09:22:55 -06:00
|
|
|
|
2015-01-28 15:34:52 -07:00
|
|
|
for process in processors:
|
2014-03-17 15:05:17 -06:00
|
|
|
try:
|
2014-03-19 09:22:55 -06:00
|
|
|
if process.is_alive():
|
2018-04-11 14:15:08 +02:00
|
|
|
# Sends sigterm which any processes after a notification is sent attempt to handle
|
|
|
|
process.terminate()
|
2016-02-05 13:19:45 -07:00
|
|
|
wait_for_exit = True
|
2017-02-06 12:45:23 -07:00
|
|
|
except Exception: # nosec
|
|
|
|
# There is really nothing to do if the kill fails, so just go on.
|
|
|
|
# The # nosec keeps bandit from reporting this as a security issue
|
2014-03-19 09:22:55 -06:00
|
|
|
pass
|
|
|
|
|
2016-02-05 13:19:45 -07:00
|
|
|
# wait for a couple seconds to give the subprocesses a chance to shut down correctly.
|
|
|
|
if wait_for_exit:
|
|
|
|
time.sleep(2)
|
|
|
|
|
2014-03-19 09:22:55 -06:00
|
|
|
# Kill everything, that didn't already die
|
|
|
|
for child in multiprocessing.active_children():
|
2017-05-16 00:14:56 +02:00
|
|
|
LOG.debug('Killing pid %s' % child.pid)
|
2014-03-19 09:22:55 -06:00
|
|
|
try:
|
|
|
|
os.kill(child.pid, signal.SIGKILL)
|
2017-02-06 12:45:23 -07:00
|
|
|
except Exception: # nosec
|
|
|
|
# There is really nothing to do if the kill fails, so just go on.
|
|
|
|
# The # nosec keeps bandit from reporting this as a security issue
|
2014-03-17 15:05:17 -06:00
|
|
|
pass
|
2014-03-06 17:24:29 -07:00
|
|
|
|
2016-02-05 13:19:45 -07:00
|
|
|
if signum == signal.SIGTERM:
|
|
|
|
sys.exit(0)
|
|
|
|
|
2015-03-03 14:30:37 -07:00
|
|
|
sys.exit(signum)
|
2014-02-27 16:55:07 -07:00
|
|
|
|
|
|
|
|
2017-05-16 00:14:56 +02:00
|
|
|
def start_process(process_type, *args):
|
|
|
|
LOG.info("start process: {}".format(process_type))
|
|
|
|
p = process_type(*args)
|
2015-02-04 16:49:06 -07:00
|
|
|
p.run()
|
|
|
|
|
|
|
|
|
2014-02-27 16:55:07 -07:00
|
|
|
def main(argv=None):
|
2017-05-16 00:14:56 +02:00
|
|
|
warnings.simplefilter('always')
|
|
|
|
config.parse_args(argv=argv)
|
|
|
|
|
|
|
|
for proc in range(0, CONF.notification_processor.number):
|
2015-02-04 16:49:06 -07:00
|
|
|
processors.append(multiprocessing.Process(
|
2017-05-16 00:14:56 +02:00
|
|
|
target=start_process,
|
|
|
|
args=(notification_engine.NotificationEngine,))
|
|
|
|
)
|
2015-02-04 16:49:06 -07:00
|
|
|
|
|
|
|
processors.append(multiprocessing.Process(
|
2017-05-16 00:14:56 +02:00
|
|
|
target=start_process,
|
|
|
|
args=(retry_engine.RetryEngine,))
|
|
|
|
)
|
2014-02-27 16:55:07 -07:00
|
|
|
|
2019-11-12 10:38:15 +00:00
|
|
|
for notification_period in CONF.kafka.periodic.keys():
|
2016-05-04 16:23:12 -06:00
|
|
|
processors.append(multiprocessing.Process(
|
2017-05-16 00:14:56 +02:00
|
|
|
target=start_process,
|
2019-11-12 10:38:15 +00:00
|
|
|
args=(periodic_engine.PeriodicEngine, int(notification_period)))
|
2017-05-16 00:14:56 +02:00
|
|
|
)
|
2016-05-04 16:23:12 -06:00
|
|
|
|
2014-02-27 16:55:07 -07:00
|
|
|
try:
|
2017-05-16 00:14:56 +02:00
|
|
|
LOG.info('Starting processes')
|
2014-02-27 16:55:07 -07:00
|
|
|
for process in processors:
|
|
|
|
process.start()
|
2014-03-19 09:22:55 -06:00
|
|
|
|
2015-01-28 15:34:52 -07:00
|
|
|
# The signal handlers must be added after the processes start otherwise
|
|
|
|
# they run on all processes
|
2014-03-19 09:22:55 -06:00
|
|
|
signal.signal(signal.SIGCHLD, clean_exit)
|
2014-03-19 11:53:47 -06:00
|
|
|
signal.signal(signal.SIGINT, clean_exit)
|
|
|
|
signal.signal(signal.SIGTERM, clean_exit)
|
2014-03-19 09:22:55 -06:00
|
|
|
|
|
|
|
while True:
|
2015-01-28 15:34:52 -07:00
|
|
|
time.sleep(10)
|
|
|
|
|
2014-03-17 16:29:09 -06:00
|
|
|
except Exception:
|
2017-05-16 00:14:56 +02:00
|
|
|
LOG.exception('Error! Exiting.')
|
2016-02-05 13:19:45 -07:00
|
|
|
clean_exit(signal.SIGKILL)
|
2014-02-27 16:55:07 -07:00
|
|
|
|
2017-05-16 00:14:56 +02:00
|
|
|
|
2014-02-27 16:55:07 -07:00
|
|
|
if __name__ == "__main__":
|
2017-05-16 00:14:56 +02:00
|
|
|
sys.exit(main(sys.argv[1:]))
|