Skip to content

Commit f897525

Browse files
committed
Log and handle any exceptions on process exit
1 parent 1321d42 commit f897525

3 files changed

Lines changed: 12 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Added
1111
- Add `Signal::HUP` to `Mel.stop` signals
1212
- Add Windows support to signal handlers
13+
- Log and handle any exceptions on process exit
1314

1415
### Fixed
1516
- Synchronize `Mel.state` reads with writes

src/mel.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module Mel
2828

2929
extend self
3030

31+
include Helpers
3132
include LogHelpers
3233

3334
@@mutex = Mutex.new(:reentrant)
@@ -131,6 +132,8 @@ module Mel
131132
{% else %}
132133
{Signal::HUP, Signal::INT, Signal::TERM}.each &.trap { stop }
133134
{% end %}
135+
136+
at_exit { |code, error| handle_exit(code, error) }
134137
end
135138

136139
private def batch_size(pond)

src/mel/helpers.cr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,13 @@ module Mel::Helpers
33
private def handle_error(error) : Nil
44
Mel.settings.error_handler.call(error)
55
end
6+
7+
private def handle_exit(code, error) : Nil
8+
message = "Mel exited with code #{code}"
9+
return Mel.log.info &.emit(message) if code == 0 && error.nil?
10+
11+
Mel.log.fatal(exception: error, &.emit(message))
12+
handle_error(error) if error
13+
end
614
end
715
end

0 commit comments

Comments
 (0)