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
Handle IOError exceptions #4
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
|
@@ -267,9 +267,13 @@ def stop(self): | |
self._signal("d") | ||
|
||
def _signal(self, signal): | ||
f = open(self._control,"w+") | ||
f.write(signal) | ||
f.close() | ||
try: | ||
f = open(self._control,"w+") | ||
f.write(signal) | ||
except IOError as e: | ||
raise ServiceControlError("Unable to write service control file '{0}'".format(e.filename)) | ||
else: | ||
f.close() | ||
|
||
def status(self): | ||
""" Read the status of a service using status binary form. Returns | ||
|
@@ -289,14 +293,21 @@ def status(self): | |
reached (means downtime for STATUS_DOWN). | ||
|
||
""" | ||
s = open(self._status,"rb").read(20) | ||
try: | ||
l = open(self._status,"rb") | ||
s = l.read(20) | ||
except IOError as e: | ||
raise ServiceStatusError("Unable to read service status file '{0}'".format(e.filename)) | ||
else: | ||
l.close() | ||
|
||
if len(s) == 18: | ||
seconds, nano, pid, paused, want = struct.unpack(">qllbc", s) | ||
term, finish = 0, 0 | ||
elif len(s) == 20: | ||
seconds, nano, pid, paused, want, term, finish = struct.unpack(">qllbcbb", s) | ||
else: | ||
raise AssertionError("Unknown status format") | ||
raise ServiceStatusError("Unknown status format") | ||
|
||
# pid is returned little-endian. Flip it. | ||
pid, = struct.unpack("<l", struct.pack(">l", pid)) | ||
|
@@ -329,3 +340,10 @@ def status(self): | |
seconds = 0 if now < seconds else (now - seconds) | ||
|
||
return ServiceStatus(status=status, pid=pid, action=action, uptime=seconds) | ||
|
||
|
||
class ServiceStatusError(Exception): | ||
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. Add a shared 'SuperviseError' base class. s/Service/Supervise/ for consistency with the module name. |
||
''' An error occurred while reading service status file''' | ||
|
||
class ServiceControlError(Exception): | ||
''' An error occurred while writing service control file''' |
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 kind of sketchy because of the write fails, we still want to close the file.
Probably initialize f = None outside of the try block and do a conditional close in a finally:
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.
You mean something like this?
What about splitting the 2 operations in different try/exc blocks?
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.
I was thinking:
Splitting and the extra try around the close is probably overkill, but I'm not used to weird scenarios that you can't close fds on.