-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/3689 kafka fallback #2306
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9b14826
Fallback for kafka failure initial implementation
RK206 c4adc17
Sync feature branches with develop branch
RK206 36d1269
Merge branch 'develop' of https://github.com/DOAJ/doaj into feature/3…
RK206 b609851
Merge branch 'develop' of https://github.com/DOAJ/doaj into feature/3…
RK206 9ce1167
test cases to check the fallback feature
RK206 de2d154
Add event logs
RK206 313146f
Merge branch 'develop' of https://github.com/DOAJ/doaj into feature/3…
RK206 e716479
Merge branch 'develop' of https://github.com/DOAJ/doaj into feature/3…
RK206 d7badea
Merge branch 'develop' of https://github.com/DOAJ/doaj into feature/3…
RK206 279dc2a
corrected the log directory to ~/appdata/doaj
RK206 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Sync Feature Branch with Develop | ||
|
||
on: | ||
pull_request: | ||
types: | ||
- opened | ||
- synchronize | ||
|
||
jobs: | ||
sync-with-develop: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Fetch and rebase develop | ||
run: | | ||
git fetch origin | ||
git rebase origin/develop | ||
|
||
- name: Push changes back to feature branch | ||
run: | | ||
git push origin HEAD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from redis import StrictRedis | ||
from unittest.mock import patch, Mock, MagicMock | ||
from doajtest.helpers import DoajTestCase | ||
from portality.events.system_status_check import KafkaStatusCheck | ||
import portality.events.kafka_producer as kafka_events | ||
class TestKafkaStatusCheck(DoajTestCase): | ||
|
||
def setUp(self): | ||
super(TestKafkaStatusCheck, self).setUp() | ||
self.kafka_status = KafkaStatusCheck() | ||
|
||
@patch.object(StrictRedis, "get", return_value=b'True') | ||
@patch.object(KafkaStatusCheck, "can_check_redis", return_value=True) | ||
def test_active_status(self, mock_kafka_status_check, mock_strict_redis): | ||
status = self.kafka_status.is_active() | ||
self.assertTrue(status) | ||
mock_kafka_status_check.assert_called_once() | ||
mock_strict_redis.assert_called_once() | ||
|
||
@patch.object(StrictRedis, "get", return_value=b'False') | ||
@patch.object(KafkaStatusCheck, "can_check_redis", return_value=True) | ||
def test_inactive_status(self, mock_kafka_status_check, mock_strict_redis): | ||
status = self.kafka_status.is_active() | ||
self.assertFalse(status) | ||
mock_kafka_status_check.assert_called_once() | ||
mock_strict_redis.assert_called_once() | ||
|
||
class TestKafkaProducer(DoajTestCase): | ||
|
||
# Test for handle_exception | ||
@patch('portality.core.app.logger.exception') | ||
@patch('portality.app_email.send_mail') | ||
def test_handle_exception(self, mock_send_mail, mock_logger_exception): | ||
error_msg = "Sample error" | ||
exception = Exception("Sample exception") | ||
kafka_events.handle_exception(error_msg, exception) | ||
|
||
mock_logger_exception.assert_called_once_with(error_msg + str(exception)) | ||
mock_send_mail.assert_called_once() | ||
|
||
# Test for kafka_producer when producer is None and no exception raised | ||
@patch('kafka.KafkaProducer') | ||
def test_kafka_producer_new(self, mock_kafka_producer): | ||
kafka_events.producer = None | ||
result = kafka_events.kafka_producer() | ||
|
||
self.assertIsNotNone(result) | ||
mock_kafka_producer.assert_called_once() | ||
|
||
# Test for kafka_producer when producer is already set | ||
def test_kafka_producer_existing(self): | ||
kafka_events.producer = Mock() | ||
result = kafka_events.kafka_producer() | ||
|
||
self.assertEqual(result, kafka_events.producer) | ||
|
||
# Test for kafka_producer when exception raised | ||
@patch('kafka.KafkaProducer', side_effect=Exception("Kafka error")) | ||
@patch('portality.events.kafka_producer.handle_exception') | ||
def test_kafka_producer_exception(self, mock_handle_exception, _): | ||
kafka_events.producer = None | ||
result = kafka_events.kafka_producer() | ||
|
||
self.assertIsNone(result) | ||
mock_handle_exception.assert_called_once() | ||
|
||
# Test for send_event when kafka_status is None | ||
@patch('portality.events.kafka_producer.shortcircuit_send_event') | ||
def test_send_event_status_none(self, mock_shortcircuit_send_event): | ||
kafka_events.kafka_status = None | ||
|
||
kafka_events.send_event(Mock()) | ||
mock_shortcircuit_send_event.assert_called_once() | ||
|
||
# Test for send_event when everything is operational | ||
@patch.object(KafkaStatusCheck, 'is_active', return_value=True) | ||
@patch('portality.events.kafka_producer.kafka_producer', return_value=Mock()) | ||
@patch('portality.events.shortcircuit') | ||
def test_send_event_operational(self, mock_shortcircuit, _, __): | ||
kafka_events.kafka_status = KafkaStatusCheck() | ||
|
||
kafka_events.send_event(Mock()) | ||
mock_shortcircuit.assert_not_called() | ||
|
||
# Test for send_event when exception occurs | ||
@patch('kafka.KafkaProducer', return_value=Mock(send=MagicMock(side_effect=Exception("Send error")))) | ||
@patch.object(KafkaStatusCheck, 'set_kafka_inactive_redis') | ||
@patch.object(KafkaStatusCheck, 'is_active', return_value=True) | ||
@patch('portality.events.kafka_producer.shortcircuit_send_event') | ||
@patch('portality.events.kafka_producer.handle_exception') | ||
@patch('portality.events.kafka_producer.producer', new=None) | ||
def test_send_event_exception(self, __, mock_handle_exception, mock_shortcircuit, _, mock_kafka_producer): | ||
kafka_events.kafka_status = KafkaStatusCheck() | ||
|
||
kafka_events.send_event(Mock()) | ||
mock_handle_exception.assert_called() | ||
mock_shortcircuit.assert_called_once() | ||
mock_kafka_producer.assert_called_once() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,54 @@ | ||
import json | ||
from kafka import KafkaProducer | ||
import kafka | ||
from portality.core import app | ||
from portality import app_email, util | ||
from portality.events.shortcircuit import send_event as shortcircuit_send_event | ||
from portality.events.system_status_check import KafkaStatusCheck | ||
|
||
from portality.core import app as doajapp | ||
bootstrap_server = doajapp.config.get("KAFKA_BOOTSTRAP_SERVER") | ||
|
||
producer = KafkaProducer(bootstrap_servers=bootstrap_server, value_serializer=lambda v: json.dumps(v).encode('utf-8')) | ||
def handle_exception(error_msg, exception): | ||
app.logger.exception(error_msg + str(exception)) | ||
app_email.send_mail( | ||
to=[app.config.get('ADMIN_EMAIL', '[email protected]')], | ||
fro=app.config.get('SYSTEM_EMAIL_FROM', '[email protected]'), | ||
subject='Alert: DOAJ Kafka send event error', | ||
msg_body=error_msg + ": \n" + str(exception) | ||
) | ||
|
||
producer = None | ||
event_logger = util.custom_timed_rotating_logger('producer_log.log') | ||
|
||
def kafka_producer(): | ||
global producer | ||
try: | ||
if producer is None: | ||
producer = kafka.KafkaProducer(bootstrap_servers=bootstrap_server, value_serializer=lambda v: json.dumps(v).encode('utf-8')) | ||
except Exception as exp: | ||
producer = None | ||
handle_exception("Error in setting up kafka connection", exp) | ||
return producer | ||
|
||
try: | ||
kafka_status = KafkaStatusCheck() | ||
except Exception as exp: | ||
kafka_status = None | ||
handle_exception("Error in setting up Redis for kafka events", exp) | ||
|
||
|
||
def send_event(event): | ||
future = producer.send('events', value=event.serialise()) | ||
future.get(timeout=60) | ||
try: | ||
event_logger.info(event.data) | ||
if kafka_status and kafka_status.is_active() and kafka_producer(): | ||
future = producer.send('events', value=event.serialise()) | ||
future.get(timeout=60) | ||
else: | ||
shortcircuit_send_event(event) | ||
except Exception as e: | ||
try: | ||
kafka_status.set_kafka_inactive_redis() | ||
except Exception as exp: | ||
handle_exception("Failed to set kafka inactive status in Redis", exp) | ||
shortcircuit_send_event(event) | ||
handle_exception("Failed to send event to Kafka.", e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
from portality.bll import DOAJ | ||
from portality import util | ||
|
||
event_logger = util.custom_timed_rotating_logger('shortcircuit_log.log') | ||
|
||
def send_event(event): | ||
event_logger.info(event.data) | ||
svc = DOAJ.eventsService() | ||
svc.consume(event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import redis | ||
from portality import constants | ||
from portality.core import app | ||
from portality.lib import dates | ||
|
||
class KafkaStatusCheck: | ||
""" | ||
useful to set Kafka status to false when there is any issue connecting to Kafka. | ||
If kafka status set to false in Redis, it has to be set to true manually in Radis after fixing the issue. | ||
""" | ||
|
||
def __init__(self): | ||
self.is_kafka_active = True | ||
self.time_gap = app.config['TIME_GAP_REDIS_KAFKA'] | ||
self.last_time = dates.now() | ||
redis_host = app.config['HUEY_REDIS_HOST'] | ||
redis_port = app.config['HUEY_REDIS_PORT'] | ||
self.redis_conn = redis.StrictRedis(host=redis_host, port=redis_port, db=0) | ||
|
||
def is_active(self): | ||
if self.can_check_redis(): | ||
self.is_kafka_active = self.is_kafka_active_redis() | ||
return self.is_kafka_active | ||
|
||
def can_check_redis(self): | ||
time_diff = dates.now() - self.last_time | ||
if time_diff.seconds > self.time_gap: | ||
self.last_time = dates.now() | ||
return True | ||
|
||
return False | ||
|
||
def is_kafka_active_redis(self): | ||
value = self.redis_conn.get(constants.KAFKA_ACTIVE_STATUS) | ||
# set the key value if not set | ||
if value is None: | ||
self.set_default_kafka_status() | ||
return True | ||
return value.decode('utf-8').lower() == "true" | ||
|
||
def set_default_kafka_status(self): | ||
self.redis_conn.set(constants.KAFKA_ACTIVE_STATUS, "true") | ||
|
||
def set_kafka_inactive_redis(self): | ||
self.redis_conn.set(constants.KAFKA_ACTIVE_STATUS, "false") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is probably best suited to the
appdata
folder - currently in~/appdata/doaj/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated log dir to
~/appdata/ doaj
atlog_dir = os.path.join(user_home, 'appdata', 'doaj')