Skip to content

Commit 70aae88

Browse files
Merge pull request #7 from BranchIntl/safe-shutdown-producer
Close producers when shutting down
2 parents e2d10c0 + 3f03485 commit 70aae88

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

lib/advanced_sneakers_activejob.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ def delayed_publisher
6666
@delayed_publisher ||= AdvancedSneakersActiveJob::DelayedPublisher.new(**config.publisher_config)
6767
end
6868

69+
def shutdown
70+
close_publisher(@publisher)
71+
@publisher = nil
72+
73+
close_publisher(@delayed_publisher)
74+
@delayed_publisher = nil
75+
end
76+
6977
# Based on ActiveSupport::Inflector#parameterize
7078
def consumer_name(queue_name:)
7179
# replace accented chars with their ascii equivalents
@@ -87,5 +95,22 @@ def const_missing(name)
8795

8896
constants.include?(name) ? const_get(name) : super
8997
end
98+
99+
private
100+
101+
def close_publisher(publisher)
102+
return unless publisher
103+
104+
begin
105+
publisher.close
106+
rescue StandardError => e
107+
Rails.logger&.error "Error closing publisher: #{e.message}" if defined?(Rails)
108+
end
109+
end
90110
end
91111
end
112+
113+
# Register shutdown hook to ensure connections are closed properly during process exit
114+
at_exit do
115+
AdvancedSneakersActiveJob.shutdown
116+
end

0 commit comments

Comments
 (0)