-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/liquidm/ext
- Loading branch information
Showing
13 changed files
with
45 additions
and
9 deletions.
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 |
---|---|---|
|
@@ -15,3 +15,4 @@ spec/reports | |
test/tmp | ||
test/version_tmp | ||
tmp | ||
.idea |
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,37 @@ | ||
module Shutdown | ||
|
||
# Receives a time span in seconds and a block which will be registered for execution when the program exits. | ||
# If multiple hanlders are registered, they are executed in reverse order of registration. | ||
# | ||
# If a handler throws an exception or takes longer then the specified time span, execution skips to the next | ||
# handler in line. | ||
# | ||
# If the first parameter is nil, the handler will not check the codes runtime. Be cautios when executing | ||
# asynchronous code when the 'seconds' parameter set to nil value as blocking codes would prevent | ||
# the process from terminating. | ||
def self.register_handler_with_timeout(seconds, &block) | ||
return unless block_given? | ||
at_exit do | ||
$log.info("shutdown", location: block.inspect) if $log | ||
begin | ||
Timeout::timeout(seconds, &block) | ||
rescue => e | ||
$log.exception e if $log | ||
end | ||
end | ||
end | ||
|
||
|
||
# Receives a block which will be registered for execution when the program exits. | ||
# If multiple hanlders are registered, they are executed in reverse order of registration. | ||
# | ||
# with_handler will not check the execution duration of the given block so only | ||
# use this for non_blocking code, use register_handler_with_timout if you make potentially | ||
# blocking calls | ||
def self.register_handler(&block) | ||
return unless block_given? | ||
self.register_handler_with_timeout(nil, &block) | ||
end | ||
|
||
end | ||
|
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