Revert commit fc1e7739506eb010707cf94ea02e412c8819c154
Revert adding common caching and offloading tasks
Change-Id: Ie07f8ba9f5f86f94b912d969ff8839d22e6eef88
Related-bug: PROD-30846
diff --git a/sf_notifier/server.py b/sf_notifier/server.py
index 9a5da5d..4d3ddc0 100644
--- a/sf_notifier/server.py
+++ b/sf_notifier/server.py
@@ -1,19 +1,35 @@
+# Copyright 2018: Mirantis Inc.
+# All Rights Reserved.
+#
+# 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 json
from logging.config import dictConfig
-import time
from flask import Flask, Response, jsonify, request
-from prometheus_client import make_wsgi_app
-from requests.exceptions import ConnectionError as RequestsConnectionError
-from simple_salesforce.exceptions import SalesforceError
-from simple_settings import settings
-from werkzeug.wsgi import DispatcherMiddleware
-from uwsgidecorators import mulefunc
+from prometheus_client import make_wsgi_app
+
+from requests.exceptions import ConnectionError as RequestsConnectionError
from sf_notifier.helpers import alert_fields_and_action, create_file
from sf_notifier.salesforce.client import SESSION_FILE, SalesforceClient
-from sf_notifier.salesforce.settings import CASE_STATUS
+
+from simple_salesforce.exceptions import SalesforceError
+
+from simple_settings import settings
+
+from werkzeug.wsgi import DispatcherMiddleware
dictConfig(settings.LOGGING)
@@ -36,56 +52,6 @@
})
-@mulefunc
-def offload(action, fields):
- try:
- getattr(sf_cli, action)(*fields)
- except (SalesforceError, RequestsConnectionError) as err:
- msg = 'Salesforce request failure: {}.'.format(err)
- sf_cli.metrics['sf_error_count'].inc()
- app.logger.error(msg)
-
-
-def create_case_results(alert_ids):
- cases = {}
- is_error = False
-
- while len(alert_ids) > len(cases):
- for alert_id in alert_ids:
- case = sf_cli._registered_alerts.get(alert_id)
-
- if case is None:
- continue
-
- if case['id'] not in CASE_STATUS:
- cases[alert_id] = {}
- cases[alert_id]['case'] = case['id']
- if case['id'] == 'error':
- if sf_cli._feed_update_ready(case['last_update']):
- continue
- is_error = True
- cases[alert_id] = {}
- cases[alert_id]['error'] = case['error']
-
- time.sleep(0.2)
- return cases, is_error
-
-
-def close_case_results(alert_ids):
- # timeout is implicit error
- cases = {}
-
- while len(alert_ids) > 0:
- for alert_id in alert_ids:
- case = sf_cli._registered_alerts.get(alert_id)
-
- if case is None:
- cases[alert_id] = {'status': 'closed'}
- alert_ids.pop(alert_ids.index(alert_id))
- time.sleep(0.2)
- return cases, False
-
-
@app.route('/hook', methods=['POST'])
def webhook_receiver():
@@ -100,21 +66,31 @@
app.logger.info('Received requests: {}'.format(data))
- alert_ids = []
+ cases = []
for alert in data['alerts']:
- alert['labels']['env_id'] = sf_cli.environment
- alert_ids.append(sf_cli.get_alert_id(alert['labels']))
- fields, action = alert_fields_and_action(alert)
+ try:
+ alert['labels']['env_id'] = sf_cli.environment
+ fields, action = alert_fields_and_action(alert)
+ except KeyError as key:
+ msg = 'Alert misses {} key.'.format(key)
+ app.logger.error(msg)
+ return Response(json.dumps({'error': msg}),
+ status=400,
+ mimetype='application/json')
- offload(action, fields)
+ if fields:
+ try:
+ cases.append(getattr(sf_cli, action)(*fields))
+ except (SalesforceError, RequestsConnectionError) as err:
+ msg = 'Salesforce request failure: {}.'.format(err)
+ sf_cli.metrics['sf_error_count'].inc()
+ app.logger.error(msg)
+ return Response(json.dumps({'error': msg}),
+ status=500,
+ mimetype='application/json')
- cases, is_error = globals()[action + '_results'](alert_ids)
-
- if is_error:
- return Response(json.dumps(cases),
- status=500,
- mimetype='application/json')
-
+ if len(cases) == 1:
+ return jsonify(cases[0])
return jsonify(cases)