Skip to content
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

Reconnect to MongoDB #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/logstash/outputs/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class LogStash::Outputs::Mongodb < LogStash::Outputs::Base
# "_id" field in the event.
config :generateId, :validate => :boolean, :default => false

# Number of insert attempts before reconnect to MongoDB
config :retries_before_reconnect, :validate => :number, :default => 3, :required => false

public
def register
Mongo::Logger.logger = @logger
Expand All @@ -42,6 +45,7 @@ def register
end # def register

def receive(event)
retries = 0
begin
# Our timestamp object now has a to_bson method, using it here
# {}.merge(other) so we don't taint the event hash innards
Expand All @@ -65,8 +69,13 @@ def receive(event)
# to fix the issue.
else
sleep @retry_delay
retries += 1
if @retries_before_reconnect == retries
register
retries = 0
end
retry
end
end
end # def receive
end # class LogStash::Outputs::Mongodb
end # class LogStash::Outputs::Mongodb