This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Creating a restart function for storage classes to fix broken database connections #196
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,17 @@ | ||
[run] | ||
source = cnxauthoring | ||
branch = True | ||
|
||
[report] | ||
# Regexes for lines to exclude from consideration | ||
exclude_lines = | ||
# Have to re-enable the standard pragma | ||
pragma: no cover | ||
raise NotImplementedError | ||
omit = | ||
cnxauthoring/scripts/* | ||
cnxauthoring/tests/* | ||
ignore_errors = True | ||
|
||
[html] | ||
directory = coverage_html_report |
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
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 |
---|---|---|
|
@@ -2773,6 +2773,33 @@ def test_user_contents_hide_documents_inside_binders(self): | |
}, | ||
}) | ||
|
||
def test_db_restart(self): | ||
''' | ||
Test to see if the database resets itself after a broken | ||
connection | ||
''' | ||
import psycopg2 | ||
from ..storage import storage | ||
|
||
self.addCleanup(setattr, storage, 'conn', | ||
psycopg2.connect(storage.conn.dsn)) | ||
|
||
storage.conn.close() | ||
|
||
response = self.testapp.post_json( | ||
'/users/contents', | ||
{'title': u'My document タイトル'}, | ||
status=503, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
response = self.testapp.post_json( | ||
'/users/contents', | ||
{'title': u'My document タイトル'}, | ||
status=201, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '201 Created') | ||
|
||
def test_service_unavailable_response(self): | ||
''' | ||
Test service unavailable response when a request is made during a | ||
|
@@ -2793,31 +2820,64 @@ def test_service_unavailable_response(self): | |
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
storage.conn.close() | ||
|
||
response = self.testapp.get( | ||
'/resources/1234abcde', | ||
status=503, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
storage.conn.close() | ||
|
||
response = self.testapp.put_json( | ||
'/contents/[email protected]', | ||
{}, | ||
status=503, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
storage.conn.close() | ||
|
||
response = self.testapp.get( | ||
'/search', | ||
status=503, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
storage.conn.close() | ||
|
||
response = self.testapp.delete( | ||
'/contents/{}@draft'.format(id), | ||
status=503, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
@mock.patch('cnxauthoring.views.logger') | ||
def test_database_restart_failed(self, logger): | ||
import psycopg2 | ||
from ..storage import storage | ||
|
||
self.addCleanup(setattr, storage, 'conn', | ||
psycopg2.connect(storage.conn.dsn)) | ||
|
||
storage.conn.close() | ||
|
||
with mock.patch.object(storage, 'restart') as mock_restart: | ||
mock_restart.side_effect = storage.Error | ||
|
||
response = self.testapp.post_json( | ||
'/users/contents', | ||
{'title': 'Test Document'}, | ||
status=503) | ||
self.assertEqual(mock_restart.call_count, 1) | ||
|
||
self.assertEqual(logger.exception.call_count, 3) | ||
args1, args2, args3 = logger.exception.call_args_list | ||
self.assertEqual(args1[0], ('Storage failure',)) | ||
self.assertEqual(args2[0], ('Storage failed to abort',)) | ||
self.assertEqual(args3[0], ('Storage failed to restart',)) | ||
|
||
|
||
class PublicationTests(BaseFunctionalTestCase): | ||
|
||
|
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 |
---|---|---|
|
@@ -62,7 +62,12 @@ def wrapper(*args, **kwargs): | |
storage.abort() | ||
except storage.Error: | ||
logger.exception('Storage failed to abort') | ||
raise httpexceptions.HTTPServiceUnavailable() | ||
try: | ||
storage.restart() | ||
except storage.Error: | ||
logger.exception('Storage failed to restart') | ||
finally: | ||
raise httpexceptions.HTTPServiceUnavailable() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious if there is anyway to make this section more concise without having to use these nested try / except statements There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know a better way to do this. |
||
return wrapper | ||
|
||
|
||
|
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.
adding a variable to store the db_connection string needed to restart the database when a connection is broken or lost.
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.
Come to think about it, shouldn't your note be comment in the code?